mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-24 07:46:41 +00:00
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 <cameron@resin.io>
This commit is contained in:
parent
71dd2fc72e
commit
d37eb8e8a9
@ -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';
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user