001package de.deepamehta.webservice.provider;
002
003import de.deepamehta.core.util.UniversalExceptionMapper;
004
005import javax.servlet.http.HttpServletRequest;
006
007import javax.ws.rs.core.Context;
008import javax.ws.rs.core.Response;
009import javax.ws.rs.ext.ExceptionMapper;
010import javax.ws.rs.ext.Provider;
011
012
013
014/**
015 * This mapper maps <i>all</i> Throwables to a suitable response.
016 * <p>
017 * We don't want Jersey to re-throw anything to the HTTP container as this would result in logging
018 * the exception twice and possibly in interspersed illegible stack traces (see #484).
019 * <p>
020 * 2 additional aspects are handled:
021 *   - Logging the exception.
022 *   - Enriching the response with an error entity.
023 */
024@Provider
025public class CatchAllExceptionMapper implements ExceptionMapper<Throwable> {
026
027    // ---------------------------------------------------------------------------------------------- Instance Variables
028
029    @Context
030    HttpServletRequest request;
031
032    // -------------------------------------------------------------------------------------------------- Public Methods
033
034    @Override
035    public Response toResponse(Throwable e) {
036        return new UniversalExceptionMapper(e, request).toResponse();
037    }
038}