Skip to content

Commit 155f41c

Browse files
committed
feat: extend IConvertibleToPartialErrorInfo with detail field.
- Adds field to the Partial error info interface and sanity checks the type.
1 parent 9300ca9 commit 155f41c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/common/lib/types/errorinfo.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export interface IConvertibleToErrorInfo {
2626
message: string;
2727
code: number;
2828
statusCode: number;
29+
detail?: Record<string, string>;
2930
}
3031

3132
export interface IConvertibleToPartialErrorInfo {
3233
message: string;
3334
code: number | null;
3435
statusCode?: number;
36+
detail?: Record<string, string>;
3537
}
3638

3739
export default class ErrorInfo extends Error implements IPartialErrorInfo, API.ErrorInfo {
@@ -57,11 +59,11 @@ export default class ErrorInfo extends Error implements IPartialErrorInfo, API.E
5759
}
5860

5961
static fromValues(values: IConvertibleToErrorInfo): ErrorInfo {
60-
const { message, code, statusCode } = values;
61-
if (typeof message !== 'string' || typeof code !== 'number' || typeof statusCode !== 'number') {
62+
const { message, code, statusCode, detail } = values;
63+
if (typeof message !== 'string' || typeof code !== 'number' || typeof statusCode !== 'number' || (!Utils.isNil(detail) && typeof detail !== 'object')) {
6264
throw new Error('ErrorInfo.fromValues(): invalid values: ' + Platform.Config.inspect(values));
6365
}
64-
const result = Object.assign(new ErrorInfo(message, code, statusCode), values);
66+
const result = Object.assign(new ErrorInfo(message, code, statusCode, undefined, detail), values);
6567
if (result.code && !result.href) {
6668
result.href = 'https://help.ably.io/error/' + result.code;
6769
}
@@ -76,30 +78,32 @@ export class PartialErrorInfo extends Error implements IPartialErrorInfo {
7678
href?: string;
7779
detail?: Record<string, string>;
7880

79-
constructor(message: string, code: number | null, statusCode?: number, cause?: ErrorInfo | PartialErrorInfo) {
81+
constructor(message: string, code: number | null, statusCode?: number, cause?: ErrorInfo | PartialErrorInfo, detail?: Record<string, string>) {
8082
super(message);
8183
if (typeof Object.setPrototypeOf !== 'undefined') {
8284
Object.setPrototypeOf(this, PartialErrorInfo.prototype);
8385
}
8486
this.code = code;
8587
this.statusCode = statusCode;
8688
this.cause = cause;
89+
this.detail = detail;
8790
}
8891

8992
toString(): string {
9093
return toString(this);
9194
}
9295

9396
static fromValues(values: IConvertibleToPartialErrorInfo): PartialErrorInfo {
94-
const { message, code, statusCode } = values;
97+
const { message, code, statusCode, detail } = values;
9598
if (
9699
typeof message !== 'string' ||
97100
(!Utils.isNil(code) && typeof code !== 'number') ||
98-
(!Utils.isNil(statusCode) && typeof statusCode !== 'number')
101+
(!Utils.isNil(statusCode) && typeof statusCode !== 'number') ||
102+
(!Utils.isNil(detail) && typeof detail !== 'object')
99103
) {
100104
throw new Error('PartialErrorInfo.fromValues(): invalid values: ' + Platform.Config.inspect(values));
101105
}
102-
const result = Object.assign(new PartialErrorInfo(message, code, statusCode), values);
106+
const result = Object.assign(new PartialErrorInfo(message, code, statusCode, undefined, detail), values);
103107
if (result.code && !result.href) {
104108
result.href = 'https://help.ably.io/error/' + result.code;
105109
}

0 commit comments

Comments
 (0)