@redthreadlabs/tracelog-client 1.3.0 → 1.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.
@@ -42,7 +42,7 @@ class EventBuilder {
42
42
  }
43
43
  withError(err) {
44
44
  if (err instanceof Error) {
45
- this._error = { message: err.message };
45
+ this._error = { message: withCode(err.message, err.code) };
46
46
  if (err.name)
47
47
  this._error.type = err.name;
48
48
  if (err.stack)
@@ -52,7 +52,17 @@ class EventBuilder {
52
52
  this._error = { message: err };
53
53
  }
54
54
  else if (err != null) {
55
- this._error = { message: String(err) };
55
+ // Error-like plain objects (e.g. ShareDB op errors: {code, message}).
56
+ // String(err) would yield the useless '[object Object]'.
57
+ const message = typeof err.message === 'string' && err.message.length > 0
58
+ ? err.message
59
+ : safeJson(err);
60
+ this._error = { message: withCode(message, err.code) };
61
+ if (typeof err.name === 'string' && err.name.length > 0) {
62
+ this._error.type = err.name;
63
+ }
64
+ if (typeof err.stack === 'string')
65
+ this._error.stack = err.stack;
56
66
  }
57
67
  return this;
58
68
  }
@@ -77,3 +87,28 @@ class EventBuilder {
77
87
  }
78
88
  }
79
89
  exports.EventBuilder = EventBuilder;
90
+ // Append an error code (ShareDB and Node errors carry one) to the message.
91
+ function withCode(message, code) {
92
+ if (typeof code === 'string' || typeof code === 'number') {
93
+ return `${message} (code: ${code})`;
94
+ }
95
+ return message;
96
+ }
97
+ // Bounded JSON fallback for objects with no usable message property.
98
+ function safeJson(obj) {
99
+ try {
100
+ const json = JSON.stringify(obj);
101
+ if (typeof json === 'string') {
102
+ return json.length > 500 ? json.slice(0, 500) + '…' : json;
103
+ }
104
+ }
105
+ catch {
106
+ // Circular or otherwise unserializable
107
+ }
108
+ try {
109
+ return `[unserializable error: keys=${Object.keys(obj).join(',')}]`;
110
+ }
111
+ catch {
112
+ return Object.prototype.toString.call(obj);
113
+ }
114
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redthreadlabs/tracelog-client",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Lightweight logging client for tracelog — works in React Native and browsers",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",