keryx 0.24.1 → 0.25.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.
package/classes/API.ts CHANGED
@@ -86,9 +86,9 @@ export class API {
86
86
  this.logger.debug(`Initialized initializer ${initializer.name}`);
87
87
  } catch (e) {
88
88
  throw new TypedError({
89
- message: `${e}`,
89
+ message: `Failed to initialize initializer "${initializer.name}": ${e instanceof Error ? e.message : e}`,
90
90
  type: ErrorType.SERVER_INITIALIZATION,
91
- originalError: e,
91
+ cause: e,
92
92
  });
93
93
  }
94
94
  }
@@ -132,9 +132,9 @@ export class API {
132
132
  this.logger.debug(`Started initializer ${initializer.name}`);
133
133
  } catch (e) {
134
134
  throw new TypedError({
135
- message: `${e}`,
135
+ message: `Failed to start initializer "${initializer.name}": ${e instanceof Error ? e.message : e}`,
136
136
  type: ErrorType.SERVER_START,
137
- originalError: e,
137
+ cause: e,
138
138
  });
139
139
  }
140
140
  }
@@ -167,9 +167,9 @@ export class API {
167
167
  this.logger.debug(`Stopped initializer ${initializer.name}`);
168
168
  } catch (e) {
169
169
  throw new TypedError({
170
- message: `${e}`,
170
+ message: `Failed to stop initializer "${initializer.name}": ${e instanceof Error ? e.message : e}`,
171
171
  type: ErrorType.SERVER_STOP,
172
- originalError: e,
172
+ cause: e,
173
173
  });
174
174
  }
175
175
  }
@@ -161,7 +161,7 @@ export class Connection<
161
161
  : new TypedError({
162
162
  message: `${e}`,
163
163
  type: ErrorType.CONNECTION_ACTION_RUN,
164
- originalError: e,
164
+ cause: e,
165
165
  });
166
166
  } finally {
167
167
  if (action && formattedParams) {
@@ -337,7 +337,7 @@ export class Connection<
337
337
  throw new TypedError({
338
338
  message: `Error validating params: ${e}`,
339
339
  type: ErrorType.CONNECTION_ACTION_PARAM_VALIDATION,
340
- originalError: e,
340
+ cause: e,
341
341
  });
342
342
  }
343
343
  }
