Skip to content

Life Cycle

Yuki Takei edited this page Oct 10, 2016 · 2 revisions

Life Cycle

Error Handling

You can catch the error that are emitted from the middleware or the route with app.catch handler.

let app = Slimane()

app.use { request, response, responder in
    responder(.error(FooError))
}

app.`catch` { error, request, response, responder in
    var response = response
    switch error {
    case RoutingError.routeNotFound:
        response.status(.notFound)
        response.text("\(error)")

    case StaticMiddlewareError.resourceNotFound:
        response.status(.notFound)
        response.text("\(error)")

    default:
        response.status(.internalServerError)
        response.text("\(error)") // fooError
    }

    responder(.respond(response))
}

try! app.listen()

Finalization for the Request

After responded to the content, You can process finalization for the request with app.finally block. Here is a logging example for the request.

let app = Slimane()

app.use(.get, "/") { request, response, responder in
    var response = response
    response.text("Welcome to Slimane!")
    responder(.respond(response))
}

app.finally { request, response in
  print("\(request.method) \(request.path ?? "/") \(response.status.statusCode)") // GET / 200
}

try! app.listen()
Clone this wiki locally