HTTP server error handler in Typescript and Node.js
Trying changing the type of your error
variable from Error
to NodeJS.ErrnoException
. This type is an interface that extends the base Error
interface and adds the syscall
property, among others.
Source: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts#L401
export interface ErrnoException extends Error {
errno?: number;
code?: string;
path?: string;
syscall?: string;
stack?: string;
}
If you're using types, like @types/node
, the class you want to use is NodeJS.ErrnoException
. Saying use any
to solve the problem of lacking a type, is the same as saying if you want to stop punctures on a car, take off the wheels.
This is because not every Error object needs to have a syscall property.
You may be able to fix it changing this:
function onError(error: Error) {
// [ts] property syscall does not exist on error
if (error.syscall !== 'listen') {
throw error;
}
}
to this:
function onError(error: any) {
if (error.syscall !== 'listen') {
throw error;
}
}
See this issue comments for some background:
- https://github.com/angular/angularfire2/issues/666