|
| 1 | +== Whoops |
| 2 | + |
| 3 | +https://github.com/filp/whoops[Whoops] is an error handler framework for PHP. Out-of-the-box, it provides a pretty error interface |
| 4 | +that helps you debug your web projects, but at heart it's a simple yet powerful stacked error |
| 5 | +handling system. |
| 6 | + |
| 7 | +=== Usage |
| 8 | + |
| 9 | +1) Add the dependency: |
| 10 | + |
| 11 | +[dependency, artifactId="jooby-whoops"] |
| 12 | +. |
| 13 | + |
| 14 | +2) Install Whoops |
| 15 | + |
| 16 | +.Java |
| 17 | +[source, java, role="primary"] |
| 18 | +---- |
| 19 | +import io.jooby.whoops.WhoopsModule; |
| 20 | +
|
| 21 | +{ |
| 22 | + install(new WhoopsModule()); <1> |
| 23 | +
|
| 24 | + get("/{id}", ctx -> { |
| 25 | + return ctx.path("id").intValue(); <2> |
| 26 | + }); |
| 27 | +} |
| 28 | +---- |
| 29 | + |
| 30 | +.Kotlin |
| 31 | +[source, kt, role="secondary"] |
| 32 | +---- |
| 33 | +import io.jooby.whoops.WhoopsModule |
| 34 | +
|
| 35 | +{ |
| 36 | + install(WhoopsModule()) <1> |
| 37 | +
|
| 38 | + get("/{id}") { |
| 39 | + ctx.path("id").intValue() <2> |
| 40 | + } |
| 41 | +} |
| 42 | +---- |
| 43 | + |
| 44 | +<1> Install Whoops |
| 45 | +<2> Write some exceptional code (see next section) |
| 46 | + |
| 47 | +=== Test |
| 48 | + |
| 49 | +To test whoops we need to generate an exception. Our route is expecting to get an `integer` for |
| 50 | +the `id` variable. |
| 51 | + |
| 52 | +- Open a browser and type `http://localhost:8080/NaN` |
| 53 | + |
| 54 | +.Whoops in Action |
| 55 | +image::whoops.png[Whoops in Action] |
| 56 | + |
| 57 | +=== How it works |
| 58 | + |
| 59 | +Whoops only display application frames. That is: |
| 60 | + |
| 61 | +A stacktrace element where we are able to find the source code. The whole stacktrace is available in the application log/console. |
| 62 | + |
| 63 | +Whoops does nothing if there is no source code. An example of this is a `404` request: |
| 64 | + |
| 65 | +---- |
| 66 | +Not Found |
| 67 | +message: Page not found |
| 68 | +status code: 404 |
| 69 | +---- |
| 70 | + |
| 71 | +The `404` error is generated by Jooby, not by application code. So whoops let the next exception |
| 72 | +handler (which is the default here) to deal with the exception. |
| 73 | + |
| 74 | +If there is no source code to shows, whoops does nothing. |
| 75 | + |
| 76 | + |
| 77 | +If you have custom error handler just add them before whoops (or after). Remember whoops works |
| 78 | +like any other link:../#error-handler[Error Handler]. |
| 79 | + |
| 80 | +=== Production deploy |
| 81 | + |
| 82 | +Because whoops only works with source code, whoops won't ever run in production deployment |
| 83 | +(unless you deploy with source code). |
| 84 | + |
| 85 | +Still whoops is enabled and will try to find source code in the current application directory. |
| 86 | + |
| 87 | +A better option is to set `whoops.enabled = false` in your application properties file. This will |
| 88 | +completely turn off whoops. |
0 commit comments