@exodus/errors 2.2.0 → 3.0.1

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,9 +3,17 @@
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
- ## [2.2.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/errors@2.1.1...@exodus/errors@2.2.0) (2025-05-21)
6
+ ## [3.0.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/errors@3.0.0...@exodus/errors@3.0.1) (2025-07-08)
7
7
 
8
- - refactor: pass through safe-strings (#12479)
8
+ **Note:** Version bump only for package @exodus/errors
9
+
10
+ ## [3.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/errors@2.1.1...@exodus/errors@3.0.0) (2025-05-16)
11
+
12
+ ### ⚠ BREAKING CHANGES
13
+
14
+ - un-export hints whitelist, pass through safe-strings (#12479)
15
+
16
+ - refactor!: un-export hints whitelist, pass through safe-strings (#12479)
9
17
 
10
18
  ## [2.1.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/errors@2.1.0...@exodus/errors@2.1.1) (2025-05-08)
11
19
 
package/README.md CHANGED
@@ -3,33 +3,65 @@
3
3
  ## Usage
4
4
 
5
5
  ```javascript
6
- import { sanitizeErrorMessage, parseStackTrace } from '@exodus/errors'
6
+ import { SafeError } from '@exodus/errors'
7
7
 
8
8
  try {
9
- // the devil's work
9
+ throw new Error('Something went wrong')
10
10
  } catch (e) {
11
- console.error(sanitizeErrorMessage(e.message))
12
- sendToErrorTrackingServer(parseStackTrace(e))
11
+ const safeError = SafeError.from(e)
12
+
13
+ // It's now safe to report or log the error, even to a remote server.
14
+ console.error({
15
+ name: safeError.name, // Sanitized error name.
16
+ code: safeError.code, // Optional error code (if present).
17
+ hint: safeError.hint, // Optional error hint (if present).
18
+ stack: safeError.stack, // Sanitized stack trace.
19
+ timestamp: safeError.timestamp, // When the error occurred.
20
+ })
13
21
  }
14
22
  ```
15
23
 
24
+ ## FAQ
25
+
26
+ ### Why using SafeError instead of built-in Errors?
27
+
28
+ In large codebases, errors can be thrown from anywhere, making it impossible to audit every error message for sensitive information. A single error containing sensitive data could potentially expose user information. Centralizing error handling with `SafeError` makes it possible to enforce security and consistency across the board, by ensuring:
29
+
30
+ 1. **Controlled Error Flow**: All errors go through a single, well-tested sanitization layer before they hit error tracking systems.
31
+ 2. **Enforceable Security**: Error handling can be owned through codeowners and covered by tests, so nothing slips through unnoticed.
32
+
33
+ In addition to enforcing these practices, `SafeError` includes a few key design decisions that make it safer and more reliable than native Error objects:
34
+
35
+ 1. **Message Sanitization**: The `message` property from built-in Errors is intentionally omitted as it often contains sensitive information. Instead, a `hint` property is used that contains only sanitized, non-sensitive information.
36
+ 2. **Native Stack Parsing**: The library uses the [`Error.prepareStackTrace` API](https://v8.dev/docs/stack-trace-api) to parse stack traces, providing consistent and reliable stack trace information across different JavaScript environments.
37
+ 3. **Immutability**: Once created, a `SafeError` instance cannot be modified, preventing tampering with error data.
38
+ 4. **Safe Serialization**: The `toJSON` method ensures safe serialization for logging or sending to error tracking services.
39
+
40
+ ### Why not redacting Error messages?
41
+
42
+ Parsing/sanitization of error messages is unreliable and the cost of failure is potential loss of user funds and permanent reputation damage.
43
+
44
+ ### Why not use the built-in `.stack` Error property?
45
+
46
+ Unfortunately, the built-in `.stack` property is mutable and outside of our control. Instead, we use the `Error.prepareStackTrace` API, which enables us to make sure we access the actual call stack and not a cached `err.stack` value that may have already been consumed and modified. We then parse it into a structured format that we can safely sanitize and control. This approach provides consistent, reliable stack traces across different environments (currently supporting both V8 and Hermes).
47
+
16
48
  ## Troubleshooting
17
49
 
18
- ### `parseStackTrace` returns undefined stack trace?
50
+ ### A SafeError instance returns `undefined` stack trace?
19
51
 
20
- That likely means that something accesses `error.stack` _before_ the `parseStackTrace` had a chance to apply the custom handler. This could be React, a high-level error handler, or any other framework. `error.stack` is computed only on the first access, so the custom handler (`prepareStackTrace`) won’t be called on subsequent attempts (see the `stack.test.js` for a quick demo).
52
+ That likely means that something accesses `error.stack` _before_ the Safe Error constructor had a chance to apply the custom `Error.prepareStackTrace` handler. This could be React, a high-level error handler, or any other framework. `error.stack` is computed only on the first access, so the custom handler won’t be called on subsequent attempts (see the `stack.test.js` for a quick demo).
21
53
 
22
54
  If you can identify the exact place where `.stack` is accessed, consider capturing the stack trace explicitly like this:
23
55
 
24
56
  ```javascript
25
- import { parseStackTrace, captureStackTrace } from '@exodus/errors'
57
+ import { captureStackTrace, SafeError } from '@exodus/errors'
26
58
 
27
59
  try {
28
60
  // the devil's work
29
61
  } catch (e) {
30
62
  captureStackTrace(e)
31
63
  void e.stack // Intentionally access the property to "break" it here.
32
- sendToErrorTrackingServer(parseStackTrace(e))
33
- // 🎉 Congrats — you just (hopefully) saved hours of debugging! `parseStackTrace` now works because the stack was captured explicitly above.
64
+ SafeError.from(e).stack // A non-empty string!
65
+ // 🎉 Congrats — you just (hopefully) saved hours of debugging! The custom stack trace parsing logic now works because the stack was captured explicitly above.
34
66
  }
35
67
  ```
@@ -12,16 +12,16 @@ declare const commonErrors: readonly [{
12
12
  readonly normalized: "Cannot convert undefined to object";
13
13
  }, {
14
14
  readonly pattern: RegExp;
15
- readonly normalized: "Cannot read property/properties of null";
15
+ readonly normalized: "Cannot read properties of null";
16
16
  }, {
17
17
  readonly pattern: RegExp;
18
- readonly normalized: "Cannot read property/properties of undefined";
18
+ readonly normalized: "Cannot read properties of undefined";
19
19
  }, {
20
20
  readonly pattern: RegExp;
21
- readonly normalized: "Cannot set property/properties of null";
21
+ readonly normalized: "Cannot set properties of null";
22
22
  }, {
23
23
  readonly pattern: RegExp;
24
- readonly normalized: "Cannot set property/properties of undefined";
24
+ readonly normalized: "Cannot set properties of undefined";
25
25
  }, {
26
26
  readonly pattern: RegExp;
27
27
  readonly normalized: "Cannot access property of undefined";
@@ -17,19 +17,19 @@ const commonErrors = [
17
17
  },
18
18
  {
19
19
  pattern: /cannot read (property|properties).* of null/iu,
20
- normalized: 'Cannot read property/properties of null',
20
+ normalized: 'Cannot read properties of null',
21
21
  },
22
22
  {
23
23
  pattern: /cannot read (property|properties).* of undefined/iu,
24
- normalized: 'Cannot read property/properties of undefined',
24
+ normalized: 'Cannot read properties of undefined',
25
25
  },
26
26
  {
27
27
  pattern: /cannot set (property|properties).* of null/iu,
28
- normalized: 'Cannot set property/properties of null',
28
+ normalized: 'Cannot set properties of null',
29
29
  },
30
30
  {
31
31
  pattern: /cannot set (property|properties).* of undefined/iu,
32
- normalized: 'Cannot set property/properties of undefined',
32
+ normalized: 'Cannot set properties of undefined',
33
33
  },
34
34
  {
35
35
  pattern: /undefined is not an object \(evaluating '.*'\)/iu,
@@ -20,24 +20,6 @@ type UnknownError = Error & {
20
20
  declare const FACTORY_SYMBOL: unique symbol;
21
21
  export declare class SafeError {
22
22
  #private;
23
- static readonly hints: {
24
- readonly __proto__: null;
25
- readonly broadcastTx: {
26
- readonly general: "broadcastTx";
27
- readonly other: "otherErr:broadcastTx";
28
- readonly retry: "retry:broadcastTx";
29
- };
30
- readonly getNftArguments: {
31
- readonly general: "getNftArguments";
32
- };
33
- readonly ethCall: {
34
- readonly general: "ethCall";
35
- readonly executionReverted: "ethCall:executionReverted";
36
- };
37
- readonly safeError: {
38
- readonly failedToParse: "failed to parse error";
39
- };
40
- };
41
23
  static from<T extends UnknownError>(err: T): SafeError;
42
24
  get name(): "Error" | "AssertionError" | "TypeError" | "RangeError" | "UnknownError" | "SafeErrorFailedToParse" | "TimeoutError";
43
25
  get code(): SafeCode | undefined;
package/lib/safe-error.js CHANGED
@@ -16,24 +16,6 @@ const SAFE_NAMES_SET = makeReadonlySet([
16
16
  'SafeErrorFailedToParse',
17
17
  'TimeoutError',
18
18
  ]);
19
- const safeHints = {
20
- __proto__: null,
21
- broadcastTx: {
22
- general: 'broadcastTx',
23
- other: 'otherErr:broadcastTx',
24
- retry: 'retry:broadcastTx',
25
- },
26
- getNftArguments: {
27
- general: 'getNftArguments',
28
- },
29
- ethCall: {
30
- general: 'ethCall',
31
- executionReverted: 'ethCall:executionReverted',
32
- },
33
- safeError: {
34
- failedToParse: 'failed to parse error',
35
- },
36
- };
37
19
  function isSafeName(value) {
38
20
  return SAFE_NAMES_SET.has(value);
39
21
  }
@@ -50,7 +32,6 @@ function getSafeString(str) {
50
32
  }
51
33
  const FACTORY_SYMBOL = Symbol('SafeError');
52
34
  export class SafeError {
53
- static hints = safeHints;
54
35
  static from(err) {
55
36
  let safeName;
56
37
  let safeCode;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@exodus/errors",
3
3
  "type": "module",
4
- "version": "2.2.0",
4
+ "version": "3.0.1",
5
5
  "description": "Utilities for error handling in client code, such as sanitization",
6
6
  "author": "Exodus Movement, Inc.",
7
7
  "repository": {
@@ -24,14 +24,14 @@
24
24
  "CHANGELOG.md"
25
25
  ],
26
26
  "scripts": {
27
- "build": "run -T tsc --build tsconfig.build.json",
27
+ "build": "run -T tsc -p tsconfig.build.json",
28
28
  "prepublishOnly": "yarn run -T build --scope @exodus/errors",
29
29
  "clean": "run -T tsc --build --clean",
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
37
  "@exodus/safe-string": "^1.0.0",
@@ -44,5 +44,5 @@
44
44
  "publishConfig": {
45
45
  "access": "public"
46
46
  },
47
- "gitHead": "30ebd4bfc064662078e5819e335a3d658f00bd1b"
47
+ "gitHead": "7fd991f460540c1396fc68391393ee690269fa7f"
48
48
  }