Errors
Errors let you define consistent domain/application errors in one place. They are useful for keeping error code and status conventions aligned across services.
Basic Syntax
Use error followed by the error name and a property block.
contractor
error UserNotFoundError {
message: "User not found"
code: "USER_NOT_FOUND_ERR"
status: 404
}Properties
Supported properties:
message(required): string literalcode(optional): string literalscope(optional): string literalstatus(optional): string literal or number literal
Type checker rules:
messageis required.codemust be string if provided.scopemust be string if provided.statusmust be string or number if provided.
The parser only accepts literal values here. You cannot use a model field, variable, or expression.
Example
contractor
error SignInInformationIncorrectError {
message: "Sign-in information incorrect"
code: "SIGNIN_INFO_INCORRECT_ERR"
status: 400
}
error AuthorizationError {
message: "Unauthenticated"
code: "AUTHORIZATION_ERR"
status: "401"
scope: "auth"
}Invalid Examples
contractor
error BrokenError {
code: "BROKEN_ERR"
status: 500
}This fails because message is required.
contractor
error BadStatusError {
message: "Bad"
status: true
}This fails because status must be a string or number literal.
contractor
error BadCodeError {
message: "Bad"
code: 123
}This fails because code must be a string literal.
Common Mistakes
- Omitting
message. - Using non-literal values for properties.
- Using unknown properties in error blocks.