com.miserablemind.butter.apps.butterApp.controller.ControllerExceptionHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.miserablemind.butter.apps.butterApp.controller.ControllerExceptionHandler.java

Source

package com.miserablemind.butter.apps.butterApp.controller;

import com.miserablemind.butter.apps.butterApp.ConfigButterApp;
import com.miserablemind.butter.apps.butterApp.exception.HTTPBadRequestException;
import com.miserablemind.butter.apps.butterApp.exception.HTTPInternalServerErrorException;
import com.miserablemind.butter.apps.butterApp.exception.HTTPNotFoundException;
import com.miserablemind.butter.helpers.Utilities;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

/*
 * Miserable Mind
 * http://www.butter.miserablemind.com
 * The MIT License (MIT)
 */

/**
 * Maps exceptions to HTTP statuses and render status pages. It also logs exception backtrace.
 * <p>Note: every method injects config to model manually as postHandle (that is responsible for that) does not get
 * invoked because of controller throwing exception.</p>
 *
 * @author <a href="http://www.miserablemind.com" target="_blank">miserablemind</a>
 */

@ControllerAdvice
public class ControllerExceptionHandler {

    @Autowired
    private ConfigButterApp config;

    private static final Logger logger = Logger.getLogger(ControllerExceptionHandler.class);

    /**
     * Handles "Not Found" scenario and renders a 404 page.
     *
     * @param request   http request used for getting the URL and wiring config into model
     * @param exception exception that was thrown, in this case {@link com.miserablemind.butter.apps.butterApp.exception.HTTPNotFoundException}
     * @return logical name of a view to render
     */
    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    @ExceptionHandler(HTTPNotFoundException.class)
    public String handleNotFound(HttpServletRequest request, Exception exception) {
        logger.error("[404] Request: " + request.getRequestURL() + " raised " + exception, exception);
        request.setAttribute("configApp", this.config);
        if (Utilities.getAuthUserId() == 0)
            return "guest/errors/404";
        return "errors/404";
    }

    /**
     * Handles "Bad Request" scenario and renders a 400 page.
     *
     * @param request   http request used for getting the URL and wiring config into model
     * @param exception exception that was thrown, in this case {@link com.miserablemind.butter.apps.butterApp.exception.HTTPBadRequestException}
     * @return logical name of a view to render
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST) // 400
    @ExceptionHandler(HTTPBadRequestException.class)
    public String handleBadRequest(HttpServletRequest request, Exception exception) {
        logger.error("[400] Request: " + request.getRequestURL() + " raised " + exception, exception);
        request.setAttribute("configApp", this.config);
        if (Utilities.getAuthUserId() == 0)
            return "guest/errors/400";
        return "errors/400";
    }

    /**
     * Handles "Internal Server Error" scenario and renders a 500 page.
     *
     * @param request   http request used for getting the URL and wiring config into model
     * @param exception exception that was thrown, in this case {@link com.miserablemind.butter.apps.butterApp.exception.HTTPBadRequestException} or {@link DataAccessException}
     * @return logical name of a view to render
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500
    @ExceptionHandler({ DataAccessException.class, HTTPInternalServerErrorException.class })
    public String handleInternalServerError(HttpServletRequest request, Exception exception) {
        logger.error("[500] Request: " + request.getRequestURL() + " raised " + exception, exception);
        request.setAttribute("configApp", this.config);
        if (Utilities.getAuthUserId() == 0)
            return "guest/errors/500";
        return "errors/500";
    }

}