Skip to content

Commit 4fb457a

Browse files
authored
fix: incorrect error types (#222)
* fix: incorrect error types * add tsd assertions
1 parent e5ed98d commit 4fb457a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as busboy from "busboy";
22
import { FastifyPlugin } from "fastify";
33
import { Readable } from 'stream';
4-
import { FastifyError } from "fastify-error";
4+
import { FastifyErrorConstructor } from "fastify-error";
55

66
type MultipartHandler = (
77
field: string,
@@ -41,12 +41,12 @@ interface MultipartValue<T> {
4141
}
4242

4343
interface MultipartErrors {
44-
PartsLimitError: FastifyError,
45-
FilesLimitError: FastifyError,
46-
FieldsLimitError: FastifyError,
47-
PrototypeViolationError: FastifyError,
48-
InvalidMultipartContentTypeError: FastifyError,
49-
RequestFileTooLargeError: FastifyError
44+
PartsLimitError: FastifyErrorConstructor,
45+
FilesLimitError: FastifyErrorConstructor,
46+
FieldsLimitError: FastifyErrorConstructor,
47+
PrototypeViolationError: FastifyErrorConstructor,
48+
InvalidMultipartContentTypeError: FastifyErrorConstructor,
49+
RequestFileTooLargeError: FastifyErrorConstructor
5050
}
5151

5252
declare module "fastify" {

test/index.test-d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as util from 'util'
55
import { pipeline } from 'stream'
66
import * as fs from 'fs'
77
import { expectError, expectType } from 'tsd'
8+
import { FastifyErrorConstructor } from "fastify-error"
89

910
const pump = util.promisify(pipeline)
1011

@@ -141,6 +142,21 @@ const runServer = async () => {
141142
// access all errors
142143
app.post('/upload/files', async function (req, reply) {
143144
const { FilesLimitError } = app.multipartErrors
145+
146+
expectType<FastifyErrorConstructor>(app.multipartErrors.FieldsLimitError);
147+
expectType<FastifyErrorConstructor>(app.multipartErrors.FilesLimitError);
148+
expectType<FastifyErrorConstructor>(app.multipartErrors.InvalidMultipartContentTypeError);
149+
expectType<FastifyErrorConstructor>(app.multipartErrors.PartsLimitError);
150+
expectType<FastifyErrorConstructor>(app.multipartErrors.PrototypeViolationError);
151+
expectType<FastifyErrorConstructor>(app.multipartErrors.RequestFileTooLargeError);
152+
153+
// test instanceof Error
154+
const a = new FilesLimitError();
155+
if (a instanceof FilesLimitError) {
156+
console.log("FilesLimitError occurred.");
157+
}
158+
159+
reply.send();
144160
})
145161

146162
await app.ready()

0 commit comments

Comments
 (0)