@buka/nestjs-kit 4.1.1 → 4.1.2

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
@@ -8,9 +8,9 @@ import { ApiExtraModels, ApiHideProperty, ApiOkResponse, ApiOperation, ApiProper
8
8
  import { Transform, Type as Type$1, plainToInstance } from "class-transformer";
9
9
  import * as R from "ramda";
10
10
  import * as classTransformer from "class-transformer/cjs/storage.js";
11
- import { ModelPropertiesAccessor } from "@nestjs/swagger/dist/services/model-properties-accessor.js";
12
- import { DECORATORS } from "@nestjs/swagger/dist/constants.js";
13
- import { METADATA_FACTORY_NAME } from "@nestjs/swagger/dist/plugin/plugin-constants.js";
11
+ import { ModelPropertiesAccessor } from "@nestjs/swagger/dist/services/model-properties-accessor";
12
+ import { DECORATORS } from "@nestjs/swagger/dist/constants";
13
+ import { METADATA_FACTORY_NAME } from "@nestjs/swagger/dist/plugin/plugin-constants";
14
14
  import { clonePluginMetadataFactory } from "@nestjs/swagger/dist/type-helpers/mapped-types.utils";
15
15
  import { isFunction } from "@nestjs/common/utils/shared.utils";
16
16
  import "@nestjs/swagger/dist/utils/is-built-in-type.util";
@@ -8063,6 +8063,51 @@ function setOpenBaoToken(getToken) {
8063
8063
  return middleware;
8064
8064
  }
8065
8065
  //#endregion
8066
+ //#region src/modules/open-bao/request/format-error.ts
8067
+ /**
8068
+ * 根据 HTTP 状态码生成中文操作建议
8069
+ */
8070
+ function getStatusCodeGuidance(statusCode) {
8071
+ if (statusCode === 400) return "请检查请求参数是否正确";
8072
+ if (statusCode === 403) return "请检查 OpenBao 策略(Policy)是否为该 Token 授予了足够的权限";
8073
+ if (statusCode === 404) return "请检查请求的路径或密钥是否存在";
8074
+ if (statusCode >= 500) return "请检查 OpenBao 服务器日志以获取更多信息";
8075
+ return "";
8076
+ }
8077
+ /**
8078
+ * OpenBao 请求错误格式化中间件
8079
+ *
8080
+ * 必须注册在 `validateStatusCode` 之后,作为中间件链的最外层,
8081
+ * 捕获 `RequestException` 并重写其错误消息,补充请求上下文和操作建议。
8082
+ *
8083
+ * 非 RequestException 错误(网络故障、超时等)原样透传。
8084
+ */
8085
+ function formatError() {
8086
+ const middleware = async (ctx, next) => {
8087
+ try {
8088
+ await next();
8089
+ } catch (err) {
8090
+ if (!(err instanceof RequestException)) throw err;
8091
+ const method = ctx.request.method.toUpperCase();
8092
+ const url = ctx.request.url.toString();
8093
+ const statusCode = err.statusCode;
8094
+ const statusText = err.response?.statusText ?? "";
8095
+ let message = `OpenBaoModule API 请求失败: ${method} ${url}`;
8096
+ message += `\n响应状态: ${String(statusCode)} ${statusText}`;
8097
+ if (err.response) try {
8098
+ const bodyText = await err.response.clone().text();
8099
+ if (bodyText) message += `\n响应体: ${bodyText}`;
8100
+ } catch {}
8101
+ const guidance = getStatusCodeGuidance(typeof statusCode === "number" ? statusCode : parseInt(String(statusCode), 10));
8102
+ if (guidance) message += `\n建议: ${guidance}`;
8103
+ err.message = message;
8104
+ throw err;
8105
+ }
8106
+ };
8107
+ middleware.__keqMiddlewareName__ = "formatError";
8108
+ return middleware;
8109
+ }
8110
+ //#endregion
8066
8111
  //#region src/modules/open-bao/open-bao.module.ts
8067
8112
  /**
8068
8113
  * 内部 KeqRequest 注入 Token
@@ -8078,6 +8123,7 @@ const KEQ_REQUEST_PROVIDER = {
8078
8123
  request.use(setBaseUrl(options.address + "/v1/"));
8079
8124
  request.use(setOpenBaoToken(() => tokenManager.getToken()));
8080
8125
  request.use(validateStatusCode());
8126
+ request.use(formatError());
8081
8127
  return request;
8082
8128
  },
8083
8129
  inject: [MODULE_OPTIONS_TOKEN$3, OpenBaoTokenManager]