RestExceptionHandler.java

package gogreen.server.rest;

import gogreen.common.ApplicationException;
import gogreen.common.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class RestExceptionHandler {

    @ExceptionHandler(ApplicationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse applicationException (ApplicationException ex) {
        return new ErrorResponse(ex.getMessage());
    }
}