Skip to content

Commit

Permalink
JDF-77 Improve exceptions
Browse files Browse the repository at this point in the history
* Mark SeatAllocationException as @applicationexception and indicate the correct rollback strategy
* Add an ApplicationException subclass to WebApplicationException
* Both changes improve clarity as well and don't cause the container to log them as system exceptions
  • Loading branch information
mbogoevici committed Jul 26, 2012
1 parent b127e35 commit 001d997
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
@@ -1,11 +1,20 @@
package org.jboss.jdf.example.ticketmonster.model;

import javax.ejb.ApplicationException;

/**
* The exception thrown if an error occurs in seat allocation
* <p>
* The exception thrown if an error occurs in seat allocation.
* </p>
* <p>
* We mark it as {@link ApplicationException} because it is part of the application logic. Also,
* we want the container to roll back automatically when it is thrown.
* </p>
*
* @author Marius Bogoevici
*/
@SuppressWarnings("serial")
@ApplicationException(rollback = true)
public class SeatAllocationException extends RuntimeException {

public SeatAllocationException() {
Expand Down
Expand Up @@ -18,7 +18,6 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -210,14 +209,14 @@ public Response createBooking(BookingRequest bookingRequest) {
errors.put("errors", errorMessages);
// A WebApplicationException can wrap a response
// Throwing the exception causes an automatic rollback
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build());
throw new RestServiceException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build());
} catch (Exception e) {
// Finally, handle unexpected exceptions
Map<String, Object> errors = new HashMap<String, Object>();
errors.put("errors", Collections.singletonList(e.getMessage()));
// A WebApplicationException can wrap a response
// Throwing the exception causes an automatic rollback
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build());
throw new RestServiceException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build());
}
}

Expand Down
@@ -0,0 +1,51 @@
package org.jboss.jdf.example.ticketmonster.rest;

import javax.ejb.ApplicationException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

/**
* <p>
* This exception is thrown by RESTful services. As a subclass of {@link WebApplicationException},
* it is translated automatically into a {@link Response}.
* </p>
* <p>
* We mark it as {@link ApplicationException} because it is part of the application logic. Also,
* we want the container to roll back automatically when it is thrown.
* </p>
* @author Marius Bogoevici
*/
@ApplicationException(inherited = true, rollback = true)
public class RestServiceException extends WebApplicationException {

public RestServiceException() {
}

public RestServiceException(Response response) {
super(response);
}

public RestServiceException(int status) {
super(status);
}

public RestServiceException(Response.Status status) {
super(status);
}

public RestServiceException(Throwable cause) {
super(cause);
}

public RestServiceException(Throwable cause, Response response) {
super(cause, response);
}

public RestServiceException(Throwable cause, int status) {
super(cause, status);
}

public RestServiceException(Throwable cause, Response.Status status) {
super(cause, status);
}
}
Expand Up @@ -16,7 +16,6 @@
import javax.ejb.TimerService;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.jboss.jdf.example.ticketmonster.model.Performance;
Expand Down

0 comments on commit 001d997

Please sign in to comment.