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

org.minijax.mustache.MinijaxMustacheExceptionMapper Maven / Gradle / Ivy

There is a newer version: 0.5.10
Show newest version
package org.minijax.mustache;

import static javax.ws.rs.core.MediaType.*;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.minijax.util.ExceptionUtils;
import org.minijax.view.View;

@Provider
@Produces(TEXT_HTML)
public class MinijaxMustacheExceptionMapper implements ExceptionMapper {

    @Context
    private UriInfo uriInfo;

    @Override
    public Response toResponse(final Exception exception) {
        final WebApplicationException webAppException = ExceptionUtils.toWebAppException(exception);

        String message = webAppException.getMessage();
        if (message != null && message.startsWith("HTTP ")) {
            message = message.substring(5);
        }

        final int status = webAppException.getResponse().getStatus();
        final View view = new View("error");
        view.getModel().put("message", message);
        return Response.status(status).type(TEXT_HTML_TYPE).entity(view).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy