Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time

Basic error handling

If you want to generate an error in Express you need to throw an exception.

Let's say you have a NastyError defined:

enum NastyError : ErrorType {
    case Recoverable
    case Fatal(reason:String)

Here is an example on how to throw it:

app.get("/error/:fatal?") { request in
    guard let fatal = request.params["fatal"] else {
        throw NastyError.Recoverable
    throw NastyError.Fatal(reason: fatal)

Now how to handle it. In Express you can define error handlers of two types. General and specific. Specific are the ones with the specified Error type. In our case it's NastyError. Here is an example:

app.errorHandler.register { (e:NastyError) in
    switch e {
    case .Recoverable:
        return Action<AnyContent>.redirect("/")
    case .Fatal(let reason):
        let content = AnyContent(str: "Unrecoverable nasty error happened. Reason: " + reason)
        return Action<AnyContent>.response(.InternalServerError, content: content)

General error handlers are the same except you omit the error type. Something like this:

app.errorHandler.register { e in
    return nil

If your handler can handle the error, you return an Action. Otherwise you return nil. Here is an example of selective error handling:

/// Custom page not found error handler
app.errorHandler.register { (e:ExpressError) in
    switch e {
    case .PageNotFound(let path):
        return Action<AnyContent>.render("404", context: ["path": path], status: .NotFound)
        return nil

Next tutorial: Advanced something