koishi-plugin-rocom 1.0.6 → 1.0.7

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/lib/client.d.ts CHANGED
@@ -4,11 +4,14 @@ export declare class RocomClient {
4
4
  private apiKey;
5
5
  private timeout;
6
6
  private lastError;
7
+ private lastErrorBrief;
7
8
  constructor(baseUrl: string, apiKey: string, timeout?: number);
8
9
  private sanitizeUid;
9
10
  private wegameHeaders;
10
11
  private rocomHeaders;
11
12
  private formatHttpError;
13
+ private simplifyErrorMessage;
14
+ private shouldRetryIngameWithApiKey;
12
15
  private isSensitiveLogKey;
13
16
  private maskSensitiveValue;
14
17
  private sanitizeForLog;
@@ -32,6 +35,7 @@ export declare class RocomClient {
32
35
  getRole(ctx: Context, fwToken: string, accountType?: number, userIdentifier?: string): Promise<any>;
33
36
  getEvaluation(ctx: Context, fwToken: string, userIdentifier?: string): Promise<any>;
34
37
  getLastError(defaultMessage?: string): string;
38
+ getLastErrorBrief(defaultMessage?: string): string;
35
39
  private setLastError;
36
40
  getPetSummary(ctx: Context, fwToken: string, userIdentifier?: string): Promise<any>;
37
41
  getCollection(ctx: Context, fwToken: string, userIdentifier?: string): Promise<any>;
package/lib/client.js CHANGED
@@ -8,6 +8,7 @@ class RocomClient {
8
8
  apiKey;
9
9
  timeout;
10
10
  lastError = '接口异常';
11
+ lastErrorBrief = '接口异常';
11
12
  constructor(baseUrl, apiKey, timeout = 15000) {
12
13
  this.baseUrl = baseUrl.replace(/\/$/, '');
13
14
  this.apiKey = apiKey;
@@ -53,6 +54,56 @@ class RocomClient {
53
54
  }
54
55
  return err?.message || String(e);
55
56
  }
57
+ simplifyErrorMessage(message) {
58
+ const raw = String(message || '').replace(/\s+/g, ' ').trim();
59
+ if (!raw)
60
+ return '接口异常';
61
+ if (/ETIMEDOUT|request timeout|timeout/i.test(raw)) {
62
+ return '请求超时,请稍后重试';
63
+ }
64
+ if (/ENOTFOUND|EAI_AGAIN|ECONNRESET|ECONNREFUSED|socket hang up|fetch failed|network error/i.test(raw)) {
65
+ return '网络连接异常,请稍后重试';
66
+ }
67
+ if (/底层凭证已失效|WeGame 凭证已失效|凭证已失效|token expired|framework token/i.test(raw)) {
68
+ return '登录凭证已失效,请重新登录';
69
+ }
70
+ if (/HTTP\s*5\d\d/i.test(raw)) {
71
+ return '服务暂时不可用,请稍后重试';
72
+ }
73
+ if (/HTTP\s*401/i.test(raw)) {
74
+ return '登录状态失效,请重新登录';
75
+ }
76
+ if (/API[\s_-]*Key/i.test(raw) && /未声明|默认拒绝访问|not declared|not allowed/i.test(raw)) {
77
+ return '接口权限受限,请稍后重试';
78
+ }
79
+ if (/HTTP\s*403/i.test(raw) && !/凭证|登录|token/i.test(raw)) {
80
+ return '权限不足,暂时无法访问该接口';
81
+ }
82
+ const stripped = raw
83
+ .replace(/^HTTP\s*\d+\s*:\s*/i, '')
84
+ .replace(/^(Error|RequestError)\s*:\s*/i, '')
85
+ .trim();
86
+ if (!stripped)
87
+ return '接口异常';
88
+ return stripped.length > 120 ? `${stripped.slice(0, 117)}...` : stripped;
89
+ }
90
+ shouldRetryIngameWithApiKey(errorMessage) {
91
+ if (!this.apiKey)
92
+ return false;
93
+ const message = String(errorMessage || '').trim();
94
+ if (!message)
95
+ return true;
96
+ if (/未声明\s*API\s*Key\s*权限|默认拒绝访问|API\s*Key.*not declared|API\s*Key.*not allowed/i.test(message)) {
97
+ return false;
98
+ }
99
+ if (/缺少\s*API\s*Key|API\s*Key.*required|missing\s+api\s*key/i.test(message)) {
100
+ return true;
101
+ }
102
+ if (/HTTP\s*(401|403)/i.test(message)) {
103
+ return true;
104
+ }
105
+ return false;
106
+ }
56
107
  isSensitiveLogKey(key) {
57
108
  return /api[-_]?key|authorization|cookie|framework[-_]?token|password|secret|ticket|token/i.test(key);
58
109
  }
@@ -275,7 +326,7 @@ class RocomClient {
275
326
  let result = await requestOnce(false, Boolean(this.apiKey));
276
327
  if (result.status !== null)
277
328
  return { ...result, usedApiKey: false };
278
- if (this.apiKey) {
329
+ if (this.shouldRetryIngameWithApiKey(this.getLastError())) {
279
330
  result = await requestOnce(true, false);
280
331
  if (result.status !== null)
281
332
  return { ...result, usedApiKey: true };
@@ -339,8 +390,12 @@ class RocomClient {
339
390
  getLastError(defaultMessage = '接口异常') {
340
391
  return this.lastError || defaultMessage;
341
392
  }
393
+ getLastErrorBrief(defaultMessage = '接口异常') {
394
+ return this.lastErrorBrief || defaultMessage;
395
+ }
342
396
  setLastError(message) {
343
397
  this.lastError = message || '接口异常';
398
+ this.lastErrorBrief = this.simplifyErrorMessage(message);
344
399
  }
345
400
  async getPetSummary(ctx, fwToken, userIdentifier = '') {
346
401
  return this.get(ctx, '/api/v1/games/rocom/profile/pet-summary', this.rocomHeaders(fwToken, userIdentifier));
@@ -213,7 +213,7 @@ function register(deps) {
213
213
  .action(async ({ session }) => {
214
214
  const res = await client.getMerchantInfo(ctx, true);
215
215
  if (!res)
216
- return '\u83b7\u53d6\u8fdc\u884c\u5546\u4eba\u6570\u636e\u5931\u8d25\u3002';
216
+ return `\u83b7\u53d6\u8fdc\u884c\u5546\u4eba\u6570\u636e\u5931\u8d25\uff1a${client.getLastErrorBrief()}`;
217
217
  const { data, fallback } = buildMerchantRenderPayload(res);
218
218
  const png = await deps.renderer.renderHtml(ctx, 'yuanxing-shangren', data);
219
219
  await (0, send_image_1.sendImageWithFallback)(session, png, fallback, 'merchant:yuanxing-shangren', deps.config);