6 changed files with 69 additions and 47 deletions
@ -0,0 +1,16 @@ |
|||
package com.example.apispringgradleb2boost.exceptionhandling; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class CustomError { |
|||
|
|||
private int code; |
|||
private String message; |
|||
|
|||
public CustomError(int code, String message) { |
|||
this.code = code; |
|||
this.message = message; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,39 @@ |
|||
package com.example.apispringgradleb2boost.exceptionhandling; |
|||
|
|||
import org.springframework.http.HttpHeaders; |
|||
import org.springframework.http.HttpStatus; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.web.bind.MethodArgumentNotValidException; |
|||
import org.springframework.web.bind.annotation.ControllerAdvice; |
|||
import org.springframework.web.bind.annotation.ExceptionHandler; |
|||
import org.springframework.web.context.request.WebRequest; |
|||
import org.springframework.web.servlet.NoHandlerFoundException; |
|||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
|||
|
|||
@ControllerAdvice |
|||
public class GeneralExceptionHandler extends ResponseEntityExceptionHandler { |
|||
|
|||
@ExceptionHandler({Exception.class}) |
|||
public ResponseEntity<Object> handleAll(Exception ex, WebRequest request) { |
|||
CustomError customError = new CustomError(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getLocalizedMessage()); |
|||
return new ResponseEntity<Object>(customError, new HttpHeaders(), customError.getCode()); |
|||
} |
|||
|
|||
@Override |
|||
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, |
|||
HttpHeaders headers, |
|||
HttpStatus status, |
|||
WebRequest request) { |
|||
|
|||
CustomError customError = new CustomError(HttpStatus.BAD_REQUEST.value(), ex.getLocalizedMessage()); |
|||
return handleExceptionInternal(ex, customError, headers, HttpStatus.valueOf(customError.getCode()), request); |
|||
} |
|||
|
|||
@Override |
|||
protected ResponseEntity<Object> handleNoHandlerFoundException( |
|||
NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { |
|||
|
|||
CustomError customError = new CustomError(HttpStatus.NOT_FOUND.value(), ex.getLocalizedMessage()); |
|||
return new ResponseEntity<Object>(customError, new HttpHeaders(), customError.getCode()); |
|||
} |
|||
} |
@ -1,34 +0,0 @@ |
|||
package com.example.apispringgradleb2boost.exceptions.resolvers; |
|||
|
|||
import org.springframework.web.bind.annotation.ExceptionHandler; |
|||
import org.springframework.web.bind.annotation.RestControllerAdvice; |
|||
import org.springframework.web.bind.MissingPathVariableException; |
|||
import org.springframework.web.servlet.NoHandlerFoundException; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import java.util.HashMap; |
|||
|
|||
@RestControllerAdvice |
|||
public class ExceptionResolver { |
|||
|
|||
@ExceptionHandler(Exception.class) |
|||
public HashMap<String, String> handleException(HttpServletRequest request, Exception e) { |
|||
HashMap<String, String> response = new HashMap<>(); |
|||
response.put("message", e.getMessage()); |
|||
return response; |
|||
} |
|||
|
|||
@ExceptionHandler(MissingPathVariableException.class) |
|||
public HashMap<String, String> handleMissingPathVariableException(HttpServletRequest request, MissingPathVariableException e) { |
|||
HashMap<String, String> response = new HashMap<>(); |
|||
response.put("message", "Required path variable is missing in this request. Please add it to your request."); |
|||
return response; |
|||
} |
|||
|
|||
@ExceptionHandler(NoHandlerFoundException.class) |
|||
public HashMap<String, String> handleNotFoundResourceException(HttpServletRequest request, NoHandlerFoundException e) { |
|||
HashMap<String, String> response = new HashMap<>(); |
|||
response.put("message", "Requested resource wasn't found on the server"); |
|||
return response; |
|||
} |
|||
} |
Loading…
Reference in new issue