Skip to content

Commit bf18412

Browse files
authored
Move createError utility to its own package (fastify#2339)
1 parent 1d4dcf2 commit bf18412

25 files changed

+255
-330
lines changed

docs/Plugins-Guide.md

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Fastify was built from the beginning to be an extremely modular system. We built
1212
- [How to handle encapsulation and distribution](#distribution)
1313
- [ESM support](#esm-support)
1414
- [Handle errors](#handle-errors)
15+
- [Custom errors](#custom-errors)
1516
- [Let's start!](#start)
1617

1718
<a name="register"></a>
@@ -319,6 +320,16 @@ fastify
319320
})
320321
```
321322

323+
<a name="custom-errors"></a>
324+
## Custom errors
325+
If your plugin needs to expose custom errors, you can easily generate consistent error objects across your codebase and plugins with the [`fastify-error`](https://github.com/fastify/fastify-error) module.
326+
327+
```js
328+
const createError = require('fastify-error')
329+
const CustomError = createError('ERROR_CODE', 'message')
330+
console.log(new CustomError())
331+
```
332+
322333
<a name="start"></a>
323334
## Let's start!
324335
Awesome, now you know everything you need to know about Fastify and its plugin system to start building your first plugin, and please if you do, tell us! We will add it to the [*ecosystem*](https://github.com/fastify/fastify#ecosystem) section of our documentation!

fastify.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { FastifyLoggerInstance, FastifyLoggerOptions } from './types/logger'
99
import { FastifyInstance } from './types/instance'
1010
import { FastifyServerFactory } from './types/serverFactory'
1111
import * as ajv from 'ajv'
12-
import { FastifyError } from './types/error'
12+
import { FastifyError } from 'fastify-error'
1313
import { FastifyReply } from './types/reply'
1414

1515
/**
@@ -126,7 +126,7 @@ export { FastifyContext } from './types/context'
126126
export { RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler } from './types/route'
127127
export * from './types/register'
128128
export { FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser } from './types/content-type-parser'
129-
export { FastifyError, ValidationResult } from './types/error'
129+
export { FastifyError, ValidationResult } from 'fastify-error'
130130
export { FastifySchema, FastifySchemaCompiler } from './types/schema'
131131
export { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
132132
export * from './types/hooks'

fastify.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ const build404 = require('./lib/fourOhFour')
4242
const getSecuredInitialConfig = require('./lib/initialConfigValidation')
4343
const { defaultInitOptions } = getSecuredInitialConfig
4444
const {
45-
codes: {
46-
FST_ERR_BAD_URL,
47-
FST_ERR_MISSING_MIDDLEWARE
48-
}
45+
FST_ERR_BAD_URL,
46+
FST_ERR_MISSING_MIDDLEWARE
4947
} = require('./lib/errors')
5048

5149
function fastify (options) {

lib/contentTypeParser.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ const {
1212
} = require('./symbols')
1313

1414
const {
15-
codes: {
16-
FST_ERR_CTP_INVALID_TYPE,
17-
FST_ERR_CTP_EMPTY_TYPE,
18-
FST_ERR_CTP_ALREADY_PRESENT,
19-
FST_ERR_CTP_INVALID_HANDLER,
20-
FST_ERR_CTP_INVALID_PARSE_TYPE,
21-
FST_ERR_CTP_BODY_TOO_LARGE,
22-
FST_ERR_CTP_INVALID_MEDIA_TYPE,
23-
FST_ERR_CTP_INVALID_CONTENT_LENGTH,
24-
FST_ERR_CTP_EMPTY_JSON_BODY
25-
}
15+
FST_ERR_CTP_INVALID_TYPE,
16+
FST_ERR_CTP_EMPTY_TYPE,
17+
FST_ERR_CTP_ALREADY_PRESENT,
18+
FST_ERR_CTP_INVALID_HANDLER,
19+
FST_ERR_CTP_INVALID_PARSE_TYPE,
20+
FST_ERR_CTP_BODY_TOO_LARGE,
21+
FST_ERR_CTP_INVALID_MEDIA_TYPE,
22+
FST_ERR_CTP_INVALID_CONTENT_LENGTH,
23+
FST_ERR_CTP_EMPTY_JSON_BODY
2624
} = require('./errors')
2725
const { emitWarning } = require('./warnings')
2826

lib/decorate.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ const {
99
} = require('./symbols.js')
1010

1111
const {
12-
codes: {
13-
FST_ERR_DEC_ALREADY_PRESENT,
14-
FST_ERR_DEC_MISSING_DEPENDENCY,
15-
FST_ERR_DEC_AFTER_START
16-
}
12+
FST_ERR_DEC_ALREADY_PRESENT,
13+
FST_ERR_DEC_MISSING_DEPENDENCY,
14+
FST_ERR_DEC_AFTER_START
1715
} = require('./errors')
1816

1917
function decorate (instance, name, fn, dependencies) {

0 commit comments

Comments
 (0)