forked from ratpack/ratpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a error handler impl for debugging purposes.
- Loading branch information
Showing
11 changed files
with
130 additions
and
62 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
ratpack-core/src/main/java/ratpack/error/DebugErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
42 changes: 0 additions & 42 deletions
42
ratpack-core/src/main/java/ratpack/error/internal/PrintingServerErrorHandler.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
ratpack-core/src/test/groovy/ratpack/error/DebugErrorHandlerTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters