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": 404, "message": "Not Found", "errors": [ "Resource not found" ]}
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 schema
The TypeSpec code for this model.
@error@doc("A non-2xx response schema")model Error { @example(400) status: int32;
/** Human-readable error message */ @example("Error") message: string;
/** List of errors */ errors: Array<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;
@summary("Get test")@doc("Get a test model")@getop getTest(): Ok<TestModel> | Error;
Common Error Types
Section titled “Common Error Types”The following error types are commonly used:
// 401 Unauthorized// User is not authorized to access the resourcealias Unauthorized = Error & Http.UnauthorizedResponse;
// 404 Not Found// The resource was not foundalias NotFound = Error & Http.NotFoundResponse;
// 400 Bad Request// The request was invalidalias BadRequest = Error & Http.BadRequestResponse;
// 403 Forbidden// The action is forbiddenalias Forbidden = Error & Http.ForbiddenResponse;