9

I'm trying to write error handler in Spring-Boot for my controllers that would catch most possible errors (Spring, sql etc.). So far I'm able to get JSON response with Nulls however i'm unable to put any data inside. When I try to get error message in I just receive a blank page.

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

@RestController
public class BasicErrorController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    @RequestMapping(value=ERROR_PATH)
    @ExceptionHandler(value = {NoSuchRequestHandlingMethodException.class, SQLException.class, IOException.class, RuntimeException.class, Exception.class})
    public ErrorBody defaultErrorHandler(HttpServletRequest request, Exception e) {     
        ErrorBody eBody = new ErrorBody();         
        eBody.setMessage(e.getCause().getMessage());
        return eBody;
    }
}
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ErrorBody {
    private String dateTime;
    private String exception;
    private String url;
    private String message;
}
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Groth
  • 123
  • 1
  • 1
  • 10
  • Not sure what you mean by "unable to put any data inside" - what did you do and what did you see as a result? You don't need the `@ExceptionHandler` annotation in an `ErrorController` (but you do need to implement the interface, and yours appears not to). Look at the way the `BasicErrorController` is implemented in Spring Boot for clues. – Dave Syer Feb 17 '15 at 09:39
  • It's probably better to implement `ErrorAttributes` (not `ErrorController`), but it's your choice if you want full control. – Dave Syer Feb 17 '15 at 09:40
  • Thanks for answers. By "unable to put any data inside" I meant that whenever i tried to to get any data from error JSON wasn't displayed. I solved it today i was able to get to data about errors and send them as json properly by using "HttpServletRequest request" and reading information from request. – Groth Feb 18 '15 at 10:55

3 Answers3

7

Yo can do something like this:

 @ControllerAdvice
   public class ControllerExceptionTranslator {


    @ExceptionHandler(EntityNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    SimpleErrorMessage handleException(EntityNotFoundException exception){
        log.debug("Entity Not Found Exception {}",exception.getMessage());
        log.trace(exception.getMessage(),exception);
        return new SimpleErrorMessage("Entity not found","This resource was not found");
    }


    @ExceptionHandler({UsernameNotFoundException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    SimpleErrorMessage handleException(UsernameNotFoundException exception){
        log.debug("Username not found {}",exception.getLocalizedMessage());
        log.trace(exception.getMessage(),exception);
        return new SimpleErrorMessage("Unaouthorized"," ");
    }


}
carlos_technogi
  • 198
  • 3
  • 6
1

I was able to get to data about errors and send them as json properly by using "HttpServletRequest request" and reading information from request.

@RequestMapping(value = ERROR_PATH)
    public ErrorBody defaultErrorHandler(HttpServletRequest request) {....}
Groth
  • 123
  • 1
  • 1
  • 10
0

Here this is an example of @ExceptionHandler(Exception.class)

https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

You can use @ControllerAdvice

package demo.controller;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

@ControllerAdvice
public class ExceptionControllerAdvice {

 @InitBinder
 public void initBinder(WebDataBinder binder) {
    System.out.println("controller advice: init binder");
}


@ExceptionHandler(Exception.class)
public String exception(Exception e) {
    System.out.println("controller advice: exception Handler");
    System.out.println(e.getMessage());
    return "error";
}

@ModelAttribute
public void modelAttribute(){
    System.out.println("controller advice:model Attribute");
}
}
Mari Murotani
  • 723
  • 1
  • 8
  • 19