-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3475 from IQSS/3423-api-server-error-handler
Added API-wide exception handlers for common exceptions
- Loading branch information
Showing
7 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
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
30 changes: 30 additions & 0 deletions
30
src/main/java/edu/harvard/iq/dataverse/api/CrashBoomBangEndpoint.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,30 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Response; | ||
|
||
/** | ||
* An API endpoint that crashes. Used for testing the error handlers. Should | ||
* be removed once #3423 is closed. | ||
* | ||
* @author michael | ||
*/ | ||
@Path("boom") | ||
public class CrashBoomBangEndpoint extends AbstractApiBean { | ||
|
||
@GET | ||
@Path("aoob") | ||
public Response arrayError() { | ||
String boom = "abc".split("3")[9]; | ||
return ok("Not gonna happen"); | ||
} | ||
|
||
@GET | ||
@Path("npe") | ||
public Response nullPointer() { | ||
String boom = null; | ||
boom.length(); | ||
return ok("Not gonna happen"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ain/java/edu/harvard/iq/dataverse/api/errorhandlers/ArrayOutOfBoundsExceptionHandler.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 edu.harvard.iq.dataverse.api.errorhandlers; | ||
|
||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.json.Json; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* Produces custom 500 messages for the API. | ||
* @author michael | ||
*/ | ||
@Provider | ||
public class ArrayOutOfBoundsExceptionHandler implements ExceptionMapper<java.lang.ArrayIndexOutOfBoundsException>{ | ||
|
||
private static final Logger logger = Logger.getLogger(ServeletExceptionHandler.class.getName()); | ||
|
||
@Context | ||
HttpServletRequest request; | ||
|
||
@Override | ||
public Response toResponse(java.lang.ArrayIndexOutOfBoundsException ex){ | ||
String incidentId = UUID.randomUUID().toString(); | ||
logger.log(Level.SEVERE, "API internal error " + incidentId +": ArrayOutOfBounds:" + ex.getMessage(), ex); | ||
return Response.status(500) | ||
.entity( Json.createObjectBuilder() | ||
.add("status", "ERROR") | ||
.add("code", 500) | ||
.add("message", "Internal server error. More details available at the server logs.") | ||
.add("incidentId", incidentId) | ||
.build()) | ||
.type("application/json").build(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...verse/api/NotAllowedExceptionHandler.java → ...rhandlers/NotAllowedExceptionHandler.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
2 changes: 1 addition & 1 deletion
2
...taverse/api/NotFoundExceptionHandler.java → ...rorhandlers/NotFoundExceptionHandler.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
38 changes: 38 additions & 0 deletions
38
src/main/java/edu/harvard/iq/dataverse/api/errorhandlers/NullPointerExceptionHandler.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 edu.harvard.iq.dataverse.api.errorhandlers; | ||
|
||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.json.Json; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* Produces custom 500 messages for the API. | ||
* @author michael | ||
*/ | ||
@Provider | ||
public class NullPointerExceptionHandler implements ExceptionMapper<java.lang.NullPointerException>{ | ||
|
||
private static final Logger logger = Logger.getLogger(ServeletExceptionHandler.class.getName()); | ||
|
||
@Context | ||
HttpServletRequest request; | ||
|
||
@Override | ||
public Response toResponse(java.lang.NullPointerException ex){ | ||
String incidentId = UUID.randomUUID().toString(); | ||
logger.log(Level.SEVERE, "API internal error " + incidentId +": Null Pointer", ex); | ||
return Response.status(500) | ||
.entity( Json.createObjectBuilder() | ||
.add("status", "ERROR") | ||
.add("code", 500) | ||
.add("message", "Internal server error. More details available at the server logs.") | ||
.add("incidentId", incidentId) | ||
.build()) | ||
.type("application/json").build(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/edu/harvard/iq/dataverse/api/errorhandlers/ServeletExceptionHandler.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 edu.harvard.iq.dataverse.api.errorhandlers; | ||
|
||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.json.Json; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* Produces custom 500 messages for the API. | ||
* @author michael | ||
*/ | ||
@Provider | ||
public class ServeletExceptionHandler implements ExceptionMapper<javax.servlet.ServletException>{ | ||
|
||
private static final Logger logger = Logger.getLogger(ServeletExceptionHandler.class.getName()); | ||
|
||
@Context | ||
HttpServletRequest request; | ||
|
||
@Override | ||
public Response toResponse(javax.servlet.ServletException ex){ | ||
String incidentId = UUID.randomUUID().toString(); | ||
logger.log(Level.SEVERE, "API internal error " + incidentId +": " + ex.getMessage(), ex); | ||
return Response.status(500) | ||
.entity( Json.createObjectBuilder() | ||
.add("status", "ERROR") | ||
.add("code", 500) | ||
.add("message", "Internal server error. More details available at the server logs.") | ||
.add("incidentId", incidentId) | ||
.build()) | ||
.type("application/json").build(); | ||
} | ||
} |