@bymax-one/nest-logger 1.4.12 → 1.4.13

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/CHANGELOG.md CHANGED
@@ -11,6 +11,42 @@ heading here.
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [1.4.13] - 2026-11-08
15
+
16
+ ### Fixed
17
+
18
+ - **A client error the body parser raises keeps its status.** An error with the `http-errors` shape,
19
+ a `status` or `statusCode` from 400 to 499 and `expose: true`, such as a body over the limit
20
+ (413) or an unsupported content encoding (415), was answered by `HttpExceptionFilter` as a 500
21
+ `{ statusCode, message }`. That filter is registered by `forRoot` with `http.isEnabled` and
22
+ `http.shouldCaptureExceptions` (default `true`), or by hand; `forRootAsync` does not register it, and an application that uses it without the filter was
23
+ answered by Nest's own handler, with the right status, before and after. It is now answered
24
+ with its own status and message, `{ statusCode: 413, message: 'request entity too large' }`, and
25
+ logged as `HTTP_EXCEPTION_HANDLED` at warn level. The same error thrown inside a route is no
26
+ longer logged as `HTTP_REQUEST_SERVER_ERROR` but as `HTTP_REQUEST_CLIENT_ERROR`. An error
27
+ without `expose: true`, or with a status outside 400 to 499, stays a 500 with the fixed body.
28
+ **Apply to a derived backend:** nothing to do; a client that read the 500 for these cases now
29
+ reads the 413 or 415 the parser meant.
30
+ - **The README says which filter answers a parser error, and for whom.** `forRoot` with
31
+ `http.isEnabled` and `http.shouldCaptureExceptions` (default `true`) registers the library's own
32
+ exception filter as an `APP_FILTER` provider, so the
33
+ import order of the modules decides between `APP_FILTER` providers, and `app.useGlobalFilters(...)`
34
+ always answers ahead of them. `forRootAsync` registers no filter, so none of that applies to it
35
+ unless the filter is registered by hand. The 1.4.12 note on this was written without that scope.
36
+ Pinned by `use-request-context-filters.e2e-spec.ts`.
37
+
38
+ ### Internal
39
+
40
+ - **A source file over 800 lines now fails CI unless it is on a frozen baseline.**
41
+ `scripts/check-file-length.mjs` (`pnpm check:file-length`) runs in the `verify` job and, in a
42
+ pull request, also compares the baseline with the base branch's, so the baseline cannot widen. It
43
+ starts with the three files that are already over the limit. Nothing in the package changes.
44
+ - **The redaction walk is split into functions of at most 50 lines.** `walkObject` was 174 lines;
45
+ it is now a short function that delegates to smaller ones, with the rationale kept as their
46
+ documentation. No behaviour changes: 48000 comparisons of the old and the new walk over
47
+ generated records (six configurations, as record root and not), and the existing suite, give
48
+ identical output.
49
+
14
50
  ## [1.4.12] - 2026-11-08
15
51
 
16
52
  ### Added
@@ -2547,7 +2583,8 @@ published `dist/` is identical — no runtime behaviour changes for consumers.
2547
2583
  - Professional CI suite: `ci.yml`, `bench.yml`, `codeql.yml`, `scorecard.yml`,
2548
2584
  `release.yml`, Dependabot, and issue templates
2549
2585
 
