Skip to content

Instantly share code, notes, and snippets.

@deepanshumehtaa
Last active April 24, 2024 07:28
Show Gist options
  • Save deepanshumehtaa/0bcf87b9c7608283ca79e7e3b7ab268a to your computer and use it in GitHub Desktop.
Save deepanshumehtaa/0bcf87b9c7608283ca79e7e3b7ab268a to your computer and use it in GitHub Desktop.
Exception Handler Java
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");
@deepanshumehtaa
Copy link
Author

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@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);
}

}

@deepanshumehtaa
Copy link
Author

@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