That's give me and idea, we can use the ExceptionHandler and Build the response Status using a Controller exception, it's create using JAX-RS status or whatever your want.
@Provider
public class ControllerExceptionHandler implements ExceptionMapper<ControllerException>
{
@Override
public Response toResponse(ControllerException exception)
{
return ControllerException.toResponse(exception);
}
}
For you can catch the exception you must be throw first and before you need created, something like that:
@POST
@Path("/{storeCode}/schedules")
public HolidaySchedule createHolidaySchedule(@PathParam("storeCode") String storeCode, @QueryParam("day") String day) throws ControllerException {
try {
HolidaySchedule response = new HolidaySchedule();
return response;
} catch (Exception e) {
ErrorMessage msg = ErrorMessage.from("Ups not work");
throw new ControllerException(Status.BAD_REQUEST, msg);
}
}
The ControllerException has a two static functions which create a JAX-RS Response or a Builder for other modifications like content-type or something more.
public class ControllerException extends Exception {
private static final long serialVersionUID = 1L;
private Status status;
private Object body;
public ControllerException(Status status, Object body) {
this.status = status;
this.body = body;
}
public ControllerException(Status status) {
this(status, null);
}
public static Response toResponse(ControllerException exception) {
return ControllerException.toResponseBuilder(exception).build();
}
public static ResponseBuilder toResponseBuilder(ControllerException exception) {
return Response.status(exception.status).entity(exception);
}
}