-
Notifications
You must be signed in to change notification settings - Fork 477
/
NotFoundExceptionHandler.java
43 lines (35 loc) · 1.37 KB
/
NotFoundExceptionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package edu.harvard.iq.dataverse.api.errorhandlers;
import javax.json.Json;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.NotFoundException;
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 404 messages for the API.
* @author michael
*/
@Provider
public class NotFoundExceptionHandler implements ExceptionMapper<NotFoundException>{
@Context
HttpServletRequest request;
@Override
public Response toResponse(NotFoundException ex){
String uri = request.getRequestURI();
String exMessage = ex.getMessage();
String outputMessage;
if (exMessage != null && exMessage.toLowerCase().startsWith("datafile")) {
outputMessage = exMessage;
} else {
outputMessage = "endpoint does not exist on this server. Please check your code for typos, or consult our API guide at http://guides.dataverse.org.";
}
return Response.status(404)
.entity( Json.createObjectBuilder()
.add("status", "ERROR")
.add("code", 404)
.add("message", "'" + uri + "' " + outputMessage)
.build())
.type("application/json").build();
}
}