Skip to content

Commit

Permalink
Add a error handler impl for debugging purposes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldaley committed Apr 7, 2014
1 parent 2cf3946 commit 5e97524
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 62 deletions.
56 changes: 56 additions & 0 deletions ratpack-core/src/main/java/ratpack/error/DebugErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ratpack.error;

import ratpack.handling.Context;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
* A simple server and client error handler that prints out information in plain text to the response.
* <p>
* <b>This is not suitable for use in production</b> as it exposes internal information about your application via stack traces.
*/
public class DebugErrorHandler implements ServerErrorHandler, ClientErrorHandler {

/**
* {@link Exception#printStackTrace() Prints the stacktrace} of the given exception to the response with a 500 status.
*
* @param context The context being processed
* @param exception The exception that occurred
*/
@Override
public void error(Context context, Exception exception) {
Writer writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
context.getResponse().status(500).send(writer.toString());
}

/**
* Prints the string "Client error «statusCode»" to the response as text with the given status code.
*
* @param context The context
* @param statusCode The 4xx status code that explains the problem
*/
@Override
public void error(Context context, int statusCode) {
context.getResponse().status(statusCode).send(String.format("Client error %s", statusCode));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package ratpack.background

import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.test.internal.RatpackGroovyDslSpec

class BackgroundSpec extends RatpackGroovyDslSpec {
Expand Down Expand Up @@ -48,7 +48,7 @@ class BackgroundSpec extends RatpackGroovyDslSpec {
def "by default errors during background operations are forwarded to server error handler"() {
when:
modules {
bind ServerErrorHandler, PrintingServerErrorHandler
bind ServerErrorHandler, DebugErrorHandler
}
handlers {
get {
Expand Down Expand Up @@ -89,7 +89,7 @@ class BackgroundSpec extends RatpackGroovyDslSpec {
def "errors in custom error handlers are forwarded to the server error handler"() {
when:
modules {
bind ServerErrorHandler, PrintingServerErrorHandler
bind ServerErrorHandler, DebugErrorHandler
}
handlers {
get {
Expand All @@ -111,7 +111,7 @@ class BackgroundSpec extends RatpackGroovyDslSpec {
def "errors in success handlers are forwarded to the server error handler"() {
when:
modules {
bind ServerErrorHandler, PrintingServerErrorHandler
bind ServerErrorHandler, DebugErrorHandler
}
handlers {
get {
Expand All @@ -133,7 +133,7 @@ class BackgroundSpec extends RatpackGroovyDslSpec {
def "closure arg type mismatch errors on success handler are handled well"() {
when:
modules {
bind ServerErrorHandler, PrintingServerErrorHandler
bind ServerErrorHandler, DebugErrorHandler
}
handlers {
get {
Expand All @@ -154,7 +154,7 @@ class BackgroundSpec extends RatpackGroovyDslSpec {
def "closure arg type mismatch errors on error handler are handled well"() {
when:
modules {
bind ServerErrorHandler, PrintingServerErrorHandler
bind ServerErrorHandler, DebugErrorHandler
}
handlers {
get {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ratpack.error

import ratpack.test.internal.RatpackGroovyDslSpec

class DebugErrorHandlerTest extends RatpackGroovyDslSpec {

def "debug error handler prints info"() {
given:
def e = new RuntimeException("!")

when:
modules {
bind ServerErrorHandler, new DebugErrorHandler()
bind ClientErrorHandler, new DebugErrorHandler()
}

handlers {
get("client") { clientError(404) }
get("server") { error(e) }
}

then:
with(get("client")) {
statusCode == 404
body.asString() == "Client error 404"
}

def str = new StringWriter().with {
e.printStackTrace(new PrintWriter(it))
it.toString()
}

with(get("server")) {
statusCode == 500
body.asString() == str
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package ratpack.handling

import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.test.internal.RatpackGroovyDslSpec

import static ratpack.registry.Registries.registry
Expand Down Expand Up @@ -170,7 +170,7 @@ class ProcessingInterceptionSpec extends RatpackGroovyDslSpec {
given:
modules {
bind new RecordingInterceptor("id") // just need any interceptor
bind ServerErrorHandler, new PrintingServerErrorHandler()
bind ServerErrorHandler, new DebugErrorHandler()
}

when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package ratpack.handling

import groovy.transform.TupleConstructor
import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.registry.NotInRegistryException
import ratpack.test.internal.RatpackGroovyDslSpec

Expand All @@ -28,7 +28,7 @@ class RegistryInsertionHandlerSpec extends RatpackGroovyDslSpec {

def setup() {
modules {
bind ServerErrorHandler, new PrintingServerErrorHandler()
bind ServerErrorHandler, new DebugErrorHandler()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
package ratpack.http

import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.form.Form
import ratpack.test.internal.RatpackGroovyDslSpec

class FormHandlingSpec extends RatpackGroovyDslSpec {

def setup() {
modules {
bind ServerErrorHandler, new PrintingServerErrorHandler()
bind ServerErrorHandler, new DebugErrorHandler()
}
}
def "can get form params"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package ratpack.render

import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.handling.Context
import ratpack.test.internal.RatpackGroovyDslSpec

Expand Down Expand Up @@ -53,7 +53,7 @@ class RenderingSpec extends RatpackGroovyDslSpec {
def "can use available renderers"() {
when:
handlers {
register(new PrintingServerErrorHandler()) {
register(new DebugErrorHandler()) {
register(new ThingRenderer()) {
get {
render new Thing("foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package ratpack.guice

import com.google.inject.AbstractModule
import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.handling.Context
import ratpack.render.NoSuchRendererException
import ratpack.render.RendererSupport
Expand Down Expand Up @@ -47,7 +47,7 @@ class RendererBindingsSpec extends RatpackGroovyDslSpec {
protected void configure() {
bind(IntRenderer)
bind(StringRenderer)
bind(ServerErrorHandler).to(PrintingServerErrorHandler)
bind(ServerErrorHandler).to(DebugErrorHandler)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ratpack-rx/src/test/groovy/ratpack/rx/RxBackgroundSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package ratpack.rx

import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.test.internal.RatpackGroovyDslSpec

import static ratpack.rx.RxRatpack.observe
Expand Down Expand Up @@ -52,7 +52,7 @@ class RxBackgroundSpec extends RatpackGroovyDslSpec {
def "background errors are sent to the context renderer"() {
when:
modules {
bind ServerErrorHandler, new PrintingServerErrorHandler()
bind ServerErrorHandler, new DebugErrorHandler()
}
handlers {
get(":value") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package ratpack.session

import ratpack.error.ServerErrorHandler
import ratpack.error.internal.PrintingServerErrorHandler
import ratpack.error.DebugErrorHandler
import ratpack.session.store.MapSessionsModule
import ratpack.session.store.SessionStorage
import ratpack.session.store.SessionStore
Expand All @@ -29,7 +29,7 @@ class SessionSpec extends RatpackGroovyDslSpec {
modules << new SessionModule()
modules << new MapSessionsModule(10, 5)
modules {
bind ServerErrorHandler, new PrintingServerErrorHandler()
bind ServerErrorHandler, new DebugErrorHandler()
}
}

Expand Down

0 comments on commit 5e97524

Please sign in to comment.