@Test public void exception() throws Exception { final ODataContext context = mockContext(ODataHttpMethod.GET); final ODataResponse wrappedResponse = mockResponse(HttpStatusCodes.BAD_REQUEST, null, null); ODataMessageException exception = mock(ODataMessageException.class); when(exception.getMessageReference()).thenReturn(ODataMessageException.COMMON); RuntimeException innerException = mock(RuntimeException.class); when(innerException.getLocalizedMessage()).thenReturn("error"); final StackTraceElement[] stackTrace = new StackTraceElement[] { new StackTraceElement("innerClass", "innerMethod", "innerFile", 99), new StackTraceElement("class", "method", "file", 42), new StackTraceElement("outerClass", "outerMethod", "outerFile", 11) }; when(innerException.getStackTrace()).thenReturn(stackTrace); when(exception.getCause()).thenReturn(innerException); when(exception.getStackTrace()).thenReturn(Arrays.copyOfRange(stackTrace, 1, 3)); ODataResponse response = new ODataDebugResponseWrapper(context, wrappedResponse, mock(UriInfo.class), exception, ODataDebugResponseWrapper.ODATA_DEBUG_JSON).wrapResponse(); String entity = StringHelper.inputStreamToString((InputStream) response.getEntity()); assertEquals(EXPECTED .replace(Integer.toString(HttpStatusCodes.OK.getStatusCode()), Integer.toString(HttpStatusCodes.BAD_REQUEST.getStatusCode())) .replace(HttpStatusCodes.OK.getInfo(), HttpStatusCodes.BAD_REQUEST.getInfo()) .replace("null}}", "null," + "\"stacktrace\":{\"exceptions\":[{\"class\":\"" + exception.getClass().getName() + "\"," + "\"message\":\"Common exception\"," + "\"invocation\":{\"class\":\"class\",\"method\":\"method\",\"line\":42}}," + "{\"class\":\"" + innerException.getClass().getName() + "\",\"message\":\"error\"," + "\"invocation\":{\"class\":\"innerClass\",\"method\":\"innerMethod\",\"line\":99}}]," + "\"stacktrace\":[{\"class\":\"class\",\"method\":\"method\",\"line\":42}," + "{\"class\":\"outerClass\",\"method\":\"outerMethod\",\"line\":11}]}}}"), entity); response = new ODataDebugResponseWrapper(context, wrappedResponse, mock(UriInfo.class), exception, ODataDebugResponseWrapper.ODATA_DEBUG_HTML).wrapResponse(); entity = StringHelper.inputStreamToString((InputStream) response.getEntity()); assertTrue(entity.contains("Common exception")); assertTrue(entity.contains("42")); assertTrue(entity.contains("innerMethod")); assertTrue(entity.contains("outerMethod")); }
public ODataResponse wrapResponse() { try { final List<DebugInfo> parts = createParts(); ODataResponseBuilder builder = ODataResponse.status(HttpStatusCodes.OK) .entity(isJson ? wrapInJson(parts) : wrapInHtml(parts)) .contentHeader(isJson ? HttpContentType.APPLICATION_JSON_UTF8 : HttpContentType.TEXT_HTML); if (isDownload) { builder.header("Content-Disposition", "attachment; filename=OData-Response." + new Date().toString().replace(' ', '_').replace(':', '.') + ".html"); } return builder.build(); } catch (final ODataException e) { throw new ODataRuntimeException("Should not happen", e); } catch (final IOException e) { throw new ODataRuntimeException("Should not happen", e); } }
ODataDebugResponseWrapper { public ODataResponse wrapResponse() { try { final List<DebugInfo> parts = createParts(); ODataResponseBuilder builder = ODataResponse.status(HttpStatusCodes.OK) .entity(isJson ? wrapInJson(parts) : wrapInHtml(parts)) .contentHeader(isJson ? HttpContentType.APPLICATION_JSON_UTF8 : HttpContentType.TEXT_HTML); if (isDownload) { builder.header("Content-Disposition", "attachment; filename=OData-Response." + new Date().toString().replace(' ', '_').replace(':', '.') + ".html"); } return builder.build(); } catch (final ODataException e) { throw new ODataRuntimeException("Should not happen", e); } catch (final IOException e) { throw new ODataRuntimeException("Should not happen", e); } } }
ODataDebugResponseWrapper { public ODataResponse wrapResponse() { try { final List<DebugInfo> parts = createParts(); ODataResponseBuilder builder = ODataResponse.status(HttpStatusCodes.OK) .entity(isJson ? wrapInJson(parts) : wrapInHtml(parts)) .contentHeader(isJson ? HttpContentType.APPLICATION_JSON_UTF8 : HttpContentType.TEXT_HTML); if (isDownload) { builder.header("Content-Disposition", "attachment; filename=OData-Response." + new Date().toString().replace(' ', '_').replace(':', '.') + ".html"); } return builder.build(); } catch (final ODataException e) { throw new ODataRuntimeException("Should not happen", e); } catch (final IOException e) { throw new ODataRuntimeException("Should not happen", e); } } ODataDebugResponseWrapper(final ODataContext context, final ODataResponse response, final UriInfo uriInfo,
      final Exception exception, final String debugValue); }
ODataDebugResponseWrapper { public ODataResponse wrapResponse() { try { final List<DebugInfo> parts = createParts(); ODataResponseBuilder builder = ODataResponse.status(HttpStatusCodes.OK) .entity(isJson ? wrapInJson(parts) : wrapInHtml(parts)) .contentHeader(isJson ? HttpContentType.APPLICATION_JSON_UTF8 : HttpContentType.TEXT_HTML); if (isDownload) { builder.header("Content-Disposition", "attachment; filename=OData-Response." + new Date().toString().replace(' ', '_').replace(':', '.') + ".html"); } return builder.build(); } catch (final ODataException e) { throw new ODataRuntimeException("Should not happen", e); } catch (final IOException e) { throw new ODataRuntimeException("Should not happen", e); } } ODataDebugResponseWrapper(final ODataContext context, final ODataResponse response, final UriInfo uriInfo,
      final Exception exception, final String debugValue); ODataResponse wrapResponse(); }
ODataDebugResponseWrapper { public ODataResponse wrapResponse() { try { final List<DebugInfo> parts = createParts(); ODataResponseBuilder builder = ODataResponse.status(HttpStatusCodes.OK) .entity(isJson ? wrapInJson(parts) : wrapInHtml(parts)) .contentHeader(isJson ? HttpContentType.APPLICATION_JSON_UTF8 : HttpContentType.TEXT_HTML); if (isDownload) { builder.header("Content-Disposition", "attachment; filename=OData-Response." + new Date().toString().replace(' ', '_').replace(':', '.') + ".html"); } return builder.build(); } catch (final ODataException e) { throw new ODataRuntimeException("Should not happen", e); } catch (final IOException e) { throw new ODataRuntimeException("Should not happen", e); } } ODataDebugResponseWrapper(final ODataContext context, final ODataResponse response, final UriInfo uriInfo,
      final Exception exception, final String debugValue); ODataResponse wrapResponse(); static final String ODATA_DEBUG_QUERY_PARAMETER; static final String ODATA_DEBUG_JSON; static final String ODATA_DEBUG_HTML; static final String ODATA_DEBUG_DOWNLOAD; }