Skip to content

Commit

Permalink
Export error codes (#4266)
Browse files Browse the repository at this point in the history
  • Loading branch information
fitiskin committed Oct 7, 2022
1 parent da7471f commit 6511ef4
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 93 deletions.
260 changes: 200 additions & 60 deletions docs/Reference/Errors.md
Expand Up @@ -83,146 +83,286 @@ Some things to consider in your custom error handler:
### Fastify Error Codes
<a id="fastify-error-codes"></a>

#### FST_ERR_BAD_URL
<a id="FST_ERR_BAD_URL"></a>

The router received an invalid url.

<a name="FST_ERR_DUPLICATED_ROUTE"></a>
#### FST_ERR_DUPLICATED_ROUTE

The HTTP method already has a registered controller for that URL
You can access `errorCodes` for mapping:
```js
// ESM
import { errorCodes } from 'fastify'

// CommonJs
const errorCodes = require('fastify').errorCodes
```

For example:
```js
const Fastify = require('./fastify')

// Instantiate the framework
const fastify = Fastify({
logger: true
})

// Declare a route
fastify.get('/', function (request, reply) {
reply.code('bad status code').send({ hello: 'world' })
})

fastify.setErrorHandler(function (error, request, reply) {
if (error instanceof Fastify.errorCodes.FST_ERR_BAD_STATUS_CODE) {
// Log error
this.log.error(error)
// Send error response
reply.status(500).send({ ok: false })
}
})

// Run the server!
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
// Server is now listening on ${address}
})
```

#### FST_ERR_NOT_FOUND
<a id="FST_ERR_NOT_FOUND"></a>

404 Not Found.

<a name="FST_ERR_CTP_ALREADY_PRESENT"></a>
#### FST_ERR_CTP_ALREADY_PRESENT
<a id="FST_ERR_CTP_ALREADY_PRESENT"></a>

The parser for this content type was already registered.

#### FST_ERR_CTP_BODY_TOO_LARGE
<a id="FST_ERR_CTP_BODY_TOO_LARGE"></a>

The request body is larger than the provided limit.
#### FST_ERR_CTP_INVALID_TYPE
<a id="FST_ERR_CTP_INVALID_TYPE"></a>

This setting can be defined in the Fastify server instance:
[`bodyLimit`](./Server.md#bodylimit)
The `Content-Type` should be a string.

#### FST_ERR_CTP_EMPTY_TYPE
<a id="FST_ERR_CTP_EMPTY_TYPE"></a>

The content type cannot be an empty string.

#### FST_ERR_CTP_INVALID_CONTENT_LENGTH
<a id="FST_ERR_CTP_INVALID_CONTENT_LENGTH"></a>

Request body size did not match Content-Length.

#### FST_ERR_CTP_INVALID_HANDLER
<a id="FST_ERR_CTP_INVALID_HANDLER"></a>

An invalid handler was passed for the content type.

#### FST_ERR_CTP_INVALID_PARSE_TYPE
<a id="FST_ERR_CTP_INVALID_PARSE_TYPE"></a>

The provided parse type is not supported. Accepted values are `string` or
`buffer`.

#### FST_ERR_CTP_BODY_TOO_LARGE
<a id="FST_ERR_CTP_BODY_TOO_LARGE"></a>

The request body is larger than the provided limit.

This setting can be defined in the Fastify server instance:
[`bodyLimit`](./Server.md#bodylimit)

#### FST_ERR_CTP_INVALID_MEDIA_TYPE
<a id="FST_ERR_CTP_INVALID_MEDIA_TYPE"></a>

The received media type is not supported (i.e. there is no suitable
`Content-Type` parser for it).

#### FST_ERR_CTP_INVALID_PARSE_TYPE
<a id="FST_ERR_CTP_INVALID_PARSE_TYPE"></a>
#### FST_ERR_CTP_INVALID_CONTENT_LENGTH
<a id="FST_ERR_CTP_INVALID_CONTENT_LENGTH"></a>

The provided parse type is not supported. Accepted values are `string` or
`buffer`.
Request body size did not match `Content-Length`.

#### FST_ERR_CTP_INVALID_TYPE
<a id="FST_ERR_CTP_INVALID_TYPE"></a>
#### FST_ERR_CTP_EMPTY_JSON_BODY
<a id="FST_ERR_CTP_EMPTY_JSON_BODY"></a>

The `Content-Type` should be a string.
Body cannot be empty when content-type is set to `application/json`.

#### FST_ERR_DEC_ALREADY_PRESENT
<a id="FST_ERR_DEC_ALREADY_PRESENT"></a>

A decorator with the same name is already registered.

#### FST_ERR_DEC_DEPENDENCY_INVALID_TYPE
<a id="FST_ERR_DEC_DEPENDENCY_INVALID_TYPE"></a>

The dependencies of decorator must be of type `Array`.

#### FST_ERR_DEC_MISSING_DEPENDENCY
<a id="FST_ERR_DEC_MISSING_DEPENDENCY"></a>

The decorator cannot be registered due to a missing dependency.

#### FST_ERR_HOOK_INVALID_HANDLER
<a id="FST_ERR_HOOK_INVALID_HANDLER"></a>
#### FST_ERR_DEC_AFTER_START
<a id="FST_ERR_DEC_AFTER_START"></a>

The hook callback must be a function.
The decorator cannot be added after start.

#### FST_ERR_HOOK_INVALID_TYPE
<a id="FST_ERR_HOOK_INVALID_TYPE"></a>

The hook name must be a string.

#### FST_ERR_HOOK_INVALID_HANDLER
<a id="FST_ERR_HOOK_INVALID_HANDLER"></a>

The hook callback must be a function.

#### FST_ERR_MISSING_MIDDLEWARE
<a id="FST_ERR_MISSING_MIDDLEWARE"></a>

You must register a plugin for handling middlewares,
visit [`Middleware`](./Middleware.md) for more info.

<a name="FST_ERR_HOOK_TIMEOUT"></a>
#### FST_ERR_HOOK_TIMEOUT

A callback for a hook timed out

#### FST_ERR_LOG_INVALID_DESTINATION
<a id="FST_ERR_LOG_INVALID_DESTINATION"></a>

The logger accepts either a `'stream'` or a `'file'` as the destination.

#### FST_ERR_PROMISE_NOT_FULFILLED
<a id="FST_ERR_PROMISE_NOT_FULFILLED"></a>
#### FST_ERR_REP_INVALID_PAYLOAD_TYPE
<a id="FST_ERR_REP_INVALID_PAYLOAD_TYPE"></a>

A promise may not be fulfilled with 'undefined' when statusCode is not 204.
Reply payload can be either a `string` or a `Buffer`.

#### FST_ERR_REP_ALREADY_SENT
<a id="FST_ERR_REP_ALREADY_SENT"></a>

A response was already sent.

#### FST_ERR_REP_INVALID_PAYLOAD_TYPE
<a id="FST_ERR_REP_INVALID_PAYLOAD_TYPE"></a>
#### FST_ERR_REP_SENT_VALUE
<a id="FST_ERR_REP_SENT_VALUE"></a>

Reply payload can be either a `string` or a `Buffer`.
The only possible value for `reply.sent` is `true`.

#### FST_ERR_SCH_ALREADY_PRESENT
<a id="FST_ERR_SCH_ALREADY_PRESENT"></a>
#### FST_ERR_SEND_INSIDE_ONERR
<a id="FST_ERR_SEND_INSIDE_ONERR"></a>

A schema with the same `$id` already exists.
You cannot use `send` inside the `onError` hook.

#### FST_ERR_SEND_UNDEFINED_ERR
<a id="FST_ERR_SEND_UNDEFINED_ERR"></a>

Undefined error has occurred.

#### FST_ERR_BAD_STATUS_CODE
<a id="FST_ERR_BAD_STATUS_CODE"></a>

Called `reply` with an invalid status code.

#### FST_ERR_BAD_TRAILER_NAME
<a id="FST_ERR_BAD_TRAILER_NAME"></a>

Called `reply.trailer` with an invalid header name.

#### FST_ERR_BAD_TRAILER_VALUE
<a id="FST_ERR_BAD_TRAILER_VALUE"></a>

Called `reply.trailer` with an invalid type. Expected a function.

#### FST_ERR_MISSING_SERIALIZATION_FN
<a id="FST_ERR_MISSING_SERIALIZATION_FN"></a>

Missing serialization function.

#### FST_ERR_REQ_INVALID_VALIDATION_INVOCATION
<a id="FST_ERR_REQ_INVALID_VALIDATION_INVOCATION"></a>

Invalid validation invocation. Missing validation function for
HTTP part nor schema provided.

#### FST_ERR_SCH_MISSING_ID
<a id="FST_ERR_SCH_MISSING_ID"></a>

The schema provided does not have `$id` property.

#### FST_ERR_SCH_SERIALIZATION_BUILD
<a id="FST_ERR_SCH_SERIALIZATION_BUILD"></a>
#### FST_ERR_SCH_ALREADY_PRESENT
<a id="FST_ERR_SCH_ALREADY_PRESENT"></a>

The JSON schema provided for serialization of a route response is not valid.
A schema with the same `$id` already exists.

#### FST_ERR_SCH_DUPLICATE
<a id="FST_ERR_SCH_DUPLICATE"></a>

Schema with the same `$id` already present!

#### FST_ERR_SCH_VALIDATION_BUILD
<a id="FST_ERR_SCH_VALIDATION_BUILD"></a>

The JSON schema provided for validation to a route is not valid.

#### FST_ERR_SEND_INSIDE_ONERR
<a id="FST_ERR_SEND_INSIDE_ONERR"></a>
#### FST_ERR_SCH_SERIALIZATION_BUILD
<a id="FST_ERR_SCH_SERIALIZATION_BUILD"></a>

You cannot use `send` inside the `onError` hook.
The JSON schema provided for serialization of a route response is not valid.

#### FST_ERR_SEND_UNDEFINED_ERR
<a id="FST_ERR_SEND_UNDEFINED_ERR"></a>
#### FST_ERR_HTTP2_INVALID_VERSION
<a id="FST_ERR_HTTP2_INVALID_VERSION"></a>

Undefined error has occurred.
HTTP2 is available only from node >= 8.8.1.

<a name="FST_ERR_PLUGIN_NOT_VALID"></a>
#### FST_ERR_PLUGIN_NOT_VALID
#### FST_ERR_INIT_OPTS_INVALID
<a id="FST_ERR_INIT_OPTS_INVALID"></a>

Plugin must be a function or a promise.
Invalid initialization options.

<a name="FST_ERR_PLUGIN_TIMEOUT"></a>
#### FST_ERR_PLUGIN_TIMEOUT
#### FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE
<a id="FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE"></a>

Plugin did not start in time. Default timeout (in millis): `10000`
Cannot set forceCloseConnections to `idle` as your HTTP server
does not support `closeIdleConnections` method.

<a name="FST_ERR_HOOK_TIMEOUT"></a>
#### FST_ERR_HOOK_TIMEOUT
<a name="FST_ERR_DUPLICATED_ROUTE"></a>
#### FST_ERR_DUPLICATED_ROUTE

A callback for a hook timed out
The HTTP method already has a registered controller for that URL

#### FST_ERR_BAD_URL
<a id="FST_ERR_BAD_URL"></a>

The router received an invalid url.

#### FST_ERR_DEFAULT_ROUTE_INVALID_TYPE
<a id="FST_ERR_DEFAULT_ROUTE_INVALID_TYPE"></a>

The `defaultRoute` type should be a function.

#### FST_ERR_INVALID_URL
<a id="FST_ERR_INVALID_URL"></a>

URL must be a string.

#### FST_ERR_REOPENED_CLOSE_SERVER
<a id="FST_ERR_REOPENED_CLOSE_SERVER"></a>

Fastify has already been closed and cannot be reopened.

#### FST_ERR_REOPENED_SERVER
<a id="FST_ERR_REOPENED_SERVER"></a>

Fastify is already listening.

#### FST_ERR_PLUGIN_VERSION_MISMATCH
<a id="FST_ERR_PLUGIN_VERSION_MISMATCH"></a>

Installed Fastify plugin mismatched expected version.

<a name="FST_ERR_PLUGIN_CALLBACK_NOT_FN"></a>
#### FST_ERR_PLUGIN_CALLBACK_NOT_FN

Callback for a hook is not a function (mapped directly from `avvio`)

<a name="FST_ERR_PLUGIN_NOT_VALID"></a>
#### FST_ERR_PLUGIN_NOT_VALID

Plugin must be a function or a promise.

<a name="FST_ERR_ROOT_PLG_BOOTED"></a>
#### FST_ERR_ROOT_PLG_BOOTED
Expand All @@ -234,7 +374,7 @@ Root plugin has already booted (mapped directly from `avvio`)

Impossible to load plugin because the parent (mapped directly from `avvio`)

<a name="FST_ERR_PLUGIN_CALLBACK_NOT_FN"></a>
#### FST_ERR_PLUGIN_CALLBACK_NOT_FN
<a name="FST_ERR_PLUGIN_TIMEOUT"></a>
#### FST_ERR_PLUGIN_TIMEOUT

Callback for a hook is not a function (mapped directly from `avvio`)
Plugin did not start in time. Default timeout (in millis): `10000`
6 changes: 6 additions & 0 deletions fastify.d.ts
Expand Up @@ -20,6 +20,7 @@ import { SerializerCompiler } from '@fastify/fast-json-stringify-compiler'
import { FastifySchema } from './types/schema'
import { FastifyContextConfig } from './types/context'
import { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
import { FastifyErrorCodes } from './types/errors'

/**
* Fastify factory function for the standard fastify http, https, or http2 server instance.
Expand Down Expand Up @@ -61,6 +62,10 @@ declare function fastify<
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
>(opts?: FastifyServerOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>

declare namespace fastify {
export const errorCodes: FastifyErrorCodes;
}

export default fastify

export type FastifyHttp2SecureOptions<
Expand Down Expand Up @@ -206,4 +211,5 @@ export { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaul
export * from './types/hooks'
export { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
export { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
export { FastifyErrorCodes } from './types/errors'
export { fastify }

0 comments on commit 6511ef4

Please sign in to comment.