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

net.aequologica.neo.serioulizer.jaxrs.GenericExceptionMapperWithErrorEntity Maven / Gradle / Ivy

The newest version!
package net.aequologica.neo.serioulizer.jaxrs;

import static net.aequologica.neo.serioulizer.jaxrs.GenericExceptionMapperWithErrorEntity.Error.error;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonProperty;

@Provider
public class GenericExceptionMapperWithErrorEntity implements ExceptionMapper {

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

    @Override
    public Response toResponse(Exception e) {

        log.error("Exception caught by jax-rs exception mapper", e.getMessage());

        Error error = error(e.getClass().getSimpleName() + ": " +e.getMessage());
        
        return Response.status(Status.INTERNAL_SERVER_ERROR)
                .entity(error)
                .type(MediaType.APPLICATION_JSON)
                .build();
    }

    static public class Error {
        @JsonProperty
        final String error;

        static public Error error(final String error) {
            return new Error(error);
        }

        public Error(String error) {
            this.error = error;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy