Implement ExceptionMapper (2.1.3.1)

This commit is contained in:
Tobias Eidelpes 2021-04-29 12:03:16 +02:00
parent 89467e49cb
commit 1e96417a3f
3 changed files with 34 additions and 4 deletions

View File

@ -32,8 +32,8 @@ public interface ITripServiceResource {
Response addStop(@PathParam("id") Long tripId, @FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException; Response addStop(@PathParam("id") Long tripId, @FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException;
@DELETE @DELETE
@Path("{id}/stops") @Path("{id}/stops/{locationId}")
Response removeStop(@PathParam("id") Long tripId, @FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException; Response removeStop(@PathParam("id") Long tripId, @PathParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException;
@POST @POST
@Path("{id}/match") @Path("{id}/match")

View File

@ -3,13 +3,13 @@ package dst.ass2.service.trip.impl;
import dst.ass2.service.api.trip.*; import dst.ass2.service.api.trip.*;
import dst.ass2.service.api.trip.rest.ITripServiceResource; import dst.ass2.service.api.trip.rest.ITripServiceResource;
import javax.annotation.Resource; import javax.inject.Inject;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
@Provider @Provider
public class TripServiceResource implements ITripServiceResource { public class TripServiceResource implements ITripServiceResource {
@Resource @Inject
private ITripService tripService; private ITripService tripService;
@Override @Override

View File

@ -0,0 +1,30 @@
package dst.ass2.service.trip.impl;
import dst.ass2.service.api.trip.DriverNotAvailableException;
import dst.ass2.service.api.trip.EntityNotFoundException;
import dst.ass2.service.api.trip.InvalidTripException;
import javax.ws.rs.NotAllowedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class TripServiceResourceExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable exception) {
if (exception instanceof EntityNotFoundException) {
return Response.status(Response.Status.NOT_FOUND).entity(exception.getMessage()).build();
} else if (exception instanceof IllegalStateException) {
return Response.status(Response.Status.CONFLICT).entity(exception.getMessage()).build();
} else if (exception instanceof InvalidTripException) {
return Response.status(Response.Status.CONFLICT).entity(exception.getMessage()).build();
} else if (exception instanceof DriverNotAvailableException) {
return Response.status(Response.Status.CONFLICT).entity(exception.getMessage()).build();
} else if (exception instanceof NotAllowedException) {
return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(exception.getMessage()).build();
}
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(exception.getMessage()).build();
}
}