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 |
Formats
Section titled “Formats”A JSON example of this model.
{ "status": 400, "message": "Error", "errors": [ null ]}The JSON Schema for this model.
$schema: https://json-schema.org/draft/2020-12/schema$id: Error.yamltype: objectproperties: status: type: integer minimum: -2147483648 maximum: 2147483647 examples: - 400 message: type: string examples: - Error description: Human-readable error message errors: type: array items: {} description: List of errorsrequired: - status - message - errorsdescription: A non-2xx response schemaThe TypeSpec code for this model.
/** A standard error response schema, used to create custom error responses * * @example - How to use this model to create custom error response schemas * * ``` * import "@typespec/http" * * alias Unauthorized = Error & Http.UnauthorizedResponse * * // From protocol version 0.6.0, also carry the version response headers * alias VersionedUnauthorized = Error & * Http.UnauthorizedResponse & * Headers.VersionResponseAuth * ``` */@error@doc("A non-2xx response schema")@Versioning.added(CommonGrants.Versions.v0_1)model Error { @example(400) status: int32;
/** Human-readable error message */ @example("Error") message: string;
/** List of errors */ errors: Array<unknown>;}The Python code for this model.
class Error(DefaultResponse): """A standard error response schema, used to create custom error responses."""
status: int = Field( default=400, description="The HTTP status code", examples=[400], ) message: str = Field( default="Error", description="Human-readable error message", examples=["Error"], ) errors: list[Any] = Field( ..., description="List of errors", )The TypeScript code for this model.
export const ErrorSchema = z.object({ /** HTTP status code */ status: z.number().int(),
/** Human-readable error message */ message: z.string(),
/** List of errors */ errors: z.array(z.unknown()),});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")@getop getTest(): OkT<TestModel> | Error;Changelog
Section titled “Changelog”| Version | Changes | Schema |
|---|---|---|
| 0.1.0 |
| Error.yaml |
ApplicationSubmissionError
Section titled “ApplicationSubmissionError”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 |
Formats
Section titled “Formats”A JSON example of this model.
{ "status": 400, "message": "Application submission failed due to validation errors", "errors": [ { "field": "formA.name", "message": "Name is required" } ]}The JSON Schema for this model.
$schema: https://json-schema.org/draft/2020-12/schema$id: ApplicationSubmissionError.yamltype: objectproperties: status: type: number const: 400 examples: - 400required: - statusallOf: - $ref: Error.yamlunevaluatedProperties: not: {}examples: - status: 400 message: Application submission failed due to validation errors errors: - field: formA.name message: Name is requireddescription: A failure to submit an application due to validation errorsThe TypeSpec code for this model.
@example(#{ status: 400, message: "Application submission failed due to validation errors", errors: #[#{ field: "formA.name", message: "Name is required" }],})@Versioning.added(CommonGrants.Versions.v0_2)@doc("A failure to submit an application due to validation errors")model ApplicationSubmissionError extends Error { @example(400) status: 400;}The TypeScript code for this model.
export const ApplicationSubmissionErrorSchema = ErrorSchema.extend({ status: z.literal(400),});Changelog
Section titled “Changelog”| Version | Changes | Schema |
|---|---|---|
| 0.2.0 |
| ApplicationSubmissionError.yaml |
Common Error Types
Section titled “Common Error Types”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; };Version errors
Section titled “Version errors”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,}