com.poscoict.license.web.controller.ExceptionControllerAdvice.java Source code

Java tutorial

Introduction

Here is the source code for com.poscoict.license.web.controller.ExceptionControllerAdvice.java

Source

package com.poscoict.license.web.controller;

import java.io.IOException;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import com.poscoict.license.exception.UserException;

@ControllerAdvice
public class ExceptionControllerAdvice {
    private static final String DEFAULT_ERROR_VIEW = "error/error";

    private Logger logger = LoggerFactory.getLogger(getClass());

    public ModelAndView errView(Exception ex) {
        ex.printStackTrace();
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        //mv.addObject("message", ex.getMessage());
        mv.addObject("message", "?");

        if (ex.getClass().getSimpleName().equals("BadSqlGrammarException")) {
            mv.addObject("message", "?? ? .  ? .");
        } else if (ex.getClass().getSimpleName().equals("MySQLSyntaxErrorException")) {
            mv.addObject("message", "?? ? .  ? .");
        } else if (ex.getClass().getSimpleName().equals("NullPointerException")) {
            mv.addObject("message", "?? ?. ?? ? .");
        } else if (ex.getClass().getSimpleName().equals("DuplicateKeyException")) {
            mv.addObject("message", "?  ?. ?? ? .");
        } else if (ex.getClass().getSimpleName().equals("SQLException")) {
            mv.addObject("message", "?? ? .  ? .");
        } else if (ex.getClass().getSimpleName().equals("MySQLIntegrityConstraintViolationException")) {
            mv.addObject("message", "?  ?. ?? ? .");
        } else if (ex.getClass().getSimpleName().equals("DataIntegrityViolationException")) {
            mv.addObject("message",
                    "?  ?. ?? ? . ?  ? ? ? ?  .");
        } else if (ex.getClass().getSimpleName().equals("IOException")) {
            mv.addObject("message", "?");
        } else if (ex.getClass().getSimpleName().equals("Exception")) {
            mv.addObject("message", "ex.?");
        } else {
            mv.addObject("message", "?");
        }

        return mv;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception ex) {
        return errView(ex);
    }

    @ExceptionHandler(RuntimeException.class)
    public ModelAndView handleRuntimeException(RuntimeException ex) {
        return errView(ex);
    }

    @ExceptionHandler(IOException.class)
    public ModelAndView handleIOException(IOException ex) {
        return errView(ex);
    }

    @ExceptionHandler(MySQLSyntaxErrorException.class)
    public ModelAndView handleMySQLSyntaxErrorException(MySQLSyntaxErrorException ex) {
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message", "DB?");
        return mv;
    }

    @ExceptionHandler(NullPointerException.class)
    public ModelAndView handleNullPointerException(NullPointerException ex) {
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message", "?? ?. ?? ? .");
        return mv;
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ModelAndView handleDuplicateKeyException(DuplicateKeyException ex) {
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message", "?  ?. ?? ? .");
        return mv;
    }

    @ExceptionHandler(SQLException.class)
    public ModelAndView handleSQLException(SQLException ex) {
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message", "?? ? .  ? .");
        return mv;
    }

    @ExceptionHandler(MySQLIntegrityConstraintViolationException.class)
    public ModelAndView handleMySQLIntegrityConstraintViolationException(
            MySQLIntegrityConstraintViolationException ex) {
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message", "?  ?. ?? ? .");
        return mv;
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ModelAndView handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
        logger.error(ex.toString());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message",
                "?  ?. ?? ? . ?  ? ? ? ?  .");
        return mv;
    }

    @ExceptionHandler(UserException.class)
    public ModelAndView handleUserException(UserException ex) {
        logger.error(ex.getMessage());
        ModelAndView mv = new ModelAndView(DEFAULT_ERROR_VIEW);
        mv.addObject("name", ex.getClass().getSimpleName());
        mv.addObject("message", ex.getMessage());
        return mv;
    }
}