···11+# Wisp Example: Working with form data
22+33+```sh
44+gleam run # Run the server
55+gleam test # Run the tests
66+```
77+88+Wisp has a response body value called `Empty`, which is just that: an empty
99+body. It can be returned by middleware functions such as `wisp.require_json`
1010+when the request isn't suitable for request handler to run, such as if the
1111+request body contains invalid JSON.
1212+1313+You likely want your application to return a generic error page rather than an empty body, and this example shows how to do that.
1414+1515+This example is based off of the [routing example][routing] so read that first.
1616+The additions are detailed here and commented in the code.
1717+1818+[routing]: https://github.com/lpil/wisp/tree/main/examples/1-routing
1919+2020+### `app/router` module
2121+2222+The `handle_request` function has been updated to return responses with the
2323+`wisp.Empty` body.
2424+2525+### `app/web` module
2626+2727+The `middleware` function has been updated to return default responses when an
2828+`wisp.Empty` response body is returned.
2929+3030+### `app_test` module
3131+3232+Tests have been added to test each of the .
3333+3434+### Other files
3535+3636+No changes have been made to the other files.
···11+import wisp
22+import gleam/bool
33+import gleam/string_builder
44+55+pub fn middleware(
66+ req: wisp.Request,
77+ handle_request: fn(wisp.Request) -> wisp.Response,
88+) -> wisp.Response {
99+ let req = wisp.method_override(req)
1010+ use <- wisp.log_request(req)
1111+ use <- wisp.rescue_crashes
1212+ use req <- wisp.handle_head(req)
1313+1414+ // This new middleware has been added to the stack.
1515+ // It is defined below.
1616+ use <- default_responses
1717+1818+ handle_request(req)
1919+}
2020+2121+pub fn default_responses(handle_request: fn() -> wisp.Response) -> wisp.Response {
2222+ let response = handle_request()
2323+2424+ // The `bool.guard` function is used to return the original request if the
2525+ // body is not `wisp.Empty`.
2626+ use <- bool.guard(when: response.body != wisp.Empty, return: response)
2727+2828+ // You can use any logic to return appropriate responses depending on what is
2929+ // best for your application.
3030+ // I'm going to match on the status code and depending on what it is add
3131+ // different HTML as the body. This is a good option for most applications.
3232+ case response.status {
3333+ 404 | 405 ->
3434+ "<h1>There's nothing here</h1>"
3535+ |> string_builder.from_string
3636+ |> wisp.html_body(response, _)
3737+3838+ 400 | 422 ->
3939+ "<h1>Bad request</h1>"
4040+ |> string_builder.from_string
4141+ |> wisp.html_body(response, _)
4242+4343+ 413 ->
4444+ "<h1>Request entity too large</h1>"
4545+ |> string_builder.from_string
4646+ |> wisp.html_body(response, _)
4747+4848+ 500 ->
4949+ "<h1>Internal server error</h1>"
5050+ |> string_builder.from_string
5151+ |> wisp.html_body(response, _)
5252+5353+ // For other status codes redirect to the home page
5454+ _ -> wisp.redirect("/")
5555+ }
5656+}