Last active
April 24, 2024 07:28
-
-
Save deepanshumehtaa/0bcf87b9c7608283ca79e7e3b7ab268a to your computer and use it in GitHub Desktop.
Exception Handler Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 steps: | |
1. create custom errors and sxtend from `RuntimeException` | |
2. create a global Exception Handler | |
3. directly throw the exception from anywhere | |
................................................................................................................. | |
@ResponseStatus(HttpStatus.BAD_REQUEST) | |
public class BadReq extends RuntimeException { | |
public final HttpStatus hCode = HttpStatus.BAD_REQUEST; | |
public BadReq() { | |
super("BAD_REQUEST !"); | |
} | |
public BadReq(String msg) { | |
super(msg); | |
} | |
} | |
................................................................................................................. | |
@ControllerAdvice | |
public class GlobalExceptionHandler { | |
@ExceptionHandler(BadReq.class) | |
public ResponseEntity<Object> handleBadReqException(BadReq ex) { | |
Map<String, Object> res = new HashMap<>(){{ | |
put("status_code", ex.hCode.value()); | |
put("error", ex.getMessage()); | |
}}; | |
return ResponseEntity.status(ex.hCode).body(res); | |
} | |
// @ExceptionHandler(Exception.class) | |
// public ResponseEntity<Object> handleResourceNotFoundException(Exception ex) { | |
// Map<String, Object> res = new HashMap<>(){{ | |
// put("status_code", HttpStatus.INTERNAL_SERVER_ERROR.value()); | |
// put("error", ex.getMessage()); | |
// }}; | |
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(res); | |
// } | |
@Getter | |
public static class ErrorResponse { | |
private final String message; | |
public ErrorResponse(String message) { | |
this.message = message; | |
} | |
} | |
} | |
............................................................................................... | |
throw new BadReq("`id` is missing from request"); |
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ServerError.class)
public ResponseEntity<Object> handleServerError(ServerError ex) {
Map<String, Object> res = new HashMap<>(){{
put("status_code", ex.hCode.value());
put("error", ex.getMessage());
}};
return ResponseEntity.status(ex.hCode).body(res);
}
@ExceptionHandler(BadReq.class)
public ResponseEntity<Object> handleBadReqException(BadReq ex) {
Map<String, Object> res = new HashMap<>(){{
put("status_code", ex.hCode.value());
put("error", ex.getMessage());
}};
return ResponseEntity.status(ex.hCode).body(res);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleResourceNotFoundException(Exception ex) {
Map<String, Object> res = new HashMap<>(){{
put("status_code", HttpStatus.INTERNAL_SERVER_ERROR.value());
put("error", ex.getMessage());
}};
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(res);
}
@Getter
public static class ErrorResponse {
private final String message;
public ErrorResponse(String message) {
this.message = message;
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadReq extends RuntimeException {
}