Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Exception Handler #737

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.ac.ebi.spot.ols.controller.api.exception;

public class BadRequestException extends RuntimeException {
public BadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package uk.ac.ebi.spot.ols.controller.api.exception;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class CustomErrorController implements ErrorController {

@RequestMapping(value = "/error", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<ErrorResponse> handleError(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");

if (statusCode == null) {
statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
}

String errorMessage = "An unexpected error occurred";
if (exception != null) {
errorMessage = exception.getMessage();
} else if (statusCode == HttpStatus.NOT_FOUND.value()) {
errorMessage = "The requested resource was not found";
}

ErrorResponse errorResponse = new ErrorResponse(statusCode, errorMessage);
return ResponseEntity.status(statusCode)
.contentType(MediaType.APPLICATION_JSON)
.body(errorResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package uk.ac.ebi.spot.ols.controller.api.exception;

import com.fasterxml.jackson.annotation.JsonProperty;

public class ErrorResponse {
@JsonProperty("status")
private int http_status;

@JsonProperty("message")
private String message;

public ErrorResponse(int http_status, String message) {
this.http_status = http_status;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.ac.ebi.spot.ols.controller.api.exception;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {

@ExceptionHandler(NoHandlerFoundException.class)
@ResponseBody
public ResponseEntity<ErrorResponse> handleNoHandlerFoundException(NoHandlerFoundException ex) {
ErrorResponse error = new ErrorResponse(HttpStatus.NOT_FOUND.value(), "Endpoint not found: " + ex.getRequestURL());
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(MediaType.APPLICATION_JSON)
.body(error);
}

@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<ErrorResponse> handleAllExceptions(Exception ex) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
if (ex instanceof ResourceNotFoundException) {
status = HttpStatus.NOT_FOUND;
} else if (ex instanceof BadRequestException) {
status = HttpStatus.BAD_REQUEST;
}

ErrorResponse error = new ErrorResponse(status.value(), ex.getMessage());
return ResponseEntity.status(status)
.contentType(MediaType.APPLICATION_JSON)
.body(error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.ac.ebi.spot.ols.controller.api.exception;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
7 changes: 6 additions & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ spring.jackson.serialization.INDENT_OUTPUT=true

springdoc.swagger-ui.path=/swagger-ui-ols4.html
springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.disable-swagger-default-url=true
springdoc.swagger-ui.disable-swagger-default-url=true

spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false

server.error.whitelabel.enabled=false
Loading