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

io.muserver.rest.CustomExceptionMapper Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package io.muserver.rest;

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

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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

    private final Map, ExceptionMapper> mappers;

    CustomExceptionMapper(Map, ExceptionMapper> mappers) {
        this.mappers = new HashMap<>(mappers);
    }

    @SuppressWarnings("unchecked")
    Response toResponse(Throwable ex) {

        Class exClass = ex.getClass();

        int maxDepth = Integer.MAX_VALUE;
        ExceptionMapper exceptionMapper = findBestMatchingExceptionMapper(exClass, maxDepth);

        if (exceptionMapper == null) {
            return null;
        }

        try {
            Response response = exceptionMapper.toResponse(ex);
            if (response == null) {
                response = Response.noContent().build();
            }
            return response;
        } catch (Exception e) {
            String errorID = UUID.randomUUID().toString();
            log.error("Error thrown from exception mapper " + exceptionMapper + " so returning error to client with ErrorID=" + errorID, e);
            return Response.serverError()
                .type(MediaType.TEXT_HTML_TYPE)
                .entity("

500 Internal Server Error

ErrorID=" + errorID + "

") .build(); } } @SuppressWarnings("unchecked") private ExceptionMapper findBestMatchingExceptionMapper(Class exClass, int maxDepth) { ExceptionMapper exceptionMapper = null; for (Map.Entry, ExceptionMapper> entry : mappers.entrySet()) { Class mapperClass = entry.getKey(); if (mapperClass.isAssignableFrom(exClass)) { int depth = 0; Class yo = exClass; while (yo != null) { if (mapperClass.equals(yo)) { if (depth < maxDepth) { maxDepth = depth; exceptionMapper = entry.getValue(); } break; } depth++; yo = yo.getSuperclass(); } } } return exceptionMapper; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy