Skip to content

Commit 649159e

Browse files
committed
Work around limitations of TypeScript’s “instanceof” for Errors
1 parent 7f67643 commit 649159e

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strontium",
3-
"version": "2.2.3",
3+
"version": "2.2.4",
44
"description": "Strontium is a TypeScript toolkit for High Performance API servers built for Production not Projects.",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",

src/errors/http/HTTPError.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ export abstract class HTTPError extends StrontiumError {
1717
errorMessage: this.externalMessage,
1818
}
1919
}
20+
21+
public static isHTTPError(e: any): e is HTTPError {
22+
return (
23+
e !== undefined &&
24+
typeof e.statusCode === "number" &&
25+
typeof e.toResponseBody === "function"
26+
)
27+
}
2028
}

src/http/drivers/FastifyServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class FastifyServer implements Process {
189189
} catch (e) {
190190
// Detect Input Validation issues.
191191
// Any other errors will be thrown directly if they are HTTPError compatible or 500 and logged if not.
192-
if (e instanceof HTTPError) {
192+
if (HTTPError.isHTTPError(e)) {
193193
response.code(e.statusCode)
194194
return e.toResponseBody()
195195
} else {

0 commit comments

Comments
 (0)