package com.example.DemoElasticsearch.Exception; 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 CustomHandleException { @ExceptionHandler(SeverError.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ErrorMessage internalServerError(Exception exception){ return new ErrorMessage("500",exception.getMessage()); } @ExceptionHandler(BadRequest.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorMessage badRequest(Exception exception) { return new ErrorMessage("400", exception.getMessage()); } @ExceptionHandler(NotFound.class) @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorMessage notfound(Exception exception) { return new ErrorMessage("404", exception.getMessage()); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter