BAEL-5157 - Exception Handling With Jersey (#12082)

* BAEL-5157 - Exception Handling with Jersey

First draft:
https://drafts.baeldung.com/wp-admin/post.php?post=131880&action=edit

* BAEL-5157

* more meaningful exception messages
This commit is contained in:
Ulisses Lima 2022-04-18 21:37:21 -03:00 committed by GitHub
parent 09d6595514
commit 66f829cd8d
2 changed files with 3 additions and 3 deletions

View File

@ -34,7 +34,7 @@ public class StocksResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam("ticker") String id) { public Response get(@PathParam("ticker") String id) {
Optional<Stock> stock = stocks.findById(id); Optional<Stock> stock = stocks.findById(id);
stock.orElseThrow(IllegalArgumentException::new); stock.orElseThrow(() -> new IllegalArgumentException("ticker"));
return Response.ok(stock.get()) return Response.ok(stock.get())
.build(); .build();

View File

@ -7,7 +7,7 @@ import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
public class ServerExceptionMapper implements ExceptionMapper<WebApplicationException> { public class ServerExceptionMapper implements ExceptionMapper<WebApplicationException> {
public static final String HTTP_405_MESSAGE = "METHOD_NOT_ALLOWED"; public static final String HTTP_405_MESSAGE = "use one of";
@Override @Override
public Response toResponse(final WebApplicationException exception) { public Response toResponse(final WebApplicationException exception) {
@ -18,7 +18,7 @@ public class ServerExceptionMapper implements ExceptionMapper<WebApplicationExce
switch (status) { switch (status) {
case METHOD_NOT_ALLOWED: case METHOD_NOT_ALLOWED:
message = HTTP_405_MESSAGE; message = HTTP_405_MESSAGE + response.getAllowedMethods();
break; break;
case INTERNAL_SERVER_ERROR: case INTERNAL_SERVER_ERROR:
message = "internal validation - " + exception; message = "internal validation - " + exception;