ExceptionReportHttpMessageConverter extends AbstractHttpMessageConverter<ExceptionReport> { @Override protected void writeInternal(ExceptionReport report, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Exception e = report.getException(); Map<String, Object> map = new HashMap<>(); map.put("error", UaaStringUtils.getErrorName(e)); map.put("message", e.getMessage()); map.put("error_description", e.getMessage()); if (report.getExtraInfo() != null) { map.putAll(report.getExtraInfo()); } if (report.isTrace()) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); map.put("trace", trace.toString()); } for (HttpMessageConverter<?> converter : messageConverters) { for (MediaType mediaType : converter.getSupportedMediaTypes()) { if (converter.canWrite(Map.class, mediaType)) { @SuppressWarnings({ "rawtypes", "unchecked" }) HttpMessageConverter<Map> messageConverter = (HttpMessageConverter<Map>) converter; messageConverter.write(map, mediaType, outputMessage); return; } } } }  void setMessageConverters(HttpMessageConverter<?>[] messageConverters); @Override List<MediaType> getSupportedMediaTypes();  }
@Test public void testWriteInternal() throws Exception { ExceptionReport report = new ExceptionReport(new Exception("oh noes!")); exceptionReportHttpMessageConverter.writeInternal(report, httpOutputMessage); Map<String, String> expectedFields = new HashMap<>(); expectedFields.put("error", "exception"); expectedFields.put("message", "oh noes!"); expectedFields.put("error_description", "oh noes!"); verify(httpMessageConverter).write(eq(expectedFields), eq(APPLICATION_JSON), eq(httpOutputMessage)); }
@Test public void testWriteInteralWithExtraInfo() throws Exception { Map<String, Object> extraInfo = new HashMap<>(); extraInfo.put("user_id", "cba09242-aa43-4247-9aa0-b5c75c281f94"); extraInfo.put("active", true); extraInfo.put("verified", false); ExceptionReport report = new ExceptionReport(new Exception("oh noes!"), false, extraInfo); exceptionReportHttpMessageConverter.writeInternal(report, httpOutputMessage); Map<String, Object> expectedFields = new HashMap<>(); expectedFields.put("error", "exception"); expectedFields.put("message", "oh noes!"); expectedFields.put("error_description", "oh noes!"); expectedFields.putAll(extraInfo); verify(httpMessageConverter).write(eq(expectedFields), eq(APPLICATION_JSON), eq(httpOutputMessage)); }