@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(CustomException.class)
public ResponseEntity<ApiResponse> handleCustomException(CustomException e) {
return ResponseEntity
.status(e.getErrorCode().getStatus())
.body(new ApiResponse("invalid_request", false, null));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse> handleException(Exception e) {
String errorLog = getStackTraceAsString(e);
log.error("[ERROR] : {}\n{}", e.getMessage(), errorLog);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ApiResponse("internal_server_error", false,null));
}
private String getStackTraceAsString(Throwable e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
}
Leave a comment