Skip to content

Errors

Outlines the default error response schema, and some common error types.

Default response schema for 4xx and 5xx HTTP status codes.

Parameter Type Description
status integer The HTTP status code
message string The error message
errors array The errors

A JSON example of this model.

{
"status": 400,
"message": "Error",
"errors": [
null
]
}

Here’s an example of how to use the Error response within an API operation:

import "@common-grants/core";
import "@typespec/http";
using TypeSpec.Http;
using CommonGrants.Responses;
model TestModel {
id: string;
name: string;
}
@summary("Get test")
@doc("Get a test model")
@get
op getTest(): OkT<TestModel> | Error;
VersionChangesSchema
0.1.0
  • Added Error model
Error.yaml

A failure to submit an application due to validation errors.

Property Type Required Description
status integer Yes The HTTP status code
message string Yes The error message
errors Array<unknown> Yes The errors

A JSON example of this model.

{
"status": 400,
"message": "Application submission failed due to validation errors",
"errors": [
{
"field": "formA.name",
"message": "Name is required"
}
]
}
VersionChangesSchema
0.2.0
  • Added ApplicationSubmissionError model
ApplicationSubmissionError.yaml

The following error types are commonly used. From v0.6.0 each one also carries version response headers, declared in Headers.VersionResponse or Headers.VersionResponseAuth, or, for BadRequest, inline with Headers.VersionLifecycle. CG-Version-Served reports the full protocol version the response was served in. It is optional on 401 and 403 responses, because authentication can run before a version is selected, and on BadRequest, because the version errors share its status. The optional Vary, Deprecation, Sunset and Link headers come from Headers.VersionLifecycle. See Versioning and the Versions section of the specification.

alias Unauthorized = Error &
Http.UnauthorizedResponse &
Headers.VersionResponseAuth;
alias Forbidden = Error & Http.ForbiddenResponse & Headers.VersionResponseAuth;
alias NotFound = Error & Http.NotFoundResponse & Headers.VersionResponse;
// Any other 400, such as a body that fails validation in the requested version.
// The served version is optional because version errors share this status.
alias BadRequest = Error &
Http.BadRequestResponse & {
/** The full protocol version this response was served in, such as `0.5.1`
*
* Sent on every 400 except the version errors.
*/
@Http.header("CG-Version-Served")
cgVersionServed?: Types.protocolVersion;
...Headers.VersionLifecycle;
};

From v0.6.0, a request whose CG-Version value can’t be served gets one of four version errors. They carry no version response headers. Their schemas are on the Versioning page.

// Status codes live on these aliases, not the models, so they stay out of the
// JSON Schemas. Version errors carry no version response headers (ADR 0032).
alias VersionMalformed = VersionMalformedError & Http.BadRequestResponse;
alias VersionConflict = VersionConflictError & Http.BadRequestResponse;
alias VersionUnsupported = VersionUnsupportedError & Http.Response<406>;
alias VersionRetired = VersionRetiredError & Http.Response<406>;

Use WithVersionErrors<R> to add the version errors, and BadRequest, to a route’s responses:

// Adds the version errors to a route's responses from protocol version 0.6.0.
// Docs go on the variants: a union-level doc would replace every response description.
union WithVersionErrors<R> {
response: R,
/** The request is invalid, or its `CG-Version` value is malformed or was sent more than once */
@Versioning.added(CommonGrants.Versions.v0_6)
badRequest: VersionMalformed | VersionConflict | BadRequest,
/** The requested version isn't served by this server or this route */
@Versioning.added(CommonGrants.Versions.v0_6)
notAcceptable: VersionUnsupported | VersionRetired,
}