Skip to content

Commit

Permalink
fix: errorHandler callback should utilize TypeProvider (#4989)
Browse files Browse the repository at this point in the history
* fix: errorHandler callback should utilize TypeProvider

* add typings test

* add missing import

* fix copy paste error

---------

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
muan and Uzlopak committed Aug 23, 2023
1 parent 007a307 commit 529c269
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 7 additions & 2 deletions test/types/instance.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import fastify, {
FastifyInstance,
RawReplyDefaultExpression,
RawRequestDefaultExpression,
RawServerDefault
RawServerDefault,
RouteGenericInterface
} from '../../fastify'
import { HookHandlerDoneFunction } from '../../types/hooks'
import { FastifyReply } from '../../types/reply'
Expand Down Expand Up @@ -257,9 +258,13 @@ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true },

expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as RawReplyDefaultExpression))

expectType<FastifyInstance>(fastify().get('/', {
expectType<FastifyInstance>(fastify().get<RouteGenericInterface, { contextKey: string }>('/', {
handler: () => {},
errorHandler: (error, request, reply) => {
expectAssignable<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectAssignable<{ contextKey: string }>(request.routeConfig)
expectAssignable<FastifyReply>(reply)
expectAssignable<void>(server.errorHandler(error, request, reply))
}
}))
Expand Down
27 changes: 26 additions & 1 deletion test/types/type-provider.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import fastify, {
HookHandlerDoneFunction,
FastifyRequest,
FastifyReply,
FastifyInstance
FastifyInstance,
FastifyError
} from '../../fastify'
import { expectAssignable, expectError, expectType } from 'tsd'
import { IncomingHttpHeaders } from 'http'
Expand Down Expand Up @@ -79,6 +80,14 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().get(
y: Type.Number(),
z: Type.Number()
})
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number>(request.body.x)
expectType<number>(request.body.y)
expectType<number>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down Expand Up @@ -108,6 +117,14 @@ expectAssignable(server.withTypeProvider<JsonSchemaToTsProvider>().get(
z: { type: 'boolean' }
}
} as const
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number | undefined>(request.body.x)
expectType<string | undefined>(request.body.y)
expectType<boolean | undefined>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down Expand Up @@ -135,6 +152,14 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().withTypeProvider<Jso
z: { type: 'boolean' }
}
} as const
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number | undefined>(request.body.x)
expectType<string | undefined>(request.body.y)
expectType<boolean | undefined>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down
7 changes: 6 additions & 1 deletion types/route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export interface RouteShorthandOptions<
version?: string;
constraints?: { [name: string]: any },
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
errorHandler?: (this: FastifyInstance, error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
errorHandler?: (
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
error: FastifyError,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
) => void;
childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
schemaErrorFormatter?: SchemaErrorFormatter;

Expand Down

0 comments on commit 529c269

Please sign in to comment.