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

API TEST - Add Custom Exception Handler #738

Merged
merged 3 commits 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,15 @@ public static JsonElement get(String url) throws IOException {
System.out.println("GET " + url);

HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
InputStream is = null;

if (100 <= conn.getResponseCode() && conn.getResponseCode() <= 399) {
InputStream is = conn.getInputStream();
Reader reader = new InputStreamReader(is, "UTF-8");
JsonElement result = JsonParser.parseReader(reader);
return result;
is = conn.getInputStream();
} else {
InputStream is = conn.getErrorStream();
Reader reader = new InputStreamReader(is, "UTF-8");
JsonObject error = new JsonObject();
error.addProperty("error", IOUtils.toString(is, StandardCharsets.UTF_8));
return error;
is = conn.getErrorStream();
}
Reader reader = new InputStreamReader(is, "UTF-8");
return JsonParser.parseReader(reader);
}

public JsonElement normalizeURLs(JsonElement element) {
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:31 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/annotation-properties-simple/entities/http%253A%252F%252Fwww.ebi.ac.uk%252Ftestcases%252Fannotation-properties%252Fsimple.owl%2523MyAnnotationProperty"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:31 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/data-property-domain-range-restrictions/classes/http%253A%252F%252Fwww.ebi.ac.uk%252Ftestcases%252Fowl2primer%252Fdata-property-domain-range-restrictions.rdf%2523Person"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:31 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/data-property-domain-range-restrictions/entities/http%253A%252F%252Fwww.ebi.ac.uk%252Ftestcases%252Fowl2primer%252Fdata-property-domain-range-restrictions.rdf%2523Person"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:31 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/data-property-domain-range-restrictions/entities/http%253A%252F%252Fwww.ebi.ac.uk%252Ftestcases%252Fowl2primer%252Fdata-property-domain-range-restrictions.rdf%2523hasAge"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FAPOLLO_SV_00000008"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FAPOLLO_SV_00000032"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FAPOLLO_SV_00000033"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FAPOLLO_SV_00000522"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FAPOLLO_SV_00000524"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FAPOLLO_SV_00000796"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000001"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000002"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000003"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000004"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000006"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000008"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000009"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000011"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000015"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000016"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000017"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000018"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000019"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000020"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000023"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000024"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000026"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000027"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"error": "<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Aug 30 13:21:46 BST 2024</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>"
"status": 404,
"message": "Endpoint not found: /api/ontologies/duo/classes/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252FBFO_0000028"
}
Loading
Loading