@ledgerhq/errors 6.10.1-yolo.0 → 6.10.2-nightly.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/helpers.ts CHANGED
@@ -13,24 +13,34 @@ export const addCustomErrorDeserializer = (
13
13
  deserializers[name] = deserializer;
14
14
  };
15
15
 
16
- export type CustomErrorFunc = (
17
- message?: string,
18
- fields?: { [key: string]: any },
19
- options?: any
20
- ) => void;
16
+ export interface LedgerErrorConstructor<F extends { [key: string]: unknown }>
17
+ extends ErrorConstructor {
18
+ new (message?: string, fields?: F, options?: any): Error;
19
+ (message?: string, fields?: F, options?: any): Error;
20
+ readonly prototype: Error;
21
+ }
21
22
 
22
- export const createCustomErrorClass = (name: string): CustomErrorFunc => {
23
+ export const createCustomErrorClass = <
24
+ F extends { [key: string]: unknown },
25
+ T extends LedgerErrorConstructor<F>
26
+ >(
27
+ name: string
28
+ ): T => {
23
29
  class CustomErrorClass extends Error {
24
30
  cause?: Error;
25
- constructor(message, fields, options) {
31
+ constructor(message?: string, fields?: F, options?: any) {
26
32
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
27
33
  // @ts-ignore
28
34
  super(message || name, options);
29
35
  // Set the prototype explicitly. See https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
30
36
  Object.setPrototypeOf(this, CustomErrorClass.prototype);
31
37
  this.name = name;
32
- for (const k in fields) {
33
- this[k] = fields[k];
38
+ if (fields) {
39
+ for (const k in fields) {
40
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
41
+ // @ts-ignore
42
+ this[k] = fields[k];
43
+ }
34
44
  }
35
45
  if (isObject(options) && "cause" in options && !("cause" in this)) {
36
46
  // .cause was specified but the superconstructor
@@ -46,9 +56,7 @@ export const createCustomErrorClass = (name: string): CustomErrorFunc => {
46
56
 
47
57
  errorClasses[name] = CustomErrorClass;
48
58
 
49
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
50
- // @ts-ignore
51
- return CustomErrorClass;
59
+ return CustomErrorClass as unknown as T;
52
60
  };
53
61
 
54
62
  function isObject(value) {
@@ -106,7 +114,9 @@ export const deserializeError = (object: any): Error => {
106
114
  };
107
115
 
108
116
  // inspired from https://github.com/sindresorhus/serialize-error/blob/master/index.js
109
- export const serializeError = (value: any): undefined | To | string => {
117
+ export const serializeError = (
118
+ value: undefined | To | string | (() => unknown)
119
+ ): undefined | To | string => {
110
120
  if (!value) return value;
111
121
  if (typeof value === "object") {
112
122
  return destroyCircular(value, []);
@@ -124,7 +134,7 @@ interface To {
124
134
  }
125
135
 
126
136
  // https://www.npmjs.com/package/destroy-circular
127
- function destroyCircular(from: any, seen: any[]): To {
137
+ function destroyCircular(from: To, seen: Array<To>): To {
128
138
  const to: To = {};
129
139
  seen.push(from);
130
140
  for (const key of Object.keys(from)) {
package/src/index.test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AmountRequired } from "./index";
1
+ import { AmountRequired, CurrencyNotSupported } from "./index";
2
2
 
3
3
  function functionA() {
4
4
  throw new AmountRequired();
@@ -35,9 +35,12 @@ describe("custom errors", () => {
35
35
 
36
36
  test("error with custom fields", () => {
37
37
  try {
38
- throw new AmountRequired("YO", { foo: 42 });
38
+ throw new CurrencyNotSupported("YO", { currencyName: "foo" });
39
39
  } catch (e: any) {
40
- expect(e.foo).toEqual(42);
40
+ expect(
41
+ // FIXME it's not yet the good type here
42
+ e instanceof CurrencyNotSupported && (e as any).currencyName
43
+ ).toEqual("foo");
41
44
  }
42
45
  });
43
46
 
@@ -66,4 +69,9 @@ describe("custom errors", () => {
66
69
  AmountRequired
67
70
  );
68
71
  });
72
+
73
+ test("error is instance of Error", () => {
74
+ const error: Error = new AmountRequired();
75
+ expect(error instanceof Error).toBeTruthy();
76
+ });
69
77
  });
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  deserializeError,
4
4
  createCustomErrorClass,
5
5
  addCustomErrorDeserializer,
6
+ LedgerErrorConstructor,
6
7
  } from "./helpers";
7
8
 
8
9
  export {
@@ -25,9 +26,10 @@ export const CantOpenDevice = createCustomErrorClass("CantOpenDevice");
25
26
  export const CashAddrNotSupported = createCustomErrorClass(
26
27
  "CashAddrNotSupported"
27
28
  );
28
- export const CurrencyNotSupported = createCustomErrorClass(
29
- "CurrencyNotSupported"
30
- );
29
+ export const CurrencyNotSupported = createCustomErrorClass<
30
+ { currencyName: string },
31
+ LedgerErrorConstructor<{ currencyName: string }>
32
+ >("CurrencyNotSupported");
31
33
  export const DeviceAppVerifyNotSupported = createCustomErrorClass(
32
34
  "DeviceAppVerifyNotSupported"
33
35
  );
@@ -111,6 +113,7 @@ export const ManagerUninstallBTCDep = createCustomErrorClass(
111
113
  "ManagerUninstallBTCDep"
112
114
  );
113
115
  export const NetworkDown = createCustomErrorClass("NetworkDown");
116
+ export const NetworkError = createCustomErrorClass("NetworkError");
114
117
  export const NoAddressesFound = createCustomErrorClass("NoAddressesFound");
115
118
  export const NotEnoughBalance = createCustomErrorClass("NotEnoughBalance");
116
119
  export const NotEnoughBalanceToDelegate = createCustomErrorClass(
@@ -218,6 +221,8 @@ export const FirmwareOrAppUpdateRequired = createCustomErrorClass(
218
221
  "FirmwareOrAppUpdateRequired"
219
222
  );
220
223
 
224
+ export const LanguageNotFound = createCustomErrorClass("LanguageNotFound");
225
+
221
226
  // db stuff, no need to translate
222
227
  export const NoDBPathGiven = createCustomErrorClass("NoDBPathGiven");
223
228
  export const DBWrongPassword = createCustomErrorClass("DBWrongPassword");
@@ -227,13 +232,17 @@ export const DBNotReset = createCustomErrorClass("DBNotReset");
227
232
  * TransportError is used for any generic transport errors.
228
233
  * e.g. Error thrown when data received by exchanges are incorrect or if exchanged failed to communicate with the device for various reason.
229
234
  */
230
- export function TransportError(message: string, id: string): void {
231
- this.name = "TransportError";
232
- this.message = message;
233
- this.stack = new Error().stack;
234
- this.id = id;
235
+ export class TransportError extends Error {
236
+ id: string;
237
+ constructor(message: string, id: string) {
238
+ const name = "TransportError";
239
+ super(message || name);
240
+ this.name = name;
241
+ this.message = message;
242
+ this.stack = new Error().stack;
243
+ this.id = id;
244
+ }
235
245
  }
236
- TransportError.prototype = new Error();
237
246
 
238
247
  addCustomErrorDeserializer(
239
248
  "TransportError",