@steve31415/baselib 3.62.0 → 3.62.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.
Files changed (2) hide show
  1. package/dist/log-core.js +61 -2
  2. package/package.json +1 -1
package/dist/log-core.js CHANGED
@@ -14,8 +14,8 @@ const CODE_MAX = 100;
14
14
  /** An Error's `code` property when it is a primitive worth a column: a
15
15
  * string (capped) or a finite number. Anything else is dropped, since an
16
16
  * object there would mint `meta.error.code.*` keys from data. */
17
- function errorCode(err) {
18
- const code = err.code;
17
+ function errorCode(err, key = 'code') {
18
+ const code = readProp(err, key);
19
19
  if (typeof code === 'string')
20
20
  return truncate(code, CODE_MAX);
21
21
  if (typeof code === 'number' && Number.isFinite(code))
@@ -57,8 +57,67 @@ export function serializeError(err) {
57
57
  }
58
58
  return out;
59
59
  }
60
+ if (typeof err === 'object' && err !== null)
61
+ return serializeNonErrorObject(err);
60
62
  return { name: 'NonError', message: truncate(String(err), 2000) };
61
63
  }
64
+ /** A property read that a throwing getter cannot turn into a logging failure. */
65
+ function readProp(o, key) {
66
+ try {
67
+ return o[key];
68
+ }
69
+ catch {
70
+ return undefined;
71
+ }
72
+ }
73
+ function constructorName(o) {
74
+ const name = readProp(o, 'constructor')?.name;
75
+ return typeof name === 'string' && name !== '' && name !== 'Object' ? name : undefined;
76
+ }
77
+ /** A thrown or rejected object that is not an Error. The common case is an
78
+ * event: puppeteer-core's connect() rejects with the ws library's
79
+ * ErrorEvent, whose `type`, `message` and `error` are prototype getters, so
80
+ * String() gives "[object Object]" and JSON "{}" — which hid a Cloudflare
81
+ * 401 (radar task 6262f4fb, 2026-11-08). Read the conventional fields
82
+ * directly; fall back to JSON, then to the constructor's name. The name
83
+ * stays 'NonError' so patterns keyed on it keep matching. */
84
+ function serializeNonErrorObject(o) {
85
+ const ctor = constructorName(o);
86
+ const message = readProp(o, 'message');
87
+ const type = readProp(o, 'type');
88
+ let text;
89
+ if (typeof message === 'string' && message !== '') {
90
+ const label = ctor === undefined ? '' : typeof type === 'string' && type !== '' ? `${ctor} (${type}): ` : `${ctor}: `;
91
+ text = label + message;
92
+ }
93
+ else {
94
+ let json;
95
+ try {
96
+ json = JSON.stringify(o);
97
+ }
98
+ catch {
99
+ // circular or a throwing toJSON: fall through
100
+ }
101
+ if (typeof json === 'string' && json !== '{}')
102
+ text = json;
103
+ else if (ctor !== undefined)
104
+ text = `${ctor} {}`;
105
+ else
106
+ text = json ?? '{}';
107
+ }
108
+ const out = { name: 'NonError', message: truncate(text, 2000) };
109
+ const code = errorCode(o) ?? errorCode(o, 'status');
110
+ if (code !== undefined)
111
+ out.code = code;
112
+ const inner = readProp(o, 'error');
113
+ if (inner instanceof Error) {
114
+ out.cause = { name: inner.name, message: inner.message };
115
+ const innerCode = errorCode(inner);
116
+ if (innerCode !== undefined)
117
+ out.cause.code = innerCode;
118
+ }
119
+ return out;
120
+ }
62
121
  export function truncate(s, max) {
63
122
  return s.length <= max ? s : s.slice(0, max) + `…[+${s.length - max}]`;
64
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.62.0",
3
+ "version": "3.62.1",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",