@steve31415/baselib 3.34.1 → 3.35.0

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.
@@ -21,10 +21,14 @@ export interface Logger {
21
21
  export interface SerializedError {
22
22
  name: string;
23
23
  message: string;
24
+ /** The error's own `code` (Node's `ECONNREFUSED`, Postgres SQLSTATE, an
25
+ * app error's machine-readable reason), when it is a string or number. */
26
+ code?: string | number;
24
27
  stack?: string;
25
28
  cause?: {
26
29
  name: string;
27
30
  message: string;
31
+ code?: string | number;
28
32
  };
29
33
  }
30
34
  export declare function serializeError(err: unknown): SerializedError;
package/dist/log-core.js CHANGED
@@ -10,11 +10,30 @@
10
10
  // ship-failure check counts. makeEvent also guards the shape of `meta`: every
11
11
  // distinct key is an Axiom column (see "Key-shape guard" below).
12
12
  export const LOG_LEVELS = ['debug', 'info', 'warn', 'error'];
13
+ const CODE_MAX = 100;
14
+ /** An Error's `code` property when it is a primitive worth a column: a
15
+ * string (capped) or a finite number. Anything else is dropped, since an
16
+ * object there would mint `meta.error.code.*` keys from data. */
17
+ function errorCode(err) {
18
+ const code = err.code;
19
+ if (typeof code === 'string')
20
+ return truncate(code, CODE_MAX);
21
+ if (typeof code === 'number' && Number.isFinite(code))
22
+ return code;
23
+ return undefined;
24
+ }
13
25
  export function serializeError(err) {
14
26
  if (err instanceof Error) {
15
- const out = { name: err.name, message: err.message, stack: err.stack };
27
+ const out = { name: err.name, message: err.message };
28
+ const code = errorCode(err);
29
+ if (code !== undefined)
30
+ out.code = code;
31
+ out.stack = err.stack;
16
32
  if (err.cause instanceof Error) {
17
33
  out.cause = { name: err.cause.name, message: err.cause.message };
34
+ const causeCode = errorCode(err.cause);
35
+ if (causeCode !== undefined)
36
+ out.cause.code = causeCode;
18
37
  }
19
38
  else if (err.cause !== undefined) {
20
39
  out.cause = { name: 'cause', message: truncate(String(err.cause), 500) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.34.1",
3
+ "version": "3.35.0",
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",