business.GlobalExceptionHandlerController.java Source code

Java tutorial

Introduction

Here is the source code for business.GlobalExceptionHandlerController.java

Source

/**
 * Copyright (C) 2016  Stichting PALGA
 * This file is distributed under the GNU Affero General Public License
 * (see accompanying file <a href="{@docRoot}/LICENSE">LICENSE</a>).
 */
package business;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import business.exceptions.ExcerptListUploadError;

@ControllerAdvice
public class GlobalExceptionHandlerController {

    Log log = LogFactory.getLog(getClass());

    @ExceptionHandler(ExcerptListUploadError.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResponseEntity<String> handleExcerptListUploadError(ExcerptListUploadError e) {
        log.error("ExcerptListUploadError: " + e.getMessage());
        return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
    }
}