Repository: sindresorhus/serialize-error (js)

Issue:
change in v5: `const {inspect} = require('util');` can lead to web bundle bloat
This was added in v5 for `deserializeError`:

```ts
const {inspect} = require('util');
```

This adds about 8KB to web bundles, which can be significant, especially in projects not using `deserializeError`.

Possible solutions:

* Not worry about 8KB and tell developers to use v4 if bundle size is of concern.
* Separate `serializeError` into a separate file that can be imported without the `deserializeError` code.
* Separate `deserializeError` into a new module.
* Publish code with ES6-style `import`, add `sideEffects: false` to the `package.json` to enable tree shaking in the module.
* Other ideas?

Pull request description:
Make it work better in the browser
Fixes #26

Patch:
diff --git a/index.js b/index.js
index c66d810..658411e 100644
--- a/index.js
+++ b/index.js
@@ -1,11 +1,21 @@
 'use strict';
-const {inspect} = require('util');
 
 class NonError extends Error {
 	constructor(message) {
-		super(inspect(message));
+		super(NonError._prepareSuperMessage(message));
 		this.name = 'NonError';
-		Error.captureStackTrace(this, NonError);
+
+		if (Error.captureStackTrace) {
+			Error.captureStackTrace(this, NonError);
+		}
+	}
+
+	static _prepareSuperMessage(message) {
+		try {
+			return JSON.stringify(message);
+		} catch (_) {
+			return String(message);
+		}
 	}
 }
 


Test patch:
diff --git a/test.js b/test.js
index 75b50c7..6bb512d 100644
--- a/test.js
+++ b/test.js
@@ -1,6 +1,13 @@
 import test from 'ava';
 import {serializeError, deserializeError} from '.';
 
+function deserializeNonError(t, value) {
+	const deserialized = deserializeError(value);
+	t.true(deserialized instanceof Error);
+	t.is(deserialized.constructor.name, 'NonError');
+	t.is(deserialized.message, JSON.stringify(value));
+}
+
 test('main', t => {
 	const serialized = serializeError(new Error('foo'));
 	const keys = Object.keys(serialized);
@@ -115,27 +122,29 @@ test('should handle top-level null values', t => {
 });
 
 test('should deserialize null', t => {
-	const deserialized = deserializeError(null);
-	t.true(deserialized instanceof Error);
-	t.is(deserialized.message, 'null');
+	deserializeNonError(t, null);
 });
 
 test('should deserialize number', t => {
-	const deserialized = deserializeError(1);
-	t.true(deserialized instanceof Error);
-	t.is(deserialized.message, '1');
+	deserializeNonError(t, 1);
 });
 
-test('should deserialize error', t => {
-	const deserialized = deserializeError(new Error('test'));
-	t.true(deserialized instanceof Error);
-	t.is(deserialized.message, 'test');
+test('should deserialize boolean', t => {
+	deserializeNonError(t, true);
+});
+
+test('should deserialize string', t => {
+	deserializeNonError(t, '123');
 });
 
 test('should deserialize array', t => {
-	const deserialized = deserializeError([1]);
+	deserializeNonError(t, [1]);
+});
+
+test('should deserialize error', t => {
+	const deserialized = deserializeError(new Error('test'));
 	t.true(deserialized instanceof Error);
-	t.is(deserialized.message, '[ 1 ]');
+	t.is(deserialized.message, 'test');
 });
 
 test('should deserialize and preserve existing properties', t => {