Errors
Error in middleware
const isAuthedResolver = resolver.use(({ ctx, next }) => {
if (!ctx.user)
throw new PtsqError({
code: 'UNAUTHORIZED',
message: 'Must be logged in!',
});
return next({
ctx: {
user: ctx.user,
},
});
});
The PtsqError
is the only error that is caught by the PTSQ.
The adapter such as Express
or Koa
then responds with that error code and message, if specified.
You can also specify cause, which can be something like a schema validation error.
throw new PtsqError({
code: 'BAD_REQUEST',
message: 'User already exists.',
cause: databaseInsert.error,
});
Error codes
PtsqError code | Description | HTTP status |
---|---|---|
PTSQ_VALIDATION_FAILED | The arguments schema validation failed. | 400 |
PTSQ_BODY_PARSE_FAILED | The server cannot parse the request body or the body is in bad format. | 400 |
PTSQ_BAD_ROUTE_TYPE | The route is query and should be mutation and vice versa. | 400 |
BAD_REQUEST | The server cannot or will not process the request due to something that is perceived to be a client error. | 400 |
UNAUTHORIZED | The client request has not been completed because it lacks valid authentication credentials for the requested resource. | 401 |
FORBIDDEN | The server was unauthorized to access a PTSQ endpoint. | 403 |
NOT_FOUND | The server cannot find the requested PTSQ endpoint. | 404 |
METHOD_NOT_SUPPORTED | The server knows the request method, but the target resource doesn't support this method. | 405 |
TIMEOUT | The server would like to shut down this unused connection. | 408 |
CONFLICT | The server request resource conflict with the current state of the target resource. | 409 |
PRECONDITION_FAILED | Access to the target resource has been denied. | 412 |
PAYLOAD_TOO_LARGE | Request entity is larger than limits defined by server. | 413 |
UNPROCESSABLE_CONTENT | The server understands the request method, and the request entity is correct, but the server was unable to process it. | 422 |
TOO_MANY_REQUESTS | The rate limit has been exceeded or too many requests are being sent to the server. | 429 |
CLIENT_CLOSED_REQUEST | Access to the resource has been denied. | 499 |
INTERNAL_SERVER_ERROR | The output validation failed or an unspecified error occurred. | 500 |