@chubbyts/chubbyts-throwable-to-error 2.3.0 → 2.3.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # chubbyts-throwable-to-error
2
2
 
3
- [![CI](https://github.com/chubbyts/chubbyts-throwable-to-error/workflows/CI/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-throwable-to-error/actions?query=workflow%3ACI)
3
+ [![CI](https://github.com/chubbyts/chubbyts-throwable-to-error/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-throwable-to-error/actions/workflows/ci.yml)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-throwable-to-error/badge.svg?branch=master)](https://coveralls.io/github/chubbyts/chubbyts-throwable-to-error?branch=master)
5
5
  [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchubbyts%2Fchubbyts-throwable-to-error%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/chubbyts/chubbyts-throwable-to-error/master)
6
6
  [![npm-version](https://img.shields.io/npm/v/@chubbyts/chubbyts-throwable-to-error.svg)](https://www.npmjs.com/package/@chubbyts/chubbyts-throwable-to-error)
@@ -30,7 +30,7 @@ It converts any throwable into an Error.
30
30
  Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-throwable-to-error][1].
31
31
 
32
32
  ```ts
33
- npm i @chubbyts/chubbyts-throwable-to-error@^2.3.0
33
+ npm i @chubbyts/chubbyts-throwable-to-error@^2.3.1
34
34
  ```
35
35
 
36
36
  ## Usage
@@ -1,10 +1,23 @@
1
+ const MAX_MESSAGE_LENGTH = 2048;
2
+ const truncate = (message) => message.length > MAX_MESSAGE_LENGTH ? `${message.substring(0, MAX_MESSAGE_LENGTH)}...` : message;
3
+ const throwableToMessage = (e) => {
4
+ try {
5
+ // JSON.stringify / String may invoke user-defined toJSON/toString/getters, which can throw,
6
+ // and JSON.stringify throws on circular references and returns undefined for some inputs.
7
+ const message = typeof e === 'object' && e !== null ? JSON.stringify(e) : String(e);
8
+ return message ?? '[unserializable]';
9
+ }
10
+ catch {
11
+ return '[unserializable]';
12
+ }
13
+ };
1
14
  export const throwableToError = (e) => {
2
15
  if (e instanceof Error) {
3
16
  return e;
4
17
  }
5
- const error = new Error(typeof e === 'object' ? JSON.stringify(e) : String(e));
18
+ const error = new Error(truncate(throwableToMessage(e)));
6
19
  // oxlint-disable-next-line functional/immutable-data
7
- error.name = typeof e;
20
+ error.name = e === null ? 'null' : typeof e;
8
21
  // oxlint-disable-next-line functional/immutable-data
9
22
  error.stack = undefined;
10
23
  return error;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chubbyts/chubbyts-throwable-to-error",
3
3
  "type": "module",
4
- "version": "2.3.0",
4
+ "version": "2.3.1",
5
5
  "description": "It converts any throwable into an Error.",
6
6
  "keywords": [
7
7
  "chubbyts",
@@ -10,7 +10,10 @@
10
10
  ],
11
11
  "author": "Dominik Zogg",
12
12
  "license": "MIT",
13
- "repository": "chubbyts/chubbyts-throwable-to-error",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "chubbyts/chubbyts-throwable-to-error"
16
+ },
14
17
  "prettier": {
15
18
  "printWidth": 120,
16
19
  "tabWidth": 2,
@@ -34,9 +37,9 @@
34
37
  "@chubbyts/chubbyts-oxlint": "^1.0.0",
35
38
  "@stryker-mutator/core": "^9.6.1",
36
39
  "@stryker-mutator/vitest-runner": "^9.6.1",
37
- "@types/node": "^26.1.1",
40
+ "@types/node": "^26.1.2",
38
41
  "@vitest/coverage-v8": "^4.1.10",
39
- "oxlint": "^1.75.0",
42
+ "oxlint": "^1.77.0",
40
43
  "prettier": "^3.9.6",
41
44
  "typescript": "^7.0.2",
42
45
  "vitest": "^4.1.10"