All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zandero.rest.RestExceptionMapper Maven / Gradle / Ivy

The newest version!
package com.zandero.rest;

import com.google.inject.ProvisionException;
import org.apache.http.HttpStatus;
import org.jboss.resteasy.spi.Failure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class RestExceptionMapper implements ExceptionMapper {

	private static final Logger log = LoggerFactory.getLogger(RestExceptionMapper.class.getName());

	@Override
	public Response toResponse(Throwable exception) {

		try {
			if (exception == null) { // should not happen ...
				throw new IllegalArgumentException("Missing exception!");
			}
			// to avoid ugly casting, just throw exception and catch typed exception below
			throw exception;
		}
		catch (RestException e) {
			log.error("RestException error: ", e);
			return e.getResponse();
		}
		catch (ProvisionException e) {
			log.error("ProvisionException error: ", e);
			// catch wrapped GUICE errors unwrap and call again or create response as found
			return e.getCause() != null ? toResponse(e.getCause()) : getResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e);
		}
		catch (WebApplicationException e) {
			log.error("Web application excpetion: ", e);
			return getResponse(e.getResponse().getStatus(), e);
		}
		catch (Failure e) {
			log.error("Failure: ", e);
			return getResponse(e.getErrorCode(), e);
		}
		catch (IllegalArgumentException e) {
			log.error("Missing or invalid parameters: ", e);
			return getResponse(HttpStatus.SC_NOT_ACCEPTABLE, e);
		}
		catch (Throwable e) {
			log.error("Application error: ", e);
			// other exceptions...
			return getResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e);
		}
	}

	private Response getResponse(int status, Throwable e) {

		return Response.status(status)
			.entity(new RestException(e, status))
			.type(MediaType.APPLICATION_JSON)
			.build();
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy