@exodus/errors 2.1.1 → 3.0.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/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/errors@2.1.1...@exodus/errors@3.0.0) (2025-05-16)
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ - un-export hints whitelist, pass through safe-strings (#12479)
11
+
12
+ - refactor!: un-export hints whitelist, pass through safe-strings (#12479)
13
+
6
14
  ## [2.1.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/errors@2.1.0...@exodus/errors@2.1.1) (2025-05-08)
7
15
 
8
16
  ### Bug Fixes
@@ -0,0 +1,40 @@
1
+ declare const commonErrors: readonly [{
2
+ readonly pattern: RegExp;
3
+ readonly normalized: "Value is not a function";
4
+ }, {
5
+ readonly pattern: RegExp;
6
+ readonly normalized: "Value is not a constructor";
7
+ }, {
8
+ readonly pattern: RegExp;
9
+ readonly normalized: "Cannot convert null to object";
10
+ }, {
11
+ readonly pattern: RegExp;
12
+ readonly normalized: "Cannot convert undefined to object";
13
+ }, {
14
+ readonly pattern: RegExp;
15
+ readonly normalized: "Cannot read property/properties of null";
16
+ }, {
17
+ readonly pattern: RegExp;
18
+ readonly normalized: "Cannot read property/properties of undefined";
19
+ }, {
20
+ readonly pattern: RegExp;
21
+ readonly normalized: "Cannot set property/properties of null";
22
+ }, {
23
+ readonly pattern: RegExp;
24
+ readonly normalized: "Cannot set property/properties of undefined";
25
+ }, {
26
+ readonly pattern: RegExp;
27
+ readonly normalized: "Cannot access property of undefined";
28
+ }, {
29
+ readonly pattern: RegExp;
30
+ readonly normalized: "Cannot access property of null";
31
+ }, {
32
+ readonly pattern: RegExp;
33
+ readonly normalized: "Unexpected token";
34
+ }, {
35
+ readonly pattern: RegExp;
36
+ readonly normalized: "Unexpected end of input";
37
+ }];
38
+ export type CommonErrorString = (typeof commonErrors)[number]['normalized'];
39
+ export declare function toCommonErrorMessage(errorMessage: string): CommonErrorString | undefined;
40
+ export {};
@@ -0,0 +1,59 @@
1
+ const commonErrors = [
2
+ {
3
+ pattern: /.* is not a function/iu,
4
+ normalized: 'Value is not a function',
5
+ },
6
+ {
7
+ pattern: /.* is not a constructor/iu,
8
+ normalized: 'Value is not a constructor',
9
+ },
10
+ {
11
+ pattern: /cannot convert null to object/iu,
12
+ normalized: 'Cannot convert null to object',
13
+ },
14
+ {
15
+ pattern: /cannot convert undefined to object/iu,
16
+ normalized: 'Cannot convert undefined to object',
17
+ },
18
+ {
19
+ pattern: /cannot read (property|properties).* of null/iu,
20
+ normalized: 'Cannot read property/properties of null',
21
+ },
22
+ {
23
+ pattern: /cannot read (property|properties).* of undefined/iu,
24
+ normalized: 'Cannot read property/properties of undefined',
25
+ },
26
+ {
27
+ pattern: /cannot set (property|properties).* of null/iu,
28
+ normalized: 'Cannot set property/properties of null',
29
+ },
30
+ {
31
+ pattern: /cannot set (property|properties).* of undefined/iu,
32
+ normalized: 'Cannot set property/properties of undefined',
33
+ },
34
+ {
35
+ pattern: /undefined is not an object \(evaluating '.*'\)/iu,
36
+ normalized: 'Cannot access property of undefined',
37
+ },
38
+ {
39
+ pattern: /null is not an object \(evaluating '.*'\)/iu,
40
+ normalized: 'Cannot access property of null',
41
+ },
42
+ {
43
+ pattern: /unexpected (token|character)/iu,
44
+ normalized: 'Unexpected token',
45
+ },
46
+ {
47
+ pattern: /unexpected end of( JSON)? input/iu,
48
+ normalized: 'Unexpected end of input',
49
+ },
50
+ ];
51
+ export function toCommonErrorMessage(errorMessage) {
52
+ if (!errorMessage)
53
+ return;
54
+ for (const { pattern, normalized } of commonErrors) {
55
+ if (pattern.test(errorMessage)) {
56
+ return normalized;
57
+ }
58
+ }
59
+ }
@@ -1,12 +1,18 @@
1
1
  import type { Frame } from './types.js';
2
+ import type { CommonErrorString } from './common-errors.js';
2
3
  type ReadonlySetValues<S> = S extends ReadonlySet<infer T> ? T : never;
3
4
  declare const SAFE_NAMES_SET: ReadonlySet<"Error" | "AssertionError" | "TypeError" | "RangeError" | "UnknownError" | "SafeErrorFailedToParse" | "TimeoutError">;
4
- declare const SAFE_HINTS_SET: ReadonlySet<"broadcastTx" | "otherErr:broadcastTx" | "retry:broadcastTx" | "getNftArguments" | "ethCall" | "ethCall:executionReverted" | "failed to parse error">;
5
+ type StaticAllowlistString = string & {
6
+ __branded_type: 'StaticAllowlistString';
7
+ };
8
+ type RuntimeSafeString = string & {
9
+ __branded_type: 'RuntimeSafeString';
10
+ };
5
11
  export type SafeName = ReadonlySetValues<typeof SAFE_NAMES_SET>;
6
- export type SafeHint = ReadonlySetValues<typeof SAFE_HINTS_SET>;
7
12
  export type SafeCode = string & {
8
13
  __branded_type: 'SafeCodeOutcome';
9
14
  };
15
+ export type SafeString = CommonErrorString | StaticAllowlistString | RuntimeSafeString;
10
16
  type UnknownError = Error & {
11
17
  hint?: unknown;
12
18
  code?: unknown;
@@ -14,28 +20,10 @@ type UnknownError = Error & {
14
20
  declare const FACTORY_SYMBOL: unique symbol;
15
21
  export declare class SafeError {
16
22
  #private;
17
- static readonly hints: {
18
- readonly __proto__: null;
19
- readonly broadcastTx: {
20
- readonly general: "broadcastTx";
21
- readonly other: "otherErr:broadcastTx";
22
- readonly retry: "retry:broadcastTx";
23
- };
24
- readonly getNftArguments: {
25
- readonly general: "getNftArguments";
26
- };
27
- readonly ethCall: {
28
- readonly general: "ethCall";
29
- readonly executionReverted: "ethCall:executionReverted";
30
- };
31
- readonly safeError: {
32
- readonly failedToParse: "failed to parse error";
33
- };
34
- };
35
23
  static from<T extends UnknownError>(err: T): SafeError;
36
24
  get name(): "Error" | "AssertionError" | "TypeError" | "RangeError" | "UnknownError" | "SafeErrorFailedToParse" | "TimeoutError";
37
25
  get code(): SafeCode | undefined;
38
- get hint(): "broadcastTx" | "otherErr:broadcastTx" | "retry:broadcastTx" | "getNftArguments" | "ethCall" | "ethCall:executionReverted" | "failed to parse error" | undefined;
26
+ get hint(): SafeString | undefined;
39
27
  get stackFrames(): {
40
28
  function?: string | null;
41
29
  method?: string | null;
@@ -52,14 +40,14 @@ export declare class SafeError {
52
40
  toJSON(): {
53
41
  name: SafeName;
54
42
  code?: SafeCode;
55
- hint?: SafeHint;
43
+ hint?: SafeString;
56
44
  stack?: string;
57
45
  timestamp: number;
58
46
  };
59
47
  constructor({ name, code, hint, stack, initSymbol, }: {
60
48
  name: SafeName;
61
49
  code?: SafeCode;
62
- hint?: SafeHint;
50
+ hint?: SafeString;
63
51
  stack?: Frame[];
64
52
  initSymbol: typeof FACTORY_SYMBOL;
65
53
  });
package/lib/safe-error.js CHANGED
@@ -1,6 +1,8 @@
1
+ import { getAllowlist, isSafe } from '@exodus/safe-string';
1
2
  import assert from 'minimalistic-assert';
2
3
  import parseStackTraceNatively, { stackFramesToString } from './stack.js';
3
4
  import { omitUndefined } from './utils.js';
5
+ import { toCommonErrorMessage } from './common-errors.js';
4
6
  function makeReadonlySet(values) {
5
7
  return new Set(values);
6
8
  }
@@ -14,42 +16,6 @@ const SAFE_NAMES_SET = makeReadonlySet([
14
16
  'SafeErrorFailedToParse',
15
17
  'TimeoutError',
16
18
  ]);
17
- const safeHints = {
18
- __proto__: null,
19
- broadcastTx: {
20
- general: 'broadcastTx',
21
- other: 'otherErr:broadcastTx',
22
- retry: 'retry:broadcastTx',
23
- },
24
- getNftArguments: {
25
- general: 'getNftArguments',
26
- },
27
- ethCall: {
28
- general: 'ethCall',
29
- executionReverted: 'ethCall:executionReverted',
30
- },
31
- safeError: {
32
- failedToParse: 'failed to parse error',
33
- },
34
- };
35
- const SAFE_HINTS_SET = makeReadonlySet(
36
- // @ts-expect-error doesn't like __proto__: null
37
- Object.values(safeHints).flatMap((safeHintCategory) => Object.values(safeHintCategory)));
38
- function getSafeHint(value) {
39
- if (typeof value !== 'string') {
40
- return undefined;
41
- }
42
- if (value === '') {
43
- return undefined;
44
- }
45
- if (isSafeHint(value)) {
46
- return value;
47
- }
48
- return [...SAFE_HINTS_SET].find((sh) => value.includes(sh));
49
- }
50
- function isSafeHint(value) {
51
- return SAFE_HINTS_SET.has(value);
52
- }
53
19
  function isSafeName(value) {
54
20
  return SAFE_NAMES_SET.has(value);
55
21
  }
@@ -59,9 +25,13 @@ function isExodusErrorCode(code) {
59
25
  function isSafeCode(value) {
60
26
  return SAFE_CODES_SET.has(value) || isExodusErrorCode(value);
61
27
  }
28
+ const staticAllowlist = getAllowlist();
29
+ function getSafeString(str) {
30
+ if (isSafe(str))
31
+ return str;
32
+ }
62
33
  const FACTORY_SYMBOL = Symbol('SafeError');
63
34
  export class SafeError {
64
- static hints = safeHints;
65
35
  static from(err) {
66
36
  let safeName;
67
37
  let safeCode;
@@ -71,7 +41,12 @@ export class SafeError {
71
41
  safeName = isSafeName(name) ? name : 'UnknownError';
72
42
  const safeCodeCandidate = `${code}`;
73
43
  safeCode = isSafeCode(safeCodeCandidate) ? safeCodeCandidate : undefined;
74
- safeHint = getSafeHint(hint) || getSafeHint(message);
44
+ const hintCandidates = [hint, message].filter((str) => typeof str === 'string');
45
+ safeHint =
46
+ // chicken sacrifice to TypeScript, otherwise would be hintCandidates.find((str) => isSafe(str))
47
+ hintCandidates.map((str) => getSafeString(str)).find(Boolean) ||
48
+ staticAllowlist.find((safePrefix) => hintCandidates.some((str) => str.startsWith(safePrefix))) ||
49
+ toCommonErrorMessage(message);
75
50
  const safeStack = parseStackTraceNatively(err);
76
51
  return new SafeError({
77
52
  name: safeName,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@exodus/errors",
3
3
  "type": "module",
4
- "version": "2.1.1",
4
+ "version": "3.0.0",
5
5
  "description": "Utilities for error handling in client code, such as sanitization",
6
6
  "author": "Exodus Movement, Inc.",
7
7
  "repository": {
@@ -30,10 +30,11 @@
30
30
  "lint": "run -T eslint .",
31
31
  "lint:fix": "yarn lint --fix",
32
32
  "test": "yarn run test:v8 && yarn run test:hermes",
33
- "test:v8": "run -T exodus-test --esbuild --jest src/__tests__/v8/*.test.ts",
34
- "test:hermes": "EXODUS_TEST_COVERAGE=0 run -T exodus-test --engine hermes:bundle --esbuild --jest src/__tests__/hermes/*.test.ts"
33
+ "test:v8": "run -T exodus-test --esbuild --jest src/__tests__/v8/*.test.ts src/__tests__/engine-agnostic/*.test.ts",
34
+ "test:hermes": "EXODUS_TEST_COVERAGE=0 run -T exodus-test --engine hermes:bundle --esbuild --jest src/__tests__/hermes/*.test.ts src/__tests__/engine-agnostic/*.test.ts"
35
35
  },
36
36
  "dependencies": {
37
+ "@exodus/safe-string": "^1.0.0",
37
38
  "minimalistic-assert": "^1.0.1"
38
39
  },
39
40
  "devDependencies": {
@@ -43,5 +44,5 @@
43
44
  "publishConfig": {
44
45
  "access": "public"
45
46
  },
46
- "gitHead": "5d915aed4f43c8b9377e87c902347aac778391d2"
47
+ "gitHead": "aca2b6b0dbe54c8b2b14b14412c81d0237b8d5ee"
47
48
  }