Handling excpetion in Spring web application and returing them to the client meaningfully is important part of the application design.

First lets see how the error controller in springboot can be customize.

Error Controller

Whenever an error happen, spring directs to /error. The default configuration is vanila. To spice thing a bit, implements the org.springframework.boot.autoconfigure.web.ErrorController. Now instead of returing valina error message, more detail message is recieved by the client.



Spring ControllerAdvice

Spring ControllerAdvice takes a level more and makes the error handling more easy. From the application exception are throws up at any particular point. ControllerAdvice is the single point of entry to handle all exception and let you define how to hanndle for each or group of exception.

Define an exception class extending the RuntimeException

public class InvalidOUIException extends RuntimeException {

    String oui;

    @Override
    public String getMessage() {
            return "OUI " + this.oui + " id invalid";
    }

    public InvalidOUIException(String oui) {
            this.oui = oui;
    }

    //getter and setter
}

Create an ErrorMessage Object.

public class ErrorMessage {
	String timestamp;
	private int status;
	private String error;
	private String exception;
	private String message;
	private String path;

	public ErrorMessage(HttpStatus error, String exception, String message, String path) {
		this.timestamp = DateTime.now().toString(DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss yyyy"));
		this.status = error.value();
		this.error = error.getReasonPhrase();
		this.exception = exception;
		this.message = message;
		this.path = path;
	}

	//getter and setter
}

Handle all the exception via ControllerAdvice.



Throw the exception from where relevant and ControllerAdvice will handle it.

@RestController
class MyController {
	public void doSomething() {
		if(valueNull()) {
			throw new InvalidOUIException("AA-BB-CC");
		}
	}
}

For the full project reference visit redis-device-oui