Improved faulty JSON error handling

This commit is contained in:
2023-02-10 11:55:12 +01:00
parent c14cfe8a52
commit 6a0e49546c

View File

@@ -1,5 +1,6 @@
package com.r11.tools.xslt;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.r11.tools.xslt.processors.Saxon;
@@ -65,8 +66,15 @@ public class SparkInitializer {
Map<String, String> responseMap = new HashMap<>();
try {
requestMap = mapper.readValue(body, Map.class);
} catch (JsonMappingException ex) {
ex.printStackTrace();
} catch (JsonMappingException | JsonParseException ex) {
LOG.error("Request JSON error. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("processor", "N/A");
responseMap.put("status", "ERR");
responseMap.put("time", "N/A");
resp.status(400);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
}
String data = requestMap.get("data");
@@ -103,8 +111,15 @@ public class SparkInitializer {
Map<String, String> responseMap = new HashMap<>();
try {
requestMap = mapper.readValue(body, Map.class);
} catch (JsonMappingException ex) {
LOG.error("JSON mapping error. " + ex);
} catch (JsonMappingException | JsonParseException ex) {
LOG.error("Request JSON error. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("processor", "N/A");
responseMap.put("status", "ERR");
responseMap.put("time", "N/A");
resp.status(400);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
}
String data = requestMap.get("data");
@@ -173,17 +188,24 @@ public class SparkInitializer {
private static final Route xsltHandler = (Request req, Response resp) -> {
String body = req.body();
ObjectMapper mapper = new ObjectMapper();
Map<String, String> jsonMap = new HashMap<>();
Map<String, String> requestMap = new HashMap<>();
Map<String, String> responseMap = new HashMap<>();
try {
jsonMap = mapper.readValue(body, Map.class);
} catch (JsonMappingException ex) {
LOG.error("JSON mapping error. " + ex);
requestMap = mapper.readValue(body, Map.class);
} catch (JsonMappingException | JsonParseException ex) {
LOG.error("Request JSON error. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("processor", "N/A");
responseMap.put("status", "ERR");
responseMap.put("time", "N/A");
resp.status(400);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
}
String data = jsonMap.get("data");
String query = jsonMap.get("process");
String processor = jsonMap.get("processor");
String version = jsonMap.get("version");
String data = requestMap.get("data");
String query = requestMap.get("process");
String processor = requestMap.get("processor");
String version = requestMap.get("version");
if (processor == null) {
return "saxon, xalan";