@open-mercato/shared 0.6.8-develop.7091.1.15ffbe30ce → 0.6.8-develop.7093.1.700c5c8d96

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.
@@ -54,6 +54,8 @@ import { createGenericOptimisticLockReader } from "./optimistic-lock.js";
54
54
  import { registerOptimisticLockReaderIfAbsent } from "./optimistic-lock-store.js";
55
55
  import { createLogger } from "../logger/index.js";
56
56
  import { isTransientDbError } from "../db/pg-errors.js";
57
+ import { getTelemetryRuntime } from "../telemetry/runtime.js";
58
+ import { randomUUID } from "node:crypto";
57
59
  const logger = createLogger("shared").child({ component: "crud" });
58
60
  function resolveSortParams(queryParams, defaultSort) {
59
61
  const rawSortField = queryParams.sortField ?? queryParams.sort;
@@ -298,7 +300,13 @@ function attachOperationHeader(res, logEntry) {
298
300
  }
299
301
  return res;
300
302
  }
301
- function handleError(err) {
303
+ const INBOUND_REQUEST_ID_PATTERN = /^[A-Za-z0-9._-]{1,128}$/;
304
+ function resolveRequestId(request) {
305
+ const inbound = request?.headers.get("x-request-id")?.trim();
306
+ if (inbound && INBOUND_REQUEST_ID_PATTERN.test(inbound)) return inbound;
307
+ return randomUUID();
308
+ }
309
+ function handleError(err, request) {
302
310
  if (err instanceof Response) return err;
303
311
  if (isCrudHttpError(err)) return json(err.body, { status: err.status });
304
312
  const interceptorRejection = getCommandInterceptorHttpRejection(err);
@@ -317,12 +325,19 @@ function handleError(err) {
317
325
  }
318
326
  const message = err instanceof Error ? err.message : void 0;
319
327
  const stack = err instanceof Error ? err.stack : void 0;
320
- logger.error("Unexpected CRUD error", { message, stack, err });
328
+ const errorName = err instanceof Error ? err.name : void 0;
329
+ const requestId = resolveRequestId(request);
330
+ logger.error("Unexpected CRUD error", { message, stack, err, requestId });
331
+ getTelemetryRuntime()?.reportError(err, {
332
+ module: "crud",
333
+ attributes: { requestId, errorName }
334
+ });
321
335
  const body = {
322
336
  error: "Internal server error",
323
- message: "Something went wrong. Please try again later."
337
+ message: "Something went wrong. Please try again later.",
338
+ requestId
324
339
  };
325
- return json(body, { status: 500 });
340
+ return json(body, { status: 500, headers: { "x-request-id": requestId } });
326
341
  }
327
342
  const LIFECYCLE_ACTION_MAP = {
328
343
  created: { before: "creating", after: "created" },
@@ -1569,7 +1584,7 @@ function makeCrudRoute(opts) {
1569
1584
  return response;
1570
1585
  } catch (e) {
1571
1586
  finishProfile({ result: "error" });
1572
- return handleError(e);
1587
+ return handleError(e, request);
1573
1588
  }
1574
1589
  }
1575
1590
  async function POST(request) {
@@ -1854,7 +1869,7 @@ function makeCrudRoute(opts) {
1854
1869
  payload = await enrichSingleRecord(payload, ctx);
1855
1870
  return json(payload, { status: 201 });
1856
1871
  } catch (e) {
1857
- return handleError(e);
1872
+ return handleError(e, request);
1858
1873
  }
1859
1874
  }
1860
1875
  async function PUT(request) {
@@ -2159,7 +2174,7 @@ function makeCrudRoute(opts) {
2159
2174
  }
2160
2175
  return json(payload);
2161
2176
  } catch (e) {
2162
- return handleError(e);
2177
+ return handleError(e, request);
2163
2178
  }
2164
2179
  }
2165
2180
  async function DELETE(request) {
@@ -2422,7 +2437,7 @@ function makeCrudRoute(opts) {
2422
2437
  }
2423
2438
  return json(payload);
2424
2439
  } catch (e) {
2425
- return handleError(e);
2440
+ return handleError(e, request);
2426
2441
  }
2427
2442
  }
2428
2443
  return { metadata, GET, POST, PUT, DELETE };