CustomHandleException

PHOTO EMBED

Mon Sep 25 2023 03:15:48 GMT+0000 (Coordinated Universal Time)

Saved by @namnt

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());
    }
}
content_copyCOPY