← all lessons

Lesson 11

Errors

Errors

Failures are modeled too. An error is an ordinary structure marked with the @error trait — its value is exactly "client" (the caller’s fault, ~4xx) or "server" (the service’s fault, ~5xx):

@error("client")
structure NoSuchCity {
    @required
    message: String
}

The @error trait is what makes it an error — message is just a widely-followed convention many generators surface as the exception message.

HTTP status codes

For HTTP protocols, @httpError(code) sets the response status:

@error("client")
@httpError(404)
structure NoSuchCity {
    @required
    message: String
}

Attaching errors to operations

List error shapes in an operation’s errors:

@readonly
operation GetCity {
    input := {
        @required
        cityId: String
    }
    output := {
        @required
        name: String
    }
    errors: [
        NoSuchCity
    ]
}

Only @error-marked structures may appear in an errors list.

Your turn

Define the error and attach it. The checks assert the exact value of both @error (the string "client") and @httpError (the number 404) — so a @error("server") or a @httpError(400) won’t pass.