2550
- [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.12...HEAD
2586
+ [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.13...HEAD
2587
+ [1.4.13]: https://github.com/bymaxone/nest-logger/compare/v1.4.12...v1.4.13
2551
2588
  [1.4.12]: https://github.com/bymaxone/nest-logger/compare/v1.4.11...v1.4.12
2552
2589
  [1.4.11]: https://github.com/bymaxone/nest-logger/compare/v1.4.10...v1.4.11
2553
2590
  [1.4.10]: https://github.com/bymaxone/nest-logger/compare/v1.4.9...v1.4.10
package/README.md CHANGED
@@ -451,10 +451,15 @@ To open only the correlation scope ahead of the parser, without the access log,
451
451
  `BymaxLoggerModule.useRequestContext(app)` at the same point. A request the parser refuses (truncated JSON,
452
452
  a content encoding it does not support, a body over the limit) then has its `requestId` and the
453
453
  `x-request-id` response header, so an exception filter that reads the id from `LogContextService`
454
- puts the same value in its envelope. Register that filter with `app.useGlobalFilters(...)` after `NestFactory.create()`: with
455
- `http.isEnabled` (and `http.shouldCaptureExceptions`, which defaults to `true`) the library registers its own exception filter as an `APP_FILTER` provider, and an
456
- `APP_FILTER` provider of yours was answered by it in the measurement, with or without an injected
457
- dependency, while a filter registered with `useGlobalFilters` answered instead. It can be combined with `applyAccessLog(app)` or
454
+ puts the same value in its envelope. Register that filter with `app.useGlobalFilters(...)` after `NestFactory.create()`, which answers
455
+ ahead of every `APP_FILTER` provider. `forRoot` with `http.isEnabled` (and
456
+ `http.shouldCaptureExceptions`, which defaults to `true`) registers the library's own exception
457
+ filter as an `APP_FILTER` provider; `forRootAsync` never does, so there is nothing for yours to
458
+ compete with unless you registered `HttpExceptionFilter` yourself. Where the library's filter is
459
+ registered, the order the modules are imported decides between `APP_FILTER` providers: one in your
460
+ root module, or in a module imported before `BymaxLoggerModule`, was answered by the library's
461
+ filter in the measurement, with or without an injected dependency, while one in a module imported
462
+ after it answered itself. It can be combined with `applyAccessLog(app)` or
458
463
  `applyRequestIdMiddleware(consumer)`: a second mount reuses the scope.
459
464
 
460
465
  `applyRequestIdMiddleware(consumer)` remains supported and unchanged; it is the right choice when
@@ -965,6 +965,23 @@ exports.DefaultStdoutDestination = __decorateClass([
965
965
  common.Injectable()
966
966
  ], exports.DefaultStdoutDestination);
967
967
 
968
+ // src/server/utils/exposed-client-error.util.ts
969
+ var MIN_CLIENT_STATUS = 400;
970
+ var MAX_CLIENT_STATUS = 499;
971
+ function readExposedClientError(value) {
972
+ try {
973
+ const candidate = value;
974
+ const status = candidate.status ?? candidate.statusCode;
975
+ const message = value.message;
976
+ const isClientStatus = typeof status === "number" && Number.isInteger(status) && status >= MIN_CLIENT_STATUS && status <= MAX_CLIENT_STATUS;
977
+ if (candidate.expose !== true || !isClientStatus || typeof message !== "string")
978
+ return void 0;
979
+ return { status, message };
980
+ } catch {
981
+ return void 0;
982
+ }
983
+ }
984
+
968
985
  // src/server/utils/normalize-url.util.ts
969
986
  var UUID_REGEX = /\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?![0-9a-f])/gi;
970
987
  var ULID_REGEX = /\/[0-9A-HJKMNP-TV-Z]{26}(?![0-9A-HJKMNP-TV-Z])/g;
@@ -1007,9 +1024,9 @@ exports.HttpExceptionFilter = class HttpExceptionFilter {
1007
1024
  const userId = req.user?.sub ?? req.user?.id;
1008
1025
  const url = stripQueryString(req.url);
1009
1026
  if (exception instanceof common.HttpException) {
1010
- const status2 = exception.getStatus();
1011
- const metadata = { method: req.method, url, status: status2 };
1012
- if (status2 >= common.HttpStatus.INTERNAL_SERVER_ERROR) {
1027
+ const status = exception.getStatus();
1028
+ const metadata = { method: req.method, url, status };
1029
+ if (status >= common.HttpStatus.INTERNAL_SERVER_ERROR) {
1013
1030
  this.logger.errorStructured(
1014
1031
  RESERVED_LOG_KEYS.HTTP_EXCEPTION_UNHANDLED,
1015
1032
  sanitizeError(exception),
@@ -1024,19 +1041,51 @@ exports.HttpExceptionFilter = class HttpExceptionFilter {
1024
1041
  metadata
1025
1042
  );
1026
1043
  }
1027
- res.status(status2).json(exception.getResponse());
1044
+ res.status(status).json(exception.getResponse());
1028
1045
  return;
1029
1046
  }
1047
+ const clientError = readExposedClientError(exception);
1048
+ if (clientError !== void 0) {
1049
+ this.answerClientError(clientError, res, { method: req.method, url, userId });
1050
+ return;
1051
+ }
1052
+ this.answerUnhandled(exception, res, { method: req.method, url, userId });
1053
+ }
1054
+ /**
1055
+ * Log an exposed client error as handled and answer it with its own status and message.
1056
+ *
1057
+ * @param clientError - The status and message the error exposes.
1058
+ * @param res - The response to write.
1059
+ * @param request - The request facts every entry carries.
1060
+ */
1061
+ answerClientError(clientError, res, request) {
1062
+ const { method, url, userId } = request;
1063
+ this.logger.warnStructured(
1064
+ RESERVED_LOG_KEYS.HTTP_EXCEPTION_HANDLED,
1065
+ clientError.message,
1066
+ userId,
1067
+ { method, url, status: clientError.status }
1068
+ );
1069
+ res.status(clientError.status).json({
1070
+ statusCode: clientError.status,
1071
+ message: clientError.message
1072
+ });
1073
+ }
1074
+ /**
1075
+ * Log any other non-`HttpException` as unhandled and answer it with the fixed 500 body.
1076
+ *
1077
+ * @param exception - The thrown value.
1078
+ * @param res - The response to write.
1079
+ * @param request - The request facts every entry carries.
1080
+ */
1081
+ answerUnhandled(exception, res, request) {
1082
+ const { method, url, userId } = request;
1030
1083
  const status = common.HttpStatus.INTERNAL_SERVER_ERROR;
1031
1084
  this.logger.errorStructured(
1032
1085
  RESERVED_LOG_KEYS.HTTP_EXCEPTION_UNHANDLED,
1033
1086
  sanitizeError(exception),
1034
1087
  userId,
1035
- {
1036
- method: req.method,
1037
- url,
1038
- status
1039
- }
1088
+ { method, url, status }
1040
1089
  );
1041
1090
  res.status(status).json(INTERNAL_ERROR_BODY);
1042
1091
  }
@@ -1217,8 +1266,8 @@ exports.HttpLoggingInterceptor = class HttpLoggingInterceptor {
1217
1266
  }
1218
1267
  /**
1219
1268
  * Log a thrown completion: SERVER_ERROR (error level, with stack) for 5xx,
1220
- * CLIENT_ERROR (warn level) for an `HttpException` below 500. A non-`HttpException`
1221
- * always maps to 500, so it is reported as a server error.
1269
+ * CLIENT_ERROR (warn level) for an `HttpException` below 500 or an exposed `http-errors`
1270
+ * client error. Any other non-`HttpException` maps to 500, so it is reported as a server error.
1222
1271
  *
1223
1272
  * @param err - The thrown value (anything).
1224
1273
  * @param method - The HTTP method.
@@ -1246,6 +1295,11 @@ exports.HttpLoggingInterceptor = class HttpLoggingInterceptor {
1246
1295
  }
1247
1296
  return;
1248
1297
  }
1298
+ const clientError = readExposedClientError(err);
1299
+ if (clientError !== void 0) {
1300
+ this.logClientError(clientError, method, url, userId, duration);
1301
+ return;
1302
+ }
1249
1303
  this.logger.errorStructured(RESERVED_LOG_KEYS.HTTP_REQUEST_SERVER_ERROR, toError(err), userId, {
1250
1304
  method,
1251
1305
  url,
@@ -1253,6 +1307,29 @@ exports.HttpLoggingInterceptor = class HttpLoggingInterceptor {
1253
1307
  duration
1254
1308
  });
1255
1309
  }
1310
+ /**
1311
+ * Log an exposed `http-errors` client error as CLIENT_ERROR at warn level.
1312
+ *
1313
+ * @param clientError - The status and message the error exposes.
1314
+ * @param method - The HTTP method.
1315
+ * @param url - The normalized request URL.
1316
+ * @param userId - The acting user id, when known.
1317
+ * @param duration - The elapsed time in milliseconds.
1318
+ */
1319
+ logClientError(clientError, method, url, userId, duration) {
1320
+ this.logger.warnStructured(
1321
+ RESERVED_LOG_KEYS.HTTP_REQUEST_CLIENT_ERROR,
1322
+ `${method} ${url} \u2192 ${clientError.status}`,
1323
+ userId,
1324
+ {
1325
+ method,
1326
+ url,
1327
+ statusCode: clientError.status,
1328
+ duration,
1329
+ errorMessage: cutIfText(clientError.message)
1330
+ }
1331
+ );
1332
+ }
1256
1333
  };
1257
1334
  exports.HttpLoggingInterceptor = __decorateClass([
1258
1335
  common.Injectable(),
@@ -2034,69 +2111,76 @@ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized)
2034
2111
  if (ArrayBuffer.isView(value)) {
2035
2112
  return withBinaryAsSize(value);
2036
2113
  }
2037
- const plain = rules.ownFields.safe;
2038
- const resolvedToJson = resolveToJson(value, plain);
2114
+ const method = resolveToJson(value, rules.ownFields.safe).method;
2039
2115
  ancestors.add(value);
2040
2116
  try {
2041
- const ownsItsForm = rules.ownFields.safe && isError(value);
2042
- const toJson = honorToJson && !ownsItsForm ? resolvedToJson.method : void 0;
2043
- if (toJson !== void 0) {
2044
- if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
2045
- return rules.censor;
2046
- }
2047
- return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
2048
- }
2049
- if (typeof value === "function") {
2050
- return plain ? void 0 : value;
2051
- }
2052
- if (Array.isArray(value)) {
2053
- return walkArray(value, rules, depth2, ancestors, inSerialized);
2054
- }
2055
- const source = value;
2056
- const keys = Object.keys(source);
2057
- const asData = isError(value) && (plain || rules.errorsAsData && !inSerialized);
2058
- const clone = isError(value) ? cloneError(value) : void 0;
2059
- const copy = clone !== void 0 && !asData ? clone : {};
2060
- const scrubbed = isError(value) && (plain || !inSerialized);
2061
- const omitted = /* @__PURE__ */ new Set();
2062
- for (const key of keys) {
2063
- const read = Reflect.get(source, key);
2064
- const current = scrubbed ? ownFieldForLog(source, key, read, rules.ownFields) : read;
2065
- if (current === OMIT_OWN_FIELD) {
2066
- Reflect.deleteProperty(copy, key);
2067
- omitted.add(key);
2068
- continue;
2069
- }
2070
- defineOwn(
2071
- copy,
2072
- key,
2073
- rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
2074
- // An own enumerable `name`, `message` or `stack` of an `Error` is the same text as
2075
- // the part the walk would add, so it is cut the same way.
2076
- scrubbed && ERROR_TEXT_FIELDS.has(key) ? cutIfText(current) : current,
2077
- rules,
2078
- depth2 + 1,
2079
- ancestors,
2080
- true,
2081
- inSerialized || !honorToJson && rules.serializedKeys.has(key)
2082
- )
2083
- );
2084
- }
2085
- if (asData) {
2086
- const written = keys.filter((key) => !omitted.has(key));
2087
- addErrorParts(
2088
- copy,
2089
- value,
2090
- written,
2091
- errorPartsHost(rules, ancestors, inSerialized),
2092
- depth2
2093
- );
2094
- }
2095
- return copy;
2117
+ return walkForm(value, method, rules, depth2, ancestors, honorToJson, inSerialized);
2096
2118
  } finally {
2097
2119
  ancestors.delete(value);
2098
2120
  }
2099
2121
  }
2122
+ function walkForm(value, method, rules, depth2, ancestors, honorToJson, inSerialized) {
2123
+ const plain = rules.ownFields.safe;
2124
+ const toJson = honorToJson && !(plain && isError(value)) ? method : void 0;
2125
+ if (toJson !== void 0) {
2126
+ return walkToJsonOutput(value, toJson, rules, depth2, ancestors, inSerialized);
2127
+ }
2128
+ if (typeof value === "function") {
2129
+ return plain ? void 0 : value;
2130
+ }
2131
+ if (Array.isArray(value)) {
2132
+ return walkArray(value, rules, depth2, ancestors, inSerialized);
2133
+ }
2134
+ return walkProperties(value, rules, depth2, ancestors, honorToJson, inSerialized);
2135
+ }
2136
+ function walkToJsonOutput(value, toJson, rules, depth2, ancestors, inSerialized) {
2137
+ if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
2138
+ return rules.censor;
2139
+ }
2140
+ return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
2141
+ }
2142
+ function walkProperties(value, rules, depth2, ancestors, honorToJson, inSerialized) {
2143
+ const source = value;
2144
+ const keys = Object.keys(source);
2145
+ const { safe } = rules.ownFields;
2146
+ const asData = isError(value) && (safe || rules.errorsAsData && !inSerialized);
2147
+ const copy = startCopy(value, asData);
2148
+ const scrubbed = isError(value) && (safe || !inSerialized);
2149
+ const omitted = /* @__PURE__ */ new Set();
2150
+ for (const key of keys) {
2151
+ const read = Reflect.get(source, key);
2152
+ const current = scrubbed ? ownFieldForLog(source, key, read, rules.ownFields) : read;
2153
+ if (current === OMIT_OWN_FIELD) {
2154
+ Reflect.deleteProperty(copy, key);
2155
+ omitted.add(key);
2156
+ continue;
2157
+ }
2158
+ defineOwn(
2159
+ copy,
2160
+ key,
2161
+ rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
2162
+ scrubbed && ERROR_TEXT_FIELDS.has(key) ? cutIfText(current) : current,
2163
+ rules,
2164
+ depth2 + 1,
2165
+ ancestors,
2166
+ true,
2167
+ inSerialized || !honorToJson && rules.serializedKeys.has(key)
2168
+ )
2169
+ );
2170
+ }
2171
+ if (asData) {
2172
+ const host = errorPartsHost(rules, ancestors, inSerialized);
2173
+ addErrorParts(copy, value, without(keys, omitted), host, depth2);
2174
+ }
2175
+ return copy;
2176
+ }
2177
+ function without(keys, omitted) {
2178
+ return keys.filter((key) => !omitted.has(key));
2179
+ }
2180
+ function startCopy(value, asData) {
2181
+ const clone = isError(value) ? cloneError(value) : void 0;
2182
+ return clone !== void 0 && !asData ? clone : {};
2183
+ }
2100
2184
  function errorPartsHost(rules, ancestors, inSerialized) {
2101
2185
  return {
2102
2186
  sensitive: rules.sensitive,
@@ -1257,6 +1257,22 @@ declare class HttpExceptionFilter implements ExceptionFilter {
1257
1257
  * @param host - The arguments host (switched to HTTP).
1258
1258
  */
1259
1259
  catch(exception: unknown, host: ArgumentsHost): void;
1260
+ /**
1261
+ * Log an exposed client error as handled and answer it with its own status and message.
1262
+ *
1263
+ * @param clientError - The status and message the error exposes.
1264
+ * @param res - The response to write.
1265
+ * @param request - The request facts every entry carries.
1266
+ */
1267
+ private answerClientError;
1268
+ /**
1269
+ * Log any other non-`HttpException` as unhandled and answer it with the fixed 500 body.
1270
+ *
1271
+ * @param exception - The thrown value.
1272
+ * @param res - The response to write.
1273
+ * @param request - The request facts every entry carries.
1274
+ */
1275
+ private answerUnhandled;
1260
1276
  }
1261
1277
 
1262
1278
  /**
@@ -1318,8 +1334,8 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1318
1334
  private logSuccess;
1319
1335
  /**
1320
1336
  * Log a thrown completion: SERVER_ERROR (error level, with stack) for 5xx,
1321
- * CLIENT_ERROR (warn level) for an `HttpException` below 500. A non-`HttpException`
1322
- * always maps to 500, so it is reported as a server error.
1337
+ * CLIENT_ERROR (warn level) for an `HttpException` below 500 or an exposed `http-errors`
1338
+ * client error. Any other non-`HttpException` maps to 500, so it is reported as a server error.
1323
1339
  *
1324
1340
  * @param err - The thrown value (anything).
1325
1341
  * @param method - The HTTP method.
@@ -1328,6 +1344,16 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1328
1344
  * @param duration - The elapsed time in milliseconds.
1329
1345
  */
1330
1346
  private logError;
1347
+ /**
1348
+ * Log an exposed `http-errors` client error as CLIENT_ERROR at warn level.
1349
+ *
1350
+ * @param clientError - The status and message the error exposes.
1351
+ * @param method - The HTTP method.
1352
+ * @param url - The normalized request URL.
1353
+ * @param userId - The acting user id, when known.
1354
+ * @param duration - The elapsed time in milliseconds.
1355
+ */
1356
+ private logClientError;
1331
1357
  }
1332
1358
 
1333
1359
  /**
@@ -1257,6 +1257,22 @@ declare class HttpExceptionFilter implements ExceptionFilter {
1257
1257
  * @param host - The arguments host (switched to HTTP).
1258
1258
  */
1259
1259
  catch(exception: unknown, host: ArgumentsHost): void;
1260
+ /**
1261
+ * Log an exposed client error as handled and answer it with its own status and message.
1262
+ *
1263
+ * @param clientError - The status and message the error exposes.
1264
+ * @param res - The response to write.
1265
+ * @param request - The request facts every entry carries.
1266
+ */
1267
+ private answerClientError;
1268
+ /**
1269
+ * Log any other non-`HttpException` as unhandled and answer it with the fixed 500 body.
1270
+ *
1271
+ * @param exception - The thrown value.
1272
+ * @param res - The response to write.
1273
+ * @param request - The request facts every entry carries.
1274
+ */
1275
+ private answerUnhandled;
1260
1276
  }
1261
1277
 
1262
1278
  /**
@@ -1318,8 +1334,8 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1318
1334
  private logSuccess;
1319
1335
  /**
1320
1336
  * Log a thrown completion: SERVER_ERROR (error level, with stack) for 5xx,
1321
- * CLIENT_ERROR (warn level) for an `HttpException` below 500. A non-`HttpException`
1322
- * always maps to 500, so it is reported as a server error.
1337
+ * CLIENT_ERROR (warn level) for an `HttpException` below 500 or an exposed `http-errors`
1338
+ * client error. Any other non-`HttpException` maps to 500, so it is reported as a server error.
1323
1339
  *
1324
1340
  * @param err - The thrown value (anything).
1325
1341
  * @param method - The HTTP method.
@@ -1328,6 +1344,16 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1328
1344
  * @param duration - The elapsed time in milliseconds.
1329
1345
  */
1330
1346
  private logError;
1347
+ /**
1348
+ * Log an exposed `http-errors` client error as CLIENT_ERROR at warn level.
1349
+ *
1350
+ * @param clientError - The status and message the error exposes.
1351
+ * @param method - The HTTP method.
1352
+ * @param url - The normalized request URL.
1353
+ * @param userId - The acting user id, when known.
1354
+ * @param duration - The elapsed time in milliseconds.
1355
+ */
1356
+ private logClientError;
1331
1357
  }
