@buka/nestjs-kit 4.4.9 → 4.4.10

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/dist/index.mjs CHANGED
@@ -8409,8 +8409,14 @@ function throwOnResponseError(options) {
8409
8409
  if (!ctx.response || ctx.response.status >= 200 && ctx.response.status < 400) return;
8410
8410
  const response = ctx.response;
8411
8411
  if (!response.headers.get("content-type")?.includes("application/json")) throw createExceptionByStatusCode(response);
8412
- const body = await response.json();
8413
- if (body !== null && typeof body === "object" && "error" in body) try {
8412
+ let body;
8413
+ try {
8414
+ body = await response.json();
8415
+ } catch (err) {
8416
+ if (debug) logger.debug("响应体 JSON 解析失败,降级为状态码异常", err instanceof Error ? err.stack : err);
8417
+ throw createExceptionByStatusCode(response);
8418
+ }
8419
+ if (body !== null && typeof body === "object" && "error" in body) {
8414
8420
  const { error } = body;
8415
8421
  const exceptionOptions = {
8416
8422
  code: error.code,
@@ -8422,13 +8428,13 @@ function throwOnResponseError(options) {
8422
8428
  404
8423
8429
  ].includes(response.status)
8424
8430
  };
8425
- if (errorDispatchers && error.code in errorDispatchers) {
8431
+ if (errorDispatchers && error.code in errorDispatchers) try {
8426
8432
  const ExceptionClass = errorDispatchers[error.code];
8427
8433
  throw new ExceptionClass(response.status, error.message, exceptionOptions);
8434
+ } catch (caught) {
8435
+ if (debug) logger.debug("自定义异常派发失败,降级为 BukaRequestException", caught instanceof Error ? caught.stack : caught);
8428
8436
  }
8429
8437
  throw new BukaRequestException(response.status, error.message, exceptionOptions);
8430
- } catch (caught) {
8431
- if (debug) logger.debug("解析响应错误体失败,降级为通用异常", caught instanceof Error ? caught.stack : caught);
8432
8438
  }
8433
8439
  throw createExceptionByStatusCode(response);
8434
8440
  };