Skip to content

Commit 045b483

Browse files
committed
fix: add type declarations for errors
1 parent 7a1249f commit 045b483

12 files changed

+78
-77
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.husky/_
2-
packages/errors/**/*.d.*
32
packages/fetch-error-handler/**/*.d.*
43
packages/logger/**/*.d.*
54
packages/middleware-log-errors/**/*.d.*

jsconfig.build.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"removeComments": true
99
},
1010
"include": [
11-
"packages/errors/**/*.js",
1211
"packages/fetch-error-handler/**/*.js",
1312
"packages/logger/**/*.js",
1413
"packages/middleware-log-errors/**/*.js",

packages/errors/.npmignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
!*.d.ts
2-
!*.d.ts.map
31
CHANGELOG.md
42
docs
53
test

packages/errors/lib/base-error.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
/**
2-
* @typedef {object} ErrorStrictData
3-
* @property {string} [code]
4-
* A machine-readable error code which identifies the specific type of error.
5-
* @property {string} [message]
6-
* A human readable message which describes the error.
7-
* @property {Error | null} [cause]
8-
* The root cause error instance.
9-
*/
10-
11-
/**
12-
* @typedef {ErrorStrictData & Record<string, any>} ErrorData
2+
* @typedef {import('@dotcom-reliability-kit/errors').BaseErrorData} ErrorData
133
*/
144

155
/**
@@ -19,44 +9,39 @@ class BaseError extends Error {
199
/**
2010
* @override
2111
* @readonly
22-
* @public
23-
* @type {string}
12+
* @type {import('@dotcom-reliability-kit/errors').BaseError['name']}
2413
*/
2514
name = 'BaseError';
2615

2716
/**
2817
* Whether the error is operational.
2918
*
3019
* @readonly
31-
* @public
32-
* @type {boolean}
20+
* @type {import('@dotcom-reliability-kit/errors').BaseError['isOperational']}
3321
*/
3422
isOperational = false;
3523

3624
/**
3725
* A machine-readable error code which identifies the specific type of error.
3826
*
3927
* @readonly
40-
* @public
41-
* @type {string}
28+
* @type {import('@dotcom-reliability-kit/errors').BaseError['code']}
4229
*/
4330
code = BaseError.defaultCode;
4431

4532
/**
4633
* The root cause error instance.
4734
*
4835
* @readonly
49-
* @public
50-
* @type {Error | null}
36+
* @type {import('@dotcom-reliability-kit/errors').BaseError['cause']}
5137
*/
5238
cause = null;
5339

5440
/**
5541
* Additional error information.
5642
*
5743
* @readonly
58-
* @public
59-
* @type {{[key: string]: any}}
44+
* @type {import('@dotcom-reliability-kit/errors').BaseError['data']}
6045
*/
6146
data = {};
6247

@@ -134,13 +119,7 @@ class BaseError extends Error {
134119
static defaultMessage = 'An error occurred';
135120

136121
/**
137-
* Get whether an error object is marked as operational (it has a truthy `isOperational` property).
138-
*
139-
* @public
140-
* @param {Error} error
141-
* The error object to check.
142-
* @returns {boolean}
143-
* Returns whether the error is operational.
122+
* @type {(typeof import('@dotcom-reliability-kit/errors').BaseError)['isErrorMarkedAsOperational']}
144123
*/
145124
static isErrorMarkedAsOperational(error) {
146125
// @ts-ignore Error.prototype.isOperational does not exist, but it's OK to check in this

packages/errors/lib/data-store-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class DataStoreError extends OperationalError {
77
/**
88
* @override
99
* @readonly
10-
* @public
11-
* @type {string}
10+
* @type {import('@dotcom-reliability-kit/errors').DataStoreError['name']}
1211
*/
1312
name = 'DataStoreError';
1413
}

packages/errors/lib/http-error.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ const OperationalError = require('./operational-error');
1111
const STATUS_CODES = require('http').STATUS_CODES;
1212

1313
/**
14-
* @typedef {object} HttpErrorStrictData
15-
* @property {number} [statusCode]
16-
* An HTTP status code.
17-
*/
18-
19-
/**
20-
* @typedef {HttpErrorStrictData & OperationalError.OperationalErrorData} HttpErrorData
14+
* @typedef {import('@dotcom-reliability-kit/errors').HttpErrorData} ErrorData
2115
*/
2216

2317
/**
@@ -27,28 +21,24 @@ class HttpError extends OperationalError {
2721
/**
2822
* @override
2923
* @readonly
30-
* @public
31-
* @type {string}
24+
* @type {import('@dotcom-reliability-kit/errors').HttpError['name']}
3225
*/
3326
name = 'HttpError';
3427

3528
/**
3629
* @readonly
37-
* @public
38-
* @type {number}
30+
* @type {import('@dotcom-reliability-kit/errors').HttpError['statusCode']}
3931
*/
4032
statusCode;
4133

4234
/**
4335
* @readonly
44-
* @public
45-
* @type {string}
36+
* @type {import('@dotcom-reliability-kit/errors').HttpError['statusMessage']}
4637
*/
4738
statusMessage;
4839

4940
/**
50-
* @public
51-
* @type {number}
41+
* @type {import('@dotcom-reliability-kit/errors').HttpError['status']}
5242
*/
5343
get status() {
5444
return this.statusCode;
@@ -63,7 +53,7 @@ class HttpError extends OperationalError {
6353
* Create an error with error data.
6454
*
6555
* @overload
66-
* @param {HttpErrorData} data
56+
* @param {ErrorData} data
6757
* Additional error information.
6858
*/
6959
/**
@@ -72,7 +62,7 @@ class HttpError extends OperationalError {
7262
* @overload
7363
* @param {string} message
7464
* The error message.
75-
* @param {HttpErrorData} [data]
65+
* @param {ErrorData} [data]
7666
* Additional error information.
7767
*/
7868
/**
@@ -81,14 +71,14 @@ class HttpError extends OperationalError {
8171
* @overload
8272
* @param {number} status
8373
* The error HTTP status code.
84-
* @param {HttpErrorData} [data]
74+
* @param {ErrorData} [data]
8575
* Additional error information.
8676
*/
8777
/**
88-
* @param {string | number | HttpErrorData} [message]
78+
* @param {string | number | ErrorData} [message]
8979
* The error message if it's a string, the HTTP status code if it's a number, or full error
9080
* information if an object.
91-
* @param {HttpErrorData} [data]
81+
* @param {ErrorData} [data]
9282
* Additional error information if `message` is a string or number.
9383
*/
9484
constructor(message, data = {}) {

packages/errors/lib/operational-error.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
const BaseError = require('./base-error');
22

33
/**
4-
* @typedef {object} OperationalErrorStrictData
5-
* @property {string[]} [relatesToSystems]
6-
* An array of FT system codes which are related to this error.
7-
*/
8-
9-
/**
10-
* @typedef {OperationalErrorStrictData & BaseError.ErrorData} OperationalErrorData
4+
* @typedef {import('@dotcom-reliability-kit/errors').OperationalErrorData} ErrorData
115
*/
126

137
/**
@@ -17,8 +11,7 @@ class OperationalError extends BaseError {
1711
/**
1812
* @override
1913
* @readonly
20-
* @public
21-
* @type {string}
14+
* @type {import('@dotcom-reliability-kit/errors').OperationalError['name']}
2215
*/
2316
name = 'OperationalError';
2417

@@ -27,8 +20,7 @@ class OperationalError extends BaseError {
2720
*
2821
* @override
2922
* @readonly
30-
* @public
31-
* @type {boolean}
23+
* @type {import('@dotcom-reliability-kit/errors').OperationalError['isOperational']}
3224
*/
3325
isOperational = true;
3426

@@ -37,8 +29,7 @@ class OperationalError extends BaseError {
3729
* If this error is caused by one or more dependencies, include their system code here.
3830
*
3931
* @readonly
40-
* @public
41-
* @type {string[]}
32+
* @type {import('@dotcom-reliability-kit/errors').OperationalError['relatesToSystems']}
4233
*/
4334
relatesToSystems = [];
4435

@@ -51,7 +42,7 @@ class OperationalError extends BaseError {
5142
* Create an error with error data.
5243
*
5344
* @overload
54-
* @param {OperationalErrorData} data
45+
* @param {ErrorData} data
5546
* Additional error information.
5647
*/
5748
/**
@@ -60,13 +51,13 @@ class OperationalError extends BaseError {
6051
* @overload
6152
* @param {string} message
6253
* The error message.
63-
* @param {OperationalErrorData} [data]
54+
* @param {ErrorData} [data]
6455
* Additional error information.
6556
*/
6657
/**
67-
* @param {string | OperationalErrorData} [message]
58+
* @param {string | ErrorData} [message]
6859
* The error message if it's a string, or full error information if an object.
69-
* @param {OperationalErrorData} [data]
60+
* @param {ErrorData} [data]
7061
* Additional error information if `message` is a string.
7162
*/
7263
constructor(message, data = {}) {

packages/errors/lib/upstream-service-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class UpstreamServiceError extends HttpError {
77
/**
88
* @override
99
* @readonly
10-
* @public
11-
* @type {string}
10+
* @type {import('@dotcom-reliability-kit/errors').UpstreamServiceError['name']}
1211
*/
1312
name = 'UpstreamServiceError';
1413

packages/errors/lib/user-input-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class UserInputError extends HttpError {
77
/**
88
* @override
99
* @readonly
10-
* @public
11-
* @type {string}
10+
* @type {import('@dotcom-reliability-kit/errors').UserInputError['name']}
1211
*/
1312
name = 'UserInputError';
1413

packages/errors/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"node": "18.x || 20.x || 22.x",
1515
"npm": "8.x || 9.x || 10.x"
1616
},
17-
"main": "lib"
17+
"main": "lib/index.js",
18+
"types": "types/index.d.ts"
1819
}

packages/errors/types/index.d.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
declare module '@dotcom-reliability-kit/errors' {
2+
type BaseErrorData = {
3+
code?: string;
4+
message?: string;
5+
cause?: Error | null;
6+
} & { [key: string]: any };
7+
8+
type OperationalErrorData = { relatesToSystems?: string[] } & BaseErrorData;
9+
10+
type HttpErrorData = { statusCode?: number } & OperationalErrorData;
11+
12+
export class BaseError extends Error {
13+
constructor();
14+
constructor(data: BaseErrorData);
15+
constructor(message: string, data?: BaseErrorData);
16+
17+
public override readonly name: string;
18+
public readonly isOperational: boolean;
19+
public readonly code: string;
20+
public readonly cause: Error | null;
21+
public readonly data: { [key: string]: any };
22+
23+
public static isErrorMarkedAsOperational(error: Error): boolean;
24+
}
25+
26+
export class OperationalError extends BaseError {
27+
constructor();
28+
constructor(data: OperationalErrorData);
29+
constructor(message: string, data?: OperationalErrorData);
30+
31+
public readonly relatesToSystems: string[];
32+
}
33+
34+
export class HttpError extends OperationalError {
35+
constructor();
36+
constructor(data: HttpErrorData);
37+
constructor(message: string, data?: HttpErrorData);
38+
constructor(status: number, data?: HttpErrorData);
39+
40+
public readonly statusCode: number;
41+
public readonly statusMessage: string;
42+
public readonly status: number;
43+
}
44+
45+
export class DataStoreError extends OperationalError {}
46+
export class UpstreamServiceError extends HttpError {}
47+
export class UserInputError extends HttpError {}
48+
}

scripts/clean-generated-types.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env bash
22

3-
find ./packages/errors -name "*.d.ts*" | xargs -r rm
43
find ./packages/fetch-error-handler -name "*.d.ts*" | xargs -r rm
54
find ./packages/logger -name "*.d.ts*" | xargs -r rm
65
find ./packages/middleware-log-errors -name "*.d.ts*" | xargs -r rm

0 commit comments

Comments
 (0)