1332
1358
 
1333
1359
  /**
@@ -962,6 +962,23 @@ DefaultStdoutDestination = __decorateClass([
962
962
  Injectable()
963
963
  ], DefaultStdoutDestination);
964
964
 
965
+ // src/server/utils/exposed-client-error.util.ts
966
+ var MIN_CLIENT_STATUS = 400;
967
+ var MAX_CLIENT_STATUS = 499;
968
+ function readExposedClientError(value) {
969
+ try {
970
+ const candidate = value;
971
+ const status = candidate.status ?? candidate.statusCode;
972
+ const message = value.message;
973
+ const isClientStatus = typeof status === "number" && Number.isInteger(status) && status >= MIN_CLIENT_STATUS && status <= MAX_CLIENT_STATUS;
974
+ if (candidate.expose !== true || !isClientStatus || typeof message !== "string")
975
+ return void 0;
976
+ return { status, message };
977
+ } catch {
978
+ return void 0;
979
+ }
980
+ }
981
+
965
982
  // src/server/utils/normalize-url.util.ts
966
983
  var UUID_REGEX = /\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?![0-9a-f])/gi;
967
984
  var ULID_REGEX = /\/[0-9A-HJKMNP-TV-Z]{26}(?![0-9A-HJKMNP-TV-Z])/g;
@@ -1004,9 +1021,9 @@ var HttpExceptionFilter = class {
1004
1021
  const userId = req.user?.sub ?? req.user?.id;
1005
1022
  const url = stripQueryString(req.url);
1006
1023
  if (exception instanceof HttpException) {
1007
- const status2 = exception.getStatus();
1008
- const metadata = { method: req.method, url, status: status2 };
1009
- if (status2 >= HttpStatus.INTERNAL_SERVER_ERROR) {
1024
+ const status = exception.getStatus();
1025
+ const metadata = { method: req.method, url, status };
1026
+ if (status >= HttpStatus.INTERNAL_SERVER_ERROR) {
1010
1027
  this.logger.errorStructured(
1011
1028
  RESERVED_LOG_KEYS.HTTP_EXCEPTION_UNHANDLED,
1012
1029
  sanitizeError(exception),
@@ -1021,19 +1038,51 @@ var HttpExceptionFilter = class {
1021
1038
  metadata
1022
1039
  );
1023
1040
  }
1024
- res.status(status2).json(exception.getResponse());
1041
+ res.status(status).json(exception.getResponse());
1025
1042
  return;
1026
1043
  }
1044
+ const clientError = readExposedClientError(exception);
1045
+ if (clientError !== void 0) {
1046
+ this.answerClientError(clientError, res, { method: req.method, url, userId });
1047
+ return;
1048
+ }
1049
+ this.answerUnhandled(exception, res, { method: req.method, url, userId });
1050
+ }
1051
+ /**
1052
+ * Log an exposed client error as handled and answer it with its own status and message.
1053
+ *
1054
+ * @param clientError - The status and message the error exposes.
1055
+ * @param res - The response to write.
1056
+ * @param request - The request facts every entry carries.
1057
+ */
1058
+ answerClientError(clientError, res, request) {
1059
+ const { method, url, userId } = request;
1060
+ this.logger.warnStructured(
1061
+ RESERVED_LOG_KEYS.HTTP_EXCEPTION_HANDLED,
1062
+ clientError.message,
1063
+ userId,
1064
+ { method, url, status: clientError.status }
1065
+ );
1066
+ res.status(clientError.status).json({
1067
+ statusCode: clientError.status,
1068
+ message: clientError.message
1069
+ });
1070
+ }
1071
+ /**
1072
+ * Log any other non-`HttpException` as unhandled and answer it with the fixed 500 body.
1073
+ *
1074
+ * @param exception - The thrown value.
1075
+ * @param res - The response to write.
1076
+ * @param request - The request facts every entry carries.
1077
+ */
1078
+ answerUnhandled(exception, res, request) {
1079
+ const { method, url, userId } = request;
1027
1080
  const status = HttpStatus.INTERNAL_SERVER_ERROR;
1028
1081
  this.logger.errorStructured(
1029
1082
  RESERVED_LOG_KEYS.HTTP_EXCEPTION_UNHANDLED,
1030
1083
  sanitizeError(exception),
1031
1084
  userId,
1032
- {
1033
- method: req.method,
1034
- url,
1035
- status
1036
- }
1085
+ { method, url, status }
1037
1086
  );
1038
1087
  res.status(status).json(INTERNAL_ERROR_BODY);
1039
1088
  }
