net.jkratz.igdb.controller.advice.ErrorController.java Source code

Java tutorial

Introduction

Here is the source code for net.jkratz.igdb.controller.advice.ErrorController.java

Source

/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package net.jkratz.igdb.controller.advice;

import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

@ControllerAdvice
public class ErrorController {

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

    @RequestMapping(produces = "application/json")
    @ExceptionHandler({ MissingServletRequestParameterException.class,
            UnsatisfiedServletRequestParameterException.class, ServletRequestBindingException.class })
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public @ResponseBody Map<String, Object> handleRequestException(Exception ex) {
        logger.warn(ex.getMessage());
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Request Error");
        map.put("message", ex.getMessage());
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED)
    public @ResponseBody Map<String, Object> handleRequestMethodNotSupportedException(
            HttpMediaTypeNotAcceptableException ex) {
        logger.warn(ex.getMessage());
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Request Error");
        map.put("message", ex.getMessage());
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public @ResponseBody Map<String, Object> handleValidationException(ConstraintViolationException ex)
            throws IOException {
        logger.warn(ex.getMessage());
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Validation Failure");
        map.put("violations", convertConstraintViolation(ex.getConstraintViolations()));
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public @ResponseBody Map<String, Object> handleValidationException(MethodArgumentNotValidException ex)
            throws IOException {
        logger.warn(ex.getMessage());
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Validation Failure");
        map.put("violations", convertConstraintViolation(ex));
        map.put("target", ex.getBindingResult().getTarget());
        map.put("expectedType", ex.getBindingResult().getObjectName());
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(ObjectRetrievalFailureException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public @ResponseBody Map<String, Object> handleObjectRetrievalFailureException(
            ObjectRetrievalFailureException ex) throws IOException {
        logger.error("Object not found", ex);
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Entity Not Found");
        map.put("message", ex.getMessage());
        if (ex.getRootCause() != null) {
            map.put("cause", ex.getRootCause().getMessage());
        }
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(DataIntegrityViolationException.class)
    @ResponseStatus(value = HttpStatus.CONFLICT)
    public @ResponseBody Map<String, Object> handleDataIntegrityViolationException(
            DataIntegrityViolationException ex) throws IOException {
        logger.error("Data Integrity Error", ex);
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Data Integrity Error");
        map.put("message", ex.getMessage());
        if (ex.getRootCause() != null) {
            map.put("cause", ex.getRootCause().getMessage());
        }
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(DataAccessException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public @ResponseBody Map<String, Object> handleDataAccessException(DataAccessException ex) throws IOException {
        logger.error("Database Error", ex);
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Data Error");
        map.put("message", ex.getMessage());
        if (ex.getRootCause() != null) {
            map.put("cause", ex.getRootCause().getMessage());
        }
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    public @ResponseBody Map<String, Object> handleUnsupportedMediaTypeException(
            HttpMediaTypeNotSupportedException ex) throws IOException {
        logger.warn(ex.getMessage());
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Unsupported Media Type");
        map.put("message", ex.getMessage());
        map.put("supported", ex.getSupportedMediaTypes());
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    @ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
    public @ResponseBody Map<String, Object> handleMediaTypeNotAcceptableException(
            HttpMediaTypeNotAcceptableException ex) {
        logger.warn(ex.getMessage());
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Unsupported Media Type");
        map.put("message", ex.getMessage());
        return map;
    }

    @RequestMapping(produces = "application/json")
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public @ResponseBody Map<String, Object> handleUncaughtException(Exception ex) {
        logger.error("Unhandled Exception", ex);
        Map<String, Object> map = Maps.newHashMap();
        map.put("error", "Generic Error");
        map.put("message", ex.getMessage());
        if (ex.getCause() != null) {
            map.put("cause", ex.getCause().getMessage());
        }
        return map;
    }

    private Map<String, Map<String, Object>> convertConstraintViolation(
            Set<ConstraintViolation<?>> constraintViolations) {
        Map<String, Map<String, Object>> result = Maps.newHashMap();
        for (ConstraintViolation constraintViolation : constraintViolations) {
            Map<String, Object> violationMap = Maps.newHashMap();
            violationMap.put("value", constraintViolation.getInvalidValue());
            violationMap.put("type", constraintViolation.getRootBeanClass());
            violationMap.put("message", constraintViolation.getMessage());
            result.put(constraintViolation.getPropertyPath().toString(), violationMap);
        }
        return result;
    }

    private Map<String, Object> convertConstraintViolation(MethodArgumentNotValidException ex) {
        Map<String, Object> result = Maps.newHashMap();
        for (FieldError error : ex.getBindingResult().getFieldErrors()) {
            result.put(error.getField(), error.getDefaultMessage());
        }
        return result;
    }
}