Skip to content

Commit 500d466

Browse files
author
Shogun
committed
feat: Extract errors to another module.
1 parent 73eb5d6 commit 500d466

File tree

3 files changed

+8
-117
lines changed

3 files changed

+8
-117
lines changed

src/errors.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { INTERNAL_SERVER_ERROR } from 'http-status-codes'
66
import statuses from 'statuses'
77
import { addAdditionalProperties, GenericObject, NodeError, serializeError } from './properties'
88

9-
export * from './errors'
109
export { addAdditionalProperties, GenericObject } from './properties'
1110

1211
export interface FastifyDecoratedRequest extends FastifyRequest {

test/index.spec.ts

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fastify, { FastifyInstance, RegisterOptions } from 'fastify'
22
import { IncomingMessage, Server, ServerResponse } from 'http'
3-
import { BadGateway as DefaultBadGateway } from 'http-errors'
3+
import createError, { BadGateway } from 'http-errors'
44
import { BAD_GATEWAY, INTERNAL_SERVER_ERROR, NOT_FOUND } from 'http-status-codes'
5-
import fastifyerrorProperties, { BadGateway, handleErrors, NotFound } from '../src'
5+
import fastifyerrorProperties, { handleErrors } from '../src'
66

77
type Callback = () => void
88

@@ -11,21 +11,23 @@ let server: FastifyInstance | null
1111
function defaultRoutes(instance: FastifyInstance, _options: unknown, done: Callback): void {
1212
instance.get('/bad-gateway', {
1313
async handler(): Promise<void> {
14-
throw new DefaultBadGateway('This was the error message.')
14+
throw new BadGateway('This was the error message.')
1515
}
1616
})
1717

1818
instance.get('/headers', {
1919
async handler(): Promise<void> {
20-
const error = new NotFound('This was the error message.', { headers: { 'X-Custom-Header': 'Custom-Value' } })
20+
const error = createError(NOT_FOUND, 'This was the error message.', {
21+
headers: { 'X-Custom-Header': 'Custom-Value' }
22+
})
2123

2224
throw error
2325
}
2426
})
2527

2628
instance.get('/properties', {
2729
async handler(): Promise<void> {
28-
const error = new BadGateway('This was the error message.', { id: 1 })
30+
const error = createError(BAD_GATEWAY, 'This was the error message.', { id: 1 })
2931

3032
throw error
3133
}
@@ -42,7 +44,7 @@ function defaultRoutes(instance: FastifyInstance, _options: unknown, done: Callb
4244

4345
instance.get('/weird-code', {
4446
async handler(): Promise<void> {
45-
const error = new DefaultBadGateway('This was the error message.')
47+
const error = new BadGateway('This was the error message.')
4648
error.statusCode = 10
4749

4850
throw error
@@ -239,33 +241,3 @@ describe('Using standalone error handling', function(): void {
239241
})
240242
})
241243
})
242-
243-
describe('Extending http-errors named constructors', function(): void {
244-
it('should support (message, properties) syntax', function(): void {
245-
const error404byName = new NotFound('message', { id: 1 })
246-
expect(error404byName).toBeInstanceOf(Error)
247-
expect(error404byName.statusCode).toEqual(404)
248-
expect(error404byName.message).toEqual('message')
249-
expect(error404byName.id).toEqual(1)
250-
251-
const error502byName = new BadGateway('message', { id: 1 })
252-
expect(error502byName).toBeInstanceOf(Error)
253-
expect(error502byName.statusCode).toEqual(502)
254-
expect(error502byName.message).toEqual('message')
255-
expect(error502byName.id).toEqual(1)
256-
})
257-
258-
it('should support (message, properties) syntax', function(): void {
259-
const error404byName = new NotFound({ id: 1 })
260-
expect(error404byName).toBeInstanceOf(Error)
261-
expect(error404byName.statusCode).toEqual(404)
262-
expect(error404byName.message).toEqual('Not Found')
263-
expect(error404byName.id).toEqual(1)
264-
265-
const error502byName = new BadGateway({ id: 1 })
266-
expect(error502byName).toBeInstanceOf(Error)
267-
expect(error502byName.statusCode).toEqual(502)
268-
expect(error502byName.message).toEqual('Bad Gateway')
269-
expect(error502byName.id).toEqual(1)
270-
})
271-
})

0 commit comments

Comments
 (0)