@@ -1214,8 +1263,8 @@ var HttpLoggingInterceptor = class {
1214
1263
  }
1215
1264
  /**
1216
1265
  * Log a thrown completion: SERVER_ERROR (error level, with stack) for 5xx,
1217
- * CLIENT_ERROR (warn level) for an `HttpException` below 500. A non-`HttpException`
1218
- * always maps to 500, so it is reported as a server error.
1266
+ * CLIENT_ERROR (warn level) for an `HttpException` below 500 or an exposed `http-errors`
1267
+ * client error. Any other non-`HttpException` maps to 500, so it is reported as a server error.
1219
1268
  *
1220
1269
  * @param err - The thrown value (anything).
1221
1270
  * @param method - The HTTP method.
@@ -1243,6 +1292,11 @@ var HttpLoggingInterceptor = class {
1243
1292
  }
1244
1293
  return;
1245
1294
  }
1295
+ const clientError = readExposedClientError(err);
1296
+ if (clientError !== void 0) {
1297
+ this.logClientError(clientError, method, url, userId, duration);
1298
+ return;
1299
+ }
1246
1300
  this.logger.errorStructured(RESERVED_LOG_KEYS.HTTP_REQUEST_SERVER_ERROR, toError(err), userId, {
1247
1301
  method,
1248
1302
  url,
@@ -1250,6 +1304,29 @@ var HttpLoggingInterceptor = class {
1250
1304
  duration
1251
1305
  });
1252
1306
  }
1307
+ /**
1308
+ * Log an exposed `http-errors` client error as CLIENT_ERROR at warn level.
1309
+ *
1310
+ * @param clientError - The status and message the error exposes.
1311
+ * @param method - The HTTP method.
1312
+ * @param url - The normalized request URL.
1313
+ * @param userId - The acting user id, when known.
1314
+ * @param duration - The elapsed time in milliseconds.
1315
+ */
1316
+ logClientError(clientError, method, url, userId, duration) {
1317
+ this.logger.warnStructured(
1318
+ RESERVED_LOG_KEYS.HTTP_REQUEST_CLIENT_ERROR,
1319
+ `${method} ${url} \u2192 ${clientError.status}`,
1320
+ userId,
1321
+ {
1322
+ method,
1323
+ url,
1324
+ statusCode: clientError.status,
1325
+ duration,
1326
+ errorMessage: cutIfText(clientError.message)
1327
+ }
1328
+ );
1329
+ }
1253
1330
  };
