From d37eb8e8a99685c24d60364f978c6f34ecf1eac9 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Thu, 13 Sep 2018 12:01:10 +0100 Subject: [PATCH] lib/errors: Improve typings by extending Error class for predicates When using the predicate functions in bluebird `.catch`es from typescript, the compiler would complain that the predicates do not accept a function which takes an error. Because these are specific errors, I've extended the base `Error` class, and added the extra fields we expect. Change-type: patch Signed-off-by: Cameron Diver --- src/lib/errors.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/lib/errors.ts b/src/lib/errors.ts index 95e75974..62afee3c 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -3,15 +3,25 @@ import TypedError = require('typed-error'); import { checkInt } from './validation'; -export function NotFoundError(err: { statusCode?: string }): boolean { +// To keep the bluebird typings happy, we need to accept +// an error, and in this case, it would also contain a status code +interface StatusCodeError extends Error { + statusCode?: string; +} + +interface CodedSysError extends Error { + code?: string; +} + +export function NotFoundError(err: StatusCodeError): boolean { return checkInt(err.statusCode) === 404; } -export function ENOENT(err: { code: string, [key: string]: any }): boolean { +export function ENOENT(err: CodedSysError): boolean { return err.code === 'ENOENT'; } -export function EEXIST(err: { code: string, [key: string]: any }): boolean { +export function EEXIST(err: CodedSysError): boolean { return err.code === 'EEXIST'; }