@@ -73,8 +73,11 @@ export type TypedErrorArgs = {
73
73
  message: string;
74
74
  /** The error category, which determines the HTTP status code. */
75
75
  type: ErrorType;
76
- /** The original caught error, if wrapping. Its stack trace is preserved on the `TypedError`. */
77
- originalError?: unknown;
76
+ /**
77
+ * The underlying error being wrapped. Exposed via the standard ES2022 `Error.cause`
78
+ * field, so `console.error(err)` prints the full "Caused by:" chain natively.
79
+ */
80
+ cause?: unknown;
78
81
  /** The param key that caused the error (for validation errors). */
79
82
  key?: string;
80
83
  /** The param value that caused the error (for validation errors). */
@@ -84,7 +87,9 @@ export type TypedErrorArgs = {
84
87
  /**
85
88
  * Structured error class for action and framework failures. Extends `Error` with an
86
89
  * `ErrorType` that maps to an HTTP status code, and optional `key`/`value` fields for
87
- * param validation errors. If `originalError` is provided, its stack trace is preserved.
90
+ * param validation errors. When `cause` is provided, it is forwarded to the base
91
+ * `Error` constructor so the native ES2022 cause chain is preserved — tooling that
92
+ * inspects errors (Node's default printer, debuggers) will walk both stacks.
88
93
  */
89
94
  export class TypedError extends Error {
90
95
  /** The error category, used to determine the HTTP status code via `ErrorStatusCodes`. */
@@ -95,17 +100,12 @@ export class TypedError extends Error {
95
100
  value?: any;
96
101
 
97
102
  constructor(args: TypedErrorArgs) {
98
- super(args.message);
103
+ super(
104
+ args.message,
105
+ args.cause !== undefined ? { cause: args.cause } : undefined,
106
+ );
99
107
  this.type = args.type;
100
108
  this.key = args.key;
101
109
  this.value = args.value;
102
-
103
- if (args.originalError !== undefined) {
104
- if (args.originalError instanceof Error) {
105
- this.stack = args.originalError.stack;
106
- } else {
107
- this.stack = `OriginalStringError: ${args.originalError}`;
108
- }
109
- }
110
110
  }
111
111
  }
@@ -12,6 +12,16 @@ const namespace = "channels";
12
12
  const PRESENCE_KEY_PREFIX = "presence:";
13
13
  const LUA_DIR = join(import.meta.dir, "..", "lua");
14
14
 
15
+ const ADD_PRESENCE_LUA = await Bun.file(
16
+ join(LUA_DIR, "add-presence.lua"),
17
+ ).text();
18
+ const REMOVE_PRESENCE_LUA = await Bun.file(
19
+ join(LUA_DIR, "remove-presence.lua"),
20
+ ).text();
21
+ const REFRESH_PRESENCE_LUA = await Bun.file(
22
+ join(LUA_DIR, "refresh-presence.lua"),
23
+ ).text();
24
+
15
25
  declare module "../classes/API" {
16
26
  export interface API {
17
27
  [namespace]: Awaited<ReturnType<Channels["initialize"]>>;
@@ -19,9 +29,6 @@ declare module "../classes/API" {
19
29
  }
20
30
 
21
31
  export class Channels extends Initializer {
22
- private addPresenceLua = "";
23
- private removePresenceLua = "";
24
- private refreshPresenceLua = "";
25
32
  private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
26
33
 
27
34
  constructor() {
@@ -107,7 +114,7 @@ export class Channels extends Initializer {
107
114
  const connectionSetKey = `${PRESENCE_KEY_PREFIX}${channelName}:${key}`;
108
115
 
109
116
  const added = await api.redis.redis.eval(
110
- this.addPresenceLua,
117
+ ADD_PRESENCE_LUA,
111
118
  2,
112
119
  connectionSetKey,
113
120
  channelKey,
@@ -140,7 +147,7 @@ export class Channels extends Initializer {
140
147
  const connectionSetKey = `${PRESENCE_KEY_PREFIX}${channelName}:${key}`;
141
148
 
142
149
  const shouldLeave = await api.redis.redis.eval(
143
- this.removePresenceLua,
150
+ REMOVE_PRESENCE_LUA,
144
151
  2,
145
152
  connectionSetKey,
146
153
  channelKey,
@@ -190,7 +197,7 @@ export class Channels extends Initializer {
190
197
 
191
198
  const keys = [...keysToRefresh];
192
199
  await api.redis.redis.eval(
193
- this.refreshPresenceLua,
200
+ REFRESH_PRESENCE_LUA,
194
201
  keys.length,
195
202
  ...keys,
196
203
  config.channels.presenceTTL,
@@ -218,16 +225,6 @@ export class Channels extends Initializer {
218
225
  };
219
226
 
220
227
  async initialize() {
221
- this.addPresenceLua = await Bun.file(
222
- join(LUA_DIR, "add-presence.lua"),
223
- ).text();
224
- this.removePresenceLua = await Bun.file(
225
- join(LUA_DIR, "remove-presence.lua"),
226
- ).text();
227
- this.refreshPresenceLua = await Bun.file(
228
- join(LUA_DIR, "refresh-presence.lua"),
229
- ).text();
230
-
231
228
  // Load plugin channels
232
229
  const pluginChannels: Channel[] = [];
233
230
  for (const plugin of config.plugins) {
@@ -11,7 +11,10 @@ import { api, logger } from "../api";
11
11
  import { Initializer } from "../classes/Initializer";
12
12
  import { ErrorType, TypedError } from "../classes/TypedError";
13
13
  import { config } from "../config";
14
- import { formatConnectionStringForLogging } from "../util/connectionString";
14
+ import {
15
+ formatConnectionStringForLogging,
16
+ throwConnectionError,
17
+ } from "../util/connectionString";
15
18
 
16
19
  const namespace = "db";
17
20
 
@@ -59,10 +62,7 @@ export class DB extends Initializer {
59
62
  try {
60
63
  await api.db.db.execute(sql`SELECT NOW()`);
61
64
  } catch (e) {
62
- throw new TypedError({
63
- type: ErrorType.SERVER_INITIALIZATION,
64
- message: `Cannot connect to database (${formatConnectionStringForLogging(config.database.connectionString)}): ${e}`,
65
- });
65
+ throwConnectionError("database", config.database.connectionString, e);
66
66
  }
67
67
 
68
68
  if (config.database.autoMigrate) {
@@ -1,9 +1,11 @@
1
1
  import { Redis as RedisClient } from "ioredis";
2
2
  import { api, logger } from "../api";
3
3
  import { Initializer } from "../classes/Initializer";
4
- import { ErrorType, TypedError } from "../classes/TypedError";
5
4
  import { config } from "../config";
6
- import { formatConnectionStringForLogging } from "../util/connectionString";
5
+ import {
6
+ formatConnectionStringForLogging,
7
+ throwConnectionError,
8
+ } from "../util/connectionString";
7
9
 
8
10
  const namespace = "redis";
9
11
  const testKey = `__keryx_test_key:${config.process.name}`;
@@ -42,10 +44,7 @@ export class Redis extends Initializer {
42
44
  await api.redis.subscription.set(testKey, Date.now());
43
45
  await api.redis.subscription.del(testKey);
44
46
  } catch (e) {
45
- throw new TypedError({
46
- type: ErrorType.SERVER_INITIALIZATION,
47
- message: `Cannot connect to redis (${formatConnectionStringForLogging(config.redis.connectionString)}): ${e}`,
48
- });
47
+ throwConnectionError("redis", config.redis.connectionString, e);
49
48
  }
50
49
 
51
50
  logger.info(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keryx",
3
- "version": "0.24.1",
3
+ "version": "0.25.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1,6 +1,20 @@
1
+ import { ErrorType, TypedError } from "../classes/TypedError";
2
+
1
3
  /** Strip the password from a connection string for safe logging. Preserves protocol, user, host, port, and path. */
2
4
  export function formatConnectionStringForLogging(connectionString: string) {
3
5
  const connectionStringParsed = new URL(connectionString);
4
6
  const connectionStringInfo = `${connectionStringParsed.protocol ? `${connectionStringParsed.protocol}//` : ""}${connectionStringParsed.username ? `${connectionStringParsed.username}@` : ""}${connectionStringParsed.hostname}:${connectionStringParsed.port}${connectionStringParsed.pathname}`;
5
7
  return connectionStringInfo;
6
8
  }
9
+
10
+ /** Throw a standardized `SERVER_INITIALIZATION` error for a failed connection probe. The connection string is password-stripped via {@link formatConnectionStringForLogging} before being embedded in the message. */
11
+ export function throwConnectionError(
12
+ service: string,
13
+ connectionString: string,
14
+ error: unknown,
15
+ ): never {
16
+ throw new TypedError({
17
+ type: ErrorType.SERVER_INITIALIZATION,
18
+ message: `Cannot connect to ${service} (${formatConnectionStringForLogging(connectionString)}): ${error}`,
19
+ });
20
+ }
package/util/glob.ts CHANGED
@@ -38,7 +38,7 @@ export async function globLoader<T>(searchDir: string) {
38
38
  throw new TypedError({
39
39
  message: `Error loading from ${dir} - ${name} - ${error}`,
40
40
  type: ErrorType.SERVER_INITIALIZATION,
41
- originalError: error,
41
+ cause: error,
42
42
  });
43
43
  }
44
44
  }
@@ -73,7 +73,7 @@ export async function withTransaction<T>(
73
73
  throw new TypedError({
74
74
  message: `${e}`,
75
75
  type: ErrorType.CONNECTION_ACTION_RUN,
76
- originalError: e,
76
+ cause: e,
77
77
  });
78
78
  } finally {
79
79
  client.release();
@@ -77,7 +77,7 @@ export async function parseRequestParams(
77
77
  throw new TypedError({
78
78
  message: `cannot parse request body: ${e}`,
79
79
  type: ErrorType.CONNECTION_ACTION_RUN,
80
- originalError: e,
80
+ cause: e,
81
81
  });
82
82
  }
83
83
  } else if (
package/util/webSocket.ts CHANGED
@@ -122,7 +122,7 @@ export async function handleWebsocketSubscribe(
122
122
  : new TypedError({
123
123
  message: `${e}`,
124
124
  type: ErrorType.CONNECTION_CHANNEL_AUTHORIZATION,
125
- originalError: e,
125
+ cause: e,
126
126
  });
127
127
  ws.send(
128
128
  JSON.stringify({
@@ -174,7 +174,7 @@ export async function handleWebsocketUnsubscribe(
174
174
  : new TypedError({
175
175
  message: `${e}`,
176
176
  type: ErrorType.CONNECTION_CHANNEL_VALIDATION,
177
- originalError: e,
177
+ cause: e,
178
178
  });
179
179
  ws.send(
180
180
  JSON.stringify({