-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add Custom Exception Handler (#737)
- Loading branch information
1 parent
1b74642
commit 2f92121
Showing
6 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/exception/BadRequestException.java
This file contains 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
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/exception/CustomErrorController.java
This file contains 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
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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/exception/ErrorResponse.java
This file contains 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
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...end/src/main/java/uk/ac/ebi/spot/ols/controller/api/exception/GlobalExceptionHandler.java
This file contains 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
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); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
.../src/main/java/uk/ac/ebi/spot/ols/controller/api/exception/ResourceNotFoundException.java
This file contains 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
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); | ||
} | ||
} |
This file contains 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