users@jersey.java.net

Re: ExceptionMapper not handling Exception subclasses

From: mathewch <mathewch_at_gmail.com>
Date: Tue, 29 Jun 2010 09:13:13 -0700 (PDT)

Hi Gregg,

I too have an ExceptionMapping framework for my service implementation. But
never had to declare the resource method with the *Exception signature. To
avoid that use the type RuntimeException. Also make sure the providers are
getting registered. For your reference, I have my exceptions handled in the
following way:

1). Base exception class:
public class ServiceException extends RuntimeException {

    private static final long serialVersionUID = -339330063981442247L;
    final protected int responseStatusCode;
    protected FaultInfo faultInfo = null;

    public ServiceException(String message, int responseStatusCode) {
        super(message);
        this.responseStatusCode = responseStatusCode;
    }

    public ServiceException(String message, int responseStatusCode,
FaultInfo faultInfo) {
        this(message, responseStatusCode);
        this.faultInfo = faultInfo;
    }

    public ServiceException(int responseStatusCode, FaultInfo faultInfo) {
        this(faultInfo.getReason(), responseStatusCode);
        this.faultInfo = faultInfo;
    }

    public int getResponseStatusCode() {
        return responseStatusCode;
    }

    public FaultInfo getFaultInfo() {
        return faultInfo;
    }
}

2). An exception type:
public class NotFoundException extends ServiceException {
    private static final long serialVersionUID = -3357251481427760852L;
    public static final int RESPONSE_STATUS_CODE =
HttpURLConnection.HTTP_NOT_FOUND;

    public NotFoundException(String message) {
        super(message, RESPONSE_STATUS_CODE);
    }

    public NotFoundException(String message, FaultInfo faultInfo) {
        super(message, RESPONSE_STATUS_CODE, faultInfo);
    }

    public NotFoundException(FaultInfo faultInfo) {
        super(RESPONSE_STATUS_CODE, faultInfo);
    }
}


3). A fault object to hold the full exception details
/**
 *
 * A JAXB type representing an exception for a service request.
 *
 *
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "errorCode",
    "reason",
    "resource",
    "exception"
})
@XmlRootElement(name = "FaultInfo")
public class FaultInfo
    implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElement(namespace = "..........")
    protected Integer errorCode;
    @XmlElement(namespace = "...............")
    protected String reason;
    @XmlElement(namespace = "...................")
    protected String resource;
    @XmlElement(namespace = "............")
    protected String exception;

    /**
     * Gets the value of the errorCode property.
     *
     * @return
     * possible object is
     * {_at_link Integer }
     *
     */
    public Integer getErrorCode() {
        return errorCode;
    }

    /**
     * Sets the value of the errorCode property.
     *
     * @param value
     * allowed object is
     * {_at_link Integer }
     *
     */
    public void setErrorCode(Integer value) {
        this.errorCode = value;
    }

    /**
     * Gets the value of the reason property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public String getReason() {
        return reason;
    }

    /**
     * Sets the value of the reason property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setReason(String value) {
        this.reason = value;
    }

    /**
     * Gets the value of the resource property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public String getResource() {
        return resource;
    }

    /**
     * Sets the value of the resource property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setResource(String value) {
        this.resource = value;
    }

    /**
     * Gets the value of the exception property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public String getException() {
        return exception;
    }

    /**
     * Sets the value of the exception property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setException(String value) {
        this.exception = value;
    }

}

4). The provider for the NotFoundException:
@Provider
public class NotFoundExceptionMapper implements
ExceptionMapper<NotFoundException> {

    public Response toResponse(NotFoundException ex) {
        return
Response.status(ex.getResponseStatusCode()).entity(ex.getFaultInfo()).type(MediaType.APPLICATION_XML).build();
    }
}

5). The exception is thrown in the service as follows:
FaultInfo fault = new FaultInfo();
fault.setErrorCode(ServiceErrorCode.NO_RECORDS_FOUND);
fault.setReason("Couldn't find any receipts in the search");
throw new NotFoundException(fault);

This works as expected but don't know it can be solution to your problem.

Thanks,
Mathew
-- 
View this message in context: http://jersey.576304.n2.nabble.com/ExceptionMapper-not-handling-Exception-subclasses-tp5235614p5235654.html
Sent from the Jersey mailing list archive at Nabble.com.