Search in sources :

Example 1 with APIErrorCode

use of com.dexels.navajo.article.APIErrorCode in project navajo by Dexels.

the class ExceptionFilter method doFilter.

@Override
public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain) {
    HttpServletResponse resp = (HttpServletResponse) rs;
    try {
        if (!filterCondition((HttpServletRequest) rq)) {
            chain.doFilter(rq, rs);
            return;
        }
        ErrorCatchHttpResponseWrapper wrappedResponse = new ErrorCatchHttpResponseWrapper(resp);
        chain.doFilter(rq, wrappedResponse);
        resp.setStatus(200);
        resp.getWriter().write(wrappedResponse.getBufferContent());
    } catch (IOException | ServletException e) {
        logger.error("Filter has failed {}", e);
        // We should not go in here. All exceptions should be handled in the servlet. As
        // a backup we report a server error back to the user.
        APIErrorCode internal = APIErrorCode.InternalError;
        try {
            resp.sendError(internal.getHttpStatusCode(), internal.getDescription());
        } catch (Throwable e1) {
            // We've failed to return the error to the user. We cannot do anything anymore.
            logger.error("Failed to deliver error {}", e1);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) APIErrorCode(com.dexels.navajo.article.APIErrorCode) IOException(java.io.IOException)

Example 2 with APIErrorCode

use of com.dexels.navajo.article.APIErrorCode in project navajo by Dexels.

the class ArticleBaseServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.setContentType("application/json; charset=utf-8");
        doServiceImpl(request, response);
    } catch (Throwable t1) {
        APIException exception = (t1 instanceof APIException) ? (APIException) t1 : new APIException(t1.getMessage(), t1, APIErrorCode.InternalError);
        // If we get an internal error, we want to know about it in our logging system.
        if (exception.getErrorCode().getHttpStatusCode() >= 500) {
            logger.error("Error {}", t1);
        }
        try {
            writeJSONErrorResponse(exception, response);
        } catch (Throwable t2) {
            logger.error("Failed to write JSON error response", t2);
            try {
                APIErrorCode internal = APIErrorCode.InternalError;
                response.sendError(internal.getHttpStatusCode(), internal.getDescription());
            } catch (Throwable t3) {
                // We've failed to return the error to the user. We cannot do anything anymore.
                logger.error("Failed to deliver error {}", t3);
            }
        }
    }
}
Also used : APIException(com.dexels.navajo.article.APIException) APIErrorCode(com.dexels.navajo.article.APIErrorCode)

Aggregations

APIErrorCode (com.dexels.navajo.article.APIErrorCode)2 APIException (com.dexels.navajo.article.APIException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1