JsonPayload implements SpaceFields { public static ObjectNode toJson(Throwable t, boolean debug) { ObjectNode json = Json.object(); if (!Strings.isNullOrEmpty(t.getMessage())) json.put("message", t.getMessage()); if (t instanceof SpaceException) { SpaceException se = (SpaceException) t; if (se.code() != null) json.put("code", se.code()); if (se.details() != null) json.set("details", se.details()); } if (debug) { json.put("type", t.getClass().getName()); if (t instanceof ElasticsearchException) { ElasticsearchException elasticException = ((ElasticsearchException) t); for (String key : elasticException.getHeaderKeys()) { json.with("details").set(key, Json.toJsonNode(elasticException.getHeader(key))); } } ArrayNode array = json.putArray("trace"); for (StackTraceElement element : t.getStackTrace()) array.add(element.toString()); } if (t.getCause() != null) json.set("cause", toJson(t.getCause(), debug)); return json; } private  JsonPayload(int status); JsonPayload withLocation(String uri); JsonPayload withLocation(String uri, String type, String id); JsonPayload withVersion(long version); JsonPayload withCode(int code); JsonPayload withHeader(String key, String value); JsonPayload withHeaders(Map<String, String> headers); JsonPayload withContent(Object object); JsonPayload withFields(Object... fields); JsonPayload withResults(ArrayNode array); Payload build(); static JsonPayload status(int status); static JsonPayload ok(); static JsonPayload created(); static JsonPayload saved(boolean created); static JsonPayload saved(boolean created, String uri, String type, String id); static JsonPayload saved(DataWrap<?> object); static JsonPayload error(int httpStatus); static JsonPayload error(Throwable t); static JsonPayload error(int httpStatus, String message, Object... args); JsonPayload withError(String message, Object... args); JsonPayload withError(Throwable throwable); JsonPayload withError(JsonNode error); static int toStatus(Throwable t); static ObjectNode toJson(Throwable t, boolean debug);  }
@Test public void shouldConvertRuntimeExceptionToJsonError() { JsonNode json = JsonPayload.toJson(new RuntimeException(new NullPointerException()), true); assertEquals(RuntimeException.class.getName(), json.get("type").asText()); assertEquals(NullPointerException.class.getName(), json.get("message").asText()); assertTrue(json.get("trace").size() > 5); assertEquals(NullPointerException.class.getName(), json.get("cause").get("type").asText()); assertNull(json.get("cause").get("message")); assertTrue(json.get("cause").get("trace").size() > 5); }
@Test public void convertSpaceExceptionToJsonErrorDebugMode() { JsonNode json = JsonPayload.toJson(Exceptions.alreadyExists("credentials", "vince"), true); assertEquals(SpaceException.class.getName(), json.get("type").asText()); assertEquals("[credentials][vince] already exists", json.get("message").asText()); assertEquals("already-exists", json.get("code").asText()); assertNull(json.get("cause")); }
@Test public void convertSpaceExceptionToJsonErrorNoDebugMode() { JsonNode json = JsonPayload.toJson(Exceptions.alreadyExists("credentials", "vince"), false); assertNull(json.get("type")); assertEquals("[credentials][vince] already exists", json.get("message").asText()); assertEquals("already-exists", json.get("code").asText()); assertNull(json.get("cause")); }