class GenericExceptionMapper implements ExceptionMapper<Throwable> {

    @Override
    public Response toResponse(Throwable exception) {
        final VitamError vitamError = new VitamError(VitamCodeHelper.getCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR));
        String description = exception.getMessage();
        if (Strings.isNullOrEmpty(description)) {
            description = StringUtils.getClassName(exception);
        } else {
            description = description.replaceAll(URI_HOST_PORT_PATTERN, "");
        }
        vitamError.setContext(ServerIdentity.getInstance().getJsonIdentity())
            .setMessage(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getMessage())
            .setDescription(description)
            .setState(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.name())
            .setHttpCode(VitamCode.GLOBAL_INTERNAL_SERVER_ERROR.getStatus().getStatusCode());
        if (exception instanceof BadRequestException || exception instanceof JsonMappingException) {
            vitamError.setMessage(description).setHttpCode(Status.BAD_REQUEST.getStatusCode());
        } else if (exception instanceof ForbiddenException) {
            vitamError.setMessage(description).setHttpCode(Status.FORBIDDEN.getStatusCode());
        } else if (exception instanceof NotAcceptableException) {
            vitamError.setMessage(description).setHttpCode(Status.NOT_ACCEPTABLE.getStatusCode());
        } else if (exception instanceof NotAllowedException) {
            vitamError.setMessage(description).setHttpCode(Status.METHOD_NOT_ALLOWED.getStatusCode());
        } else if (exception instanceof NotAuthorizedException) {
            vitamError.setMessage(description).setHttpCode(Status.UNAUTHORIZED.getStatusCode());
        } else if (exception instanceof NotFoundException) {
            vitamError.setMessage(description).setHttpCode(Status.NOT_FOUND.getStatusCode());
        } else if (exception instanceof NotSupportedException) {
            vitamError.setMessage(description).setHttpCode(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode());
        } else if (exception instanceof RedirectionException) {
            vitamError.setMessage(description).setHttpCode(Status.SEE_OTHER.getStatusCode());
        } else if (exception instanceof ServiceUnavailableException) {
            vitamError.setMessage(description).setHttpCode(Status.SERVICE_UNAVAILABLE.getStatusCode());
        }
        LOGGER.error(vitamError.toString(), exception);
        return Response.status(vitamError.getHttpCode()).entity(vitamError).type(MediaType.APPLICATION_JSON_TYPE)
            .build();
    }

}