Improved faulty JSON error handling
This commit is contained in:
		| @@ -1,5 +1,6 @@ | |||||||
| package com.r11.tools.xslt; | package com.r11.tools.xslt; | ||||||
|  |  | ||||||
|  | import com.fasterxml.jackson.core.JsonParseException; | ||||||
| import com.fasterxml.jackson.databind.JsonMappingException; | import com.fasterxml.jackson.databind.JsonMappingException; | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
| import com.r11.tools.xslt.processors.Saxon; | import com.r11.tools.xslt.processors.Saxon; | ||||||
| @@ -65,8 +66,15 @@ public class SparkInitializer { | |||||||
|         Map<String, String> responseMap = new HashMap<>(); |         Map<String, String> responseMap = new HashMap<>(); | ||||||
|         try { |         try { | ||||||
|             requestMap = mapper.readValue(body, Map.class); |             requestMap = mapper.readValue(body, Map.class); | ||||||
|         } catch (JsonMappingException ex) { |         } catch (JsonMappingException | JsonParseException ex) { | ||||||
|             ex.printStackTrace(); |             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"); |         String data = requestMap.get("data"); | ||||||
| @@ -103,8 +111,15 @@ public class SparkInitializer { | |||||||
|         Map<String, String> responseMap = new HashMap<>(); |         Map<String, String> responseMap = new HashMap<>(); | ||||||
|         try { |         try { | ||||||
|             requestMap = mapper.readValue(body, Map.class); |             requestMap = mapper.readValue(body, Map.class); | ||||||
|         } catch (JsonMappingException ex) { |         } catch (JsonMappingException | JsonParseException ex) { | ||||||
|             LOG.error("JSON mapping error. " + 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"); |         String data = requestMap.get("data"); | ||||||
| @@ -173,17 +188,24 @@ public class SparkInitializer { | |||||||
|     private static final Route xsltHandler = (Request req, Response resp) -> { |     private static final Route xsltHandler = (Request req, Response resp) -> { | ||||||
|         String body = req.body(); |         String body = req.body(); | ||||||
|         ObjectMapper mapper = new ObjectMapper(); |         ObjectMapper mapper = new ObjectMapper(); | ||||||
|         Map<String, String> jsonMap = new HashMap<>(); |         Map<String, String> requestMap = new HashMap<>(); | ||||||
|         Map<String, String> responseMap = new HashMap<>(); |         Map<String, String> responseMap = new HashMap<>(); | ||||||
|         try { |         try { | ||||||
|             jsonMap = mapper.readValue(body, Map.class); |             requestMap = mapper.readValue(body, Map.class); | ||||||
|         } catch (JsonMappingException ex) { |         } catch (JsonMappingException | JsonParseException ex) { | ||||||
|             LOG.error("JSON mapping error. " + 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 data = requestMap.get("data"); | ||||||
|         String query = jsonMap.get("process"); |         String query = requestMap.get("process"); | ||||||
|         String processor = jsonMap.get("processor"); |         String processor = requestMap.get("processor"); | ||||||
|         String version = jsonMap.get("version"); |         String version = requestMap.get("version"); | ||||||
|  |  | ||||||
|         if (processor == null) { |         if (processor == null) { | ||||||
|             return "saxon, xalan"; |             return "saxon, xalan"; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user