1254
1331
  HttpLoggingInterceptor = __decorateClass([
1255
1332
  Injectable(),
@@ -2031,69 +2108,76 @@ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized)
2031
2108
  if (ArrayBuffer.isView(value)) {
2032
2109
  return withBinaryAsSize(value);
2033
2110
  }
2034
- const plain = rules.ownFields.safe;
2035
- const resolvedToJson = resolveToJson(value, plain);
2111
+ const method = resolveToJson(value, rules.ownFields.safe).method;
2036
2112
  ancestors.add(value);
2037
2113
  try {
2038
- const ownsItsForm = rules.ownFields.safe && isError(value);
2039
- const toJson = honorToJson && !ownsItsForm ? resolvedToJson.method : void 0;
2040
- if (toJson !== void 0) {
2041
- if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
2042
- return rules.censor;
2043
- }
2044
- return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
2045
- }
2046
- if (typeof value === "function") {
2047
- return plain ? void 0 : value;
2048
- }
2049
- if (Array.isArray(value)) {
2050
- return walkArray(value, rules, depth2, ancestors, inSerialized);
2051
- }
2052
- const source = value;
2053
- const keys = Object.keys(source);
2054
- const asData = isError(value) && (plain || rules.errorsAsData && !inSerialized);
2055
- const clone = isError(value) ? cloneError(value) : void 0;
2056
- const copy = clone !== void 0 && !asData ? clone : {};
2057
- const scrubbed = isError(value) && (plain || !inSerialized);
2058
- const omitted = /* @__PURE__ */ new Set();
2059
- for (const key of keys) {
2060
- const read = Reflect.get(source, key);
2061
- const current = scrubbed ? ownFieldForLog(source, key, read, rules.ownFields) : read;
2062
- if (current === OMIT_OWN_FIELD) {
2063
- Reflect.deleteProperty(copy, key);
2064
- omitted.add(key);
2065
- continue;
2066
- }
2067
- defineOwn(
2068
- copy,
2069
- key,
2070
- rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
2071
- // An own enumerable `name`, `message` or `stack` of an `Error` is the same text as
2072
- // the part the walk would add, so it is cut the same way.
2073
- scrubbed && ERROR_TEXT_FIELDS.has(key) ? cutIfText(current) : current,
2074
- rules,
2075
- depth2 + 1,
2076
- ancestors,
2077
- true,
2078
- inSerialized || !honorToJson && rules.serializedKeys.has(key)
2079
- )
2080
- );
2081
- }
2082
- if (asData) {
2083
- const written = keys.filter((key) => !omitted.has(key));
2084
- addErrorParts(
2085
- copy,
2086
- value,
2087
- written,
2088
- errorPartsHost(rules, ancestors, inSerialized),
2089
- depth2
2090
- );
2091
- }
2092
- return copy;
2114
+ return walkForm(value, method, rules, depth2, ancestors, honorToJson, inSerialized);
2093
2115
  } finally {
2094
2116
  ancestors.delete(value);
2095
2117
  }
