Convert exception to JSON

Is it possible in Java 7 to convert an Exception object to Json?

Example:

 try { //something } catch(Exception ex) { Gson gson = new Gson(); System.out.println(gson.toJson(ex)); } 
+10
java json exception
source share
3 answers

Theoretically, you can also iterate over items in a stack trace and generate something similar to:

 { "NullPointerException" : { "Exception in thread \"main\" java.lang.NullPointerException", { "Book.java:16" : "com.example.myproject.Book.getTitle", "Author.java:25" : "at com.example.myproject.Author.getBookTitles", "Bootstrap.java:14" : "at com.example.myproject.Bootstrap.main()" } }, "Caused By" : { "Exception in thread \"main\" java.lang.NullPointerException", { "Book.java:16" : "com.example.myproject.Book.getTitle", "Author.java:25" : "at com.example.myproject.Author.getBookTitles", "Bootstrap.java:14" : "at com.example.myproject.Bootstrap.main()" } } } 

You can repeat the exception as follows :

 catch (Exception cause) { StackTraceElement elements[] = cause.getStackTrace(); for (int i = 0, n = elements.length; i < n; i++) { System.err.println(elements[i].getFileName() + ":" + elements[i].getLineNumber() + ">> " + elements[i].getMethodName() + "()"); } } 
+6
source share

well, it’s possible to do something like this, although you don’t want to convert the exception object itself, but rather the message that it contains in the format you are developing, something like:

 // […] } catch (Exception ex) { Gson gson = new Gson(); Map<String, String> exc_map = new HashMap<String, String>(); exc_map.put("message", ex.toString()); exc_map.put("stacktrace", getStackTrace(ex)); System.out.println(gson.toJson(exc_map)); } 

with getStackTrace() defined as assuming the answer :

 public static String getStackTrace(final Throwable throwable) { final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); return sw.getBuffer().toString(); } 
+7
source share

The following is the standard procedure for converting an exception to JSON:

 public static JSONObject convertToJSON(Throwable e, String context) throws Exception { JSONObject responseBody = new JSONObject(); JSONObject errorTag = new JSONObject(); responseBody.put("error", errorTag); errorTag.put("code", 400); errorTag.put("context", context); JSONArray detailList = new JSONArray(); errorTag.put("details", detailList); Throwable nextRunner = e; List<ExceptionTracer> traceHolder = new ArrayList<ExceptionTracer>(); while (nextRunner!=null) { Throwable runner = nextRunner; nextRunner = runner.getCause(); detailObj.put("code", runner.getClass().getName()); String msg = runner.toString(); detailObj.put("message",msg); detailList.put(detailObj); } JSONArray stackList = new JSONArray(); for (StackTraceElement ste : e.getStackTrace()) { stackList.put(ste.getFileName() + ": " + ste.getMethodName() + ": " + ste.getLineNumber()); } errorTag.put("stack", stackList); return responseBody; } 

You can find the complete open source library that implements this at: Purple JSON Utilities . This library supports JSON objects as well as exceptions.

This creates the JSON structure of this form:

 { "error": { "code": "400", "message": "main error message here", "target": "approx what the error came from", "details": [ { "code": "23-098a", "message": "Disk drive has frozen up again. It needs to be replaced", "target": "not sure what the target is" } ], "innererror": { "trace": [ ... ], "context": [ ... ] } } } 

This is the format proposed by the OASIS standard for OASIS OData data , and it seems to be the most standard option, but there are still no high rates of adoption of any standard.

Details are discussed in my blog post about error handling in the JSON REST API.

+1
source share

All Articles