use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.
the class DiagnosticErrorRenderer method tryToRender.
public static void tryToRender(Context context, Result result, DiagnosticError diagnosticError, boolean throwInternalServerExceptionOnError) {
try {
DiagnosticErrorRenderer errorRenderer = build(context, result, diagnosticError);
errorRenderer.renderResult(context, result);
} catch (IOException e) {
// fallback to ninja system-wide error handler?
if (throwInternalServerExceptionOnError) {
throw new InternalServerErrorException(e);
} else {
logger.error("Something is really fishy. Unable to render diagnostic error", e);
}
}
}
use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.
the class Result method renderRaw.
/**
* This method directly renders the byte array to the output. It
* completely bypasses any rendering engine.
*
* Thus you can render anything you want.
*
* Chaining of resultRaw().resultRaw()... is NOT supported. Mixing with render()
* is NOT supported.
*
* It is always recommended to implement your own RenderingEngine OR
* use existing rendering engines.
*
* @param bytes The bytes to render.
* @return A result that will render the string directly to the output stream.
*/
public Result renderRaw(final byte[] bytes) {
Renderable renderable = new Renderable() {
@Override
public void render(Context context, Result result) {
if (result.getContentType() == null) {
result.contentType(Result.APPLICATION_OCTET_STREAM);
}
ResponseStreams responseStreams = context.finalizeHeaders(result);
try (OutputStream outputStream = responseStreams.getOutputStream()) {
outputStream.write(bytes);
} catch (IOException ioException) {
throw new InternalServerErrorException(ioException);
}
}
};
render(renderable);
return this;
}
use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.
the class UploadController method uploadFinish.
/**
*
* This upload method expects a file and simply displays the file in the
* multipart upload again to the user (in the correct mime encoding).
*
* @param context
* @return
* @throws Exception
*/
public Result uploadFinish(Context context) throws Exception {
// we are using a renderable inner class to stream the input again to
// the user
Renderable renderable = new Renderable() {
@Override
public void render(Context context, Result result) {
try {
// make sure the context really is a multipart context...
if (context.isMultipart()) {
// This is the iterator we can use to iterate over the
// contents
// of the request.
FileItemIterator fileItemIterator = context.getFileItemIterator();
while (fileItemIterator.hasNext()) {
FileItemStream item = fileItemIterator.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
String contentType = item.getContentType();
if (contentType != null) {
result.contentType(contentType);
} else {
contentType = mimeTypes.getMimeType(name);
}
ResponseStreams responseStreams = context.finalizeHeaders(result);
if (item.isFormField()) {
System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
ByteStreams.copy(stream, responseStreams.getOutputStream());
}
}
}
} catch (IOException | FileUploadException exception) {
throw new InternalServerErrorException(exception);
}
}
};
return new Result(200).render(renderable);
}
use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.
the class NinjaDefaultTest method testOnRouteRequestWhenInternalServerErrorExceptionInDiagnosticMode.
@Test
public void testOnRouteRequestWhenInternalServerErrorExceptionInDiagnosticMode() throws Exception {
FilterChain filterChain = Mockito.mock(FilterChain.class);
Mockito.when(route.getFilterChain()).thenReturn(filterChain);
InternalServerErrorException internalServerErrorException = new InternalServerErrorException("That's an InternalServerErrorException that should be handled by onError!");
Mockito.when(filterChain.next(contextImpl)).thenThrow(internalServerErrorException);
when(ninjaProperties.isDev()).thenReturn(true);
when(ninjaProperties.getBooleanWithDefault(NinjaConstant.DIAGNOSTICS_KEY_NAME, true)).thenReturn(true);
ninjaDefault.onRouteRequest(contextImpl);
Result localResult = ninjaDefault.getInternalServerErrorResult(contextImpl, internalServerErrorException);
assertThat(localResult.getRenderable(), CoreMatchers.instanceOf(DiagnosticError.class));
}
use of ninja.exceptions.InternalServerErrorException in project ninja by ninjaframework.
the class NinjaDefaultTest method testOnRouteRequestWhenInternalServerErrorException.
@Test
public void testOnRouteRequestWhenInternalServerErrorException() throws Exception {
FilterChain filterChain = Mockito.mock(FilterChain.class);
Mockito.when(route.getFilterChain()).thenReturn(filterChain);
InternalServerErrorException internalServerErrorException = new InternalServerErrorException("That's an InternalServerErrorException that should be handled by onError!");
Mockito.when(filterChain.next(contextImpl)).thenThrow(internalServerErrorException);
ninjaDefault.onRouteRequest(contextImpl);
verify(ninjaDefault).getInternalServerErrorResult(contextImpl, internalServerErrorException, null);
}
Aggregations