2096
2118
  }
2119
+ function walkForm(value, method, rules, depth2, ancestors, honorToJson, inSerialized) {
2120
+ const plain = rules.ownFields.safe;
2121
+ const toJson = honorToJson && !(plain && isError(value)) ? method : void 0;
2122
+ if (toJson !== void 0) {
2123
+ return walkToJsonOutput(value, toJson, rules, depth2, ancestors, inSerialized);
2124
+ }
2125
+ if (typeof value === "function") {
2126
+ return plain ? void 0 : value;
2127
+ }
2128
+ if (Array.isArray(value)) {
2129
+ return walkArray(value, rules, depth2, ancestors, inSerialized);
2130
+ }
2131
+ return walkProperties(value, rules, depth2, ancestors, honorToJson, inSerialized);
2132
+ }
2133
+ function walkToJsonOutput(value, toJson, rules, depth2, ancestors, inSerialized) {
2134
+ if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
2135
+ return rules.censor;
2136
+ }
2137
+ return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
2138
+ }
2139
+ function walkProperties(value, rules, depth2, ancestors, honorToJson, inSerialized) {
2140
+ const source = value;
2141
+ const keys = Object.keys(source);
2142
+ const { safe } = rules.ownFields;
2143
+ const asData = isError(value) && (safe || rules.errorsAsData && !inSerialized);
2144
+ const copy = startCopy(value, asData);
2145
+ const scrubbed = isError(value) && (safe || !inSerialized);
2146
+ const omitted = /* @__PURE__ */ new Set();
2147
+ for (const key of keys) {
2148
+ const read = Reflect.get(source, key);
2149
+ const current = scrubbed ? ownFieldForLog(source, key, read, rules.ownFields) : read;
2150
+ if (current === OMIT_OWN_FIELD) {
2151
+ Reflect.deleteProperty(copy, key);
2152
+ omitted.add(key);
2153
+ continue;
2154
+ }
2155
+ defineOwn(
2156
+ copy,
2157
+ key,
2158
+ rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
2159
+ scrubbed && ERROR_TEXT_FIELDS.has(key) ? cutIfText(current) : current,
2160
+ rules,
2161
+ depth2 + 1,
2162
+ ancestors,
2163
+ true,
2164
+ inSerialized || !honorToJson && rules.serializedKeys.has(key)
2165
+ )
2166
+ );
2167
+ }
2168
+ if (asData) {
2169
+ const host = errorPartsHost(rules, ancestors, inSerialized);
2170
+ addErrorParts(copy, value, without(keys, omitted), host, depth2);
2171
+ }
2172
+ return copy;
2173
+ }
2174
+ function without(keys, omitted) {
2175
+ return keys.filter((key) => !omitted.has(key));
2176
+ }
2177
+ function startCopy(value, asData) {
2178
+ const clone = isError(value) ? cloneError(value) : void 0;
2179
+ return clone !== void 0 && !asData ? clone : {};
2180
+ }
2097
2181
  function errorPartsHost(rules, ancestors, inSerialized) {
2098
2182
  return {
2099
2183
  sensitive: rules.sensitive,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bymax-one/nest-logger",
3
- "version": "1.4.12",
3
+ "version": "1.4.13",
4
4
  "description": "Structured JSON logging for NestJS based on Pino 10, with optional OpenTelemetry correlation.",
5
5
  "author": "Bymax One <support@bymax.one>",
6
6
  "license": "MIT",
@@ -61,6 +61,7 @@
61
61
  "check:docs": "node scripts/check-doc-snippets.mjs",
62
62
  "check:mutants": "node scripts/check-mutation-directives.mjs",
63
63
  "check:published": "node scripts/check-published-surface.mjs",
64
+ "check:file-length": "node scripts/check-file-length.mjs",
64
65
  "clean": "rm -rf dist coverage",
65
66
  "lint": "eslint src scripts test bench '*.config.ts' '*.config.mjs' '*.config.cjs'",
66
67
  "lint:fix": "eslint src scripts test bench '*.config.ts' '*.config.mjs' '*.config.cjs' --fix",