|
3 | 3 | */ |
4 | 4 | package com.marklogic.client.io; |
5 | 5 |
|
6 | | -import java.io.IOException; |
7 | | -import java.io.InputStream; |
8 | | -import java.util.Map; |
9 | | - |
10 | | -import com.fasterxml.jackson.core.JsonParseException; |
11 | | -import com.fasterxml.jackson.databind.JsonMappingException; |
12 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
13 | 7 | import com.marklogic.client.impl.FailedRequest; |
14 | 8 | import com.marklogic.client.impl.FailedRequestParser; |
15 | 9 |
|
16 | | -/** |
17 | | - * This class is provided as a convenience method for parsing MarkLogic errors that |
18 | | - * are serialized as JSON. In order to use this class, your project must provide |
19 | | - * the Jackson data binding library for JSON. |
20 | | - */ |
| 10 | +import java.io.InputStream; |
| 11 | +import java.util.Map; |
| 12 | + |
21 | 13 | public class JSONErrorParser implements FailedRequestParser { |
22 | 14 |
|
23 | | - @SuppressWarnings("unchecked") |
24 | | - @Override |
25 | | - public FailedRequest parseFailedRequest(int httpStatus, InputStream content) { |
26 | | - FailedRequest failure = new FailedRequest(); |
27 | | - ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally |
28 | | - Map<String, Map<String, String>> errorData; |
29 | | - try { |
30 | | - errorData = mapper.readValue(content, Map.class); |
31 | | - Map<String, String> errorBody = errorData.get("errorResponse"); |
32 | | - failure.setStatusCode(httpStatus); |
33 | | - failure.setStatusString(errorBody.get("status")); |
34 | | - failure.setMessageCode(errorBody.get("messageCode")); |
35 | | - failure.setMessageString(errorBody.get("message")); |
36 | | - failure.setStackTrace(errorBody.get("stackTrace")); |
37 | | - } catch (JsonParseException e1) { |
38 | | - failure.setStatusCode(httpStatus); |
39 | | - failure.setMessageString("Request failed. Error body not received from server"); |
40 | | - } catch (JsonMappingException e1) { |
41 | | - failure.setStatusCode(httpStatus); |
42 | | - failure.setMessageString("Request failed. Error body not received from server"); |
43 | | - } catch (IOException e1) { |
44 | | - failure.setStatusCode(httpStatus); |
45 | | - failure.setMessageString("Request failed. Error body not received from server"); |
46 | | - } |
47 | | - return failure; |
48 | | - } |
| 15 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 16 | + |
| 17 | + @SuppressWarnings("unchecked") |
| 18 | + @Override |
| 19 | + public FailedRequest parseFailedRequest(int httpStatus, InputStream content) { |
| 20 | + Map<String, Map<String, String>> errorData; |
| 21 | + try { |
| 22 | + errorData = objectMapper.readValue(content, Map.class); |
| 23 | + } catch (Exception ex) { |
| 24 | + return new FailedRequest(httpStatus, "Request failed; could not parse JSON in response body."); |
| 25 | + } |
| 26 | + |
| 27 | + if (!errorData.containsKey("errorResponse")) { |
| 28 | + return new FailedRequest(httpStatus, "Unexpected JSON in response body; did not find 'errorResponse' key; response: " + errorData); |
| 29 | + } |
| 30 | + |
| 31 | + FailedRequest failure = new FailedRequest(); |
| 32 | + Map<String, String> errorBody = errorData.get("errorResponse"); |
| 33 | + failure.setStatusCode(httpStatus); |
| 34 | + failure.setStatusString(errorBody.get("status")); |
| 35 | + failure.setMessageCode(errorBody.get("messageCode")); |
| 36 | + failure.setMessageString(errorBody.get("message")); |
| 37 | + failure.setStackTrace(errorBody.get("stackTrace")); |
| 38 | + return failure; |
| 39 | + } |
49 | 40 | } |
0 commit comments