@smart-cloud/ai-kit-ui 1.5.5 → 1.5.6
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/errorHandling.d.ts +3 -0
- package/dist/index.cjs +6 -6
- package/dist/index.js +6 -6
- package/package.json +1 -1
- package/src/errorHandling.ts +43 -1
- package/tests/error-handling.test.ts +47 -6
package/package.json
CHANGED
package/src/errorHandling.ts
CHANGED
|
@@ -8,12 +8,20 @@ export type AiRunErrorKind =
|
|
|
8
8
|
| "server"
|
|
9
9
|
| "general";
|
|
10
10
|
|
|
11
|
+
export type HumanVerificationClassification =
|
|
12
|
+
| "TOKEN_REJECTED"
|
|
13
|
+
| "ACTION_REJECTED"
|
|
14
|
+
| "RISK_REJECTED"
|
|
15
|
+
| "PROVIDER_UNAVAILABLE";
|
|
16
|
+
|
|
11
17
|
export type AiRunErrorDetails = {
|
|
12
18
|
kind: AiRunErrorKind;
|
|
13
19
|
status?: number;
|
|
14
20
|
code?: string;
|
|
15
21
|
safeMessage?: string;
|
|
16
22
|
requestId?: string;
|
|
23
|
+
classification?: HumanVerificationClassification;
|
|
24
|
+
retryable?: boolean;
|
|
17
25
|
};
|
|
18
26
|
|
|
19
27
|
export type AiRunErrorFeedback = {
|
|
@@ -47,6 +55,17 @@ function asStatus(value: unknown): number | undefined {
|
|
|
47
55
|
: undefined;
|
|
48
56
|
}
|
|
49
57
|
|
|
58
|
+
function asHumanVerificationClassification(
|
|
59
|
+
value: unknown,
|
|
60
|
+
): HumanVerificationClassification | undefined {
|
|
61
|
+
return value === "TOKEN_REJECTED" ||
|
|
62
|
+
value === "ACTION_REJECTED" ||
|
|
63
|
+
value === "RISK_REJECTED" ||
|
|
64
|
+
value === "PROVIDER_UNAVAILABLE"
|
|
65
|
+
? value
|
|
66
|
+
: undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
50
69
|
function parsePayload(value: unknown): UnknownRecord | null {
|
|
51
70
|
const record = asRecord(value);
|
|
52
71
|
if (record) return record;
|
|
@@ -169,6 +188,16 @@ export function normalizeAiRunError(error: unknown): AiRunErrorDetails {
|
|
|
169
188
|
headerValue(record.headers ?? asRecord(record.response)?.headers, "x-request-id"),
|
|
170
189
|
)
|
|
171
190
|
.find(Boolean);
|
|
191
|
+
const classification = records
|
|
192
|
+
.map((record) =>
|
|
193
|
+
asHumanVerificationClassification(record.classification),
|
|
194
|
+
)
|
|
195
|
+
.find((value) => value !== undefined);
|
|
196
|
+
const retryable = records
|
|
197
|
+
.map((record) =>
|
|
198
|
+
typeof record.retryable === "boolean" ? record.retryable : undefined,
|
|
199
|
+
)
|
|
200
|
+
.find((value) => value !== undefined);
|
|
172
201
|
|
|
173
202
|
const normalizedCode = (code ?? "").toUpperCase();
|
|
174
203
|
const combined = `${normalizedCode} ${safeMessage ?? ""}`;
|
|
@@ -201,13 +230,26 @@ export function normalizeAiRunError(error: unknown): AiRunErrorDetails {
|
|
|
201
230
|
kind = "server";
|
|
202
231
|
}
|
|
203
232
|
|
|
204
|
-
return {
|
|
233
|
+
return {
|
|
234
|
+
kind,
|
|
235
|
+
status,
|
|
236
|
+
code,
|
|
237
|
+
safeMessage,
|
|
238
|
+
requestId,
|
|
239
|
+
...(classification ? { classification } : {}),
|
|
240
|
+
...(retryable === undefined ? {} : { retryable }),
|
|
241
|
+
};
|
|
205
242
|
}
|
|
206
243
|
|
|
207
244
|
export function getAiRunErrorMessage(
|
|
208
245
|
details: AiRunErrorDetails,
|
|
209
246
|
translate: (message: string) => string = (message) => message,
|
|
210
247
|
): string {
|
|
248
|
+
if (details.classification === "PROVIDER_UNAVAILABLE") {
|
|
249
|
+
return translate(
|
|
250
|
+
"Human verification is temporarily unavailable. Please try again.",
|
|
251
|
+
);
|
|
252
|
+
}
|
|
211
253
|
const messages: Record<AiRunErrorKind, string> = {
|
|
212
254
|
cancelled: "",
|
|
213
255
|
"human-verification":
|
|
@@ -23,19 +23,25 @@ function backendError(status: number, body: Record<string, unknown>) {
|
|
|
23
23
|
test("normalizes reCAPTCHA rejection without exposing request data", () => {
|
|
24
24
|
const details = normalizeAiRunError(
|
|
25
25
|
backendError(403, {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
error: {
|
|
27
|
+
code: "HUMAN_VERIFICATION_FAILED",
|
|
28
|
+
classification: "RISK_REJECTED",
|
|
29
|
+
message: "reCAPTCHA verification failed",
|
|
30
|
+
retryable: true,
|
|
31
|
+
requestId: "req-human-1",
|
|
32
|
+
token: "must-not-leak",
|
|
33
|
+
},
|
|
30
34
|
}),
|
|
31
35
|
);
|
|
32
36
|
|
|
33
37
|
assert.deepEqual(details, {
|
|
34
38
|
kind: "human-verification",
|
|
35
39
|
status: 403,
|
|
36
|
-
code: "
|
|
37
|
-
safeMessage: "
|
|
40
|
+
code: "HUMAN_VERIFICATION_FAILED",
|
|
41
|
+
safeMessage: "reCAPTCHA verification failed",
|
|
38
42
|
requestId: "req-human-1",
|
|
43
|
+
classification: "RISK_REJECTED",
|
|
44
|
+
retryable: true,
|
|
39
45
|
});
|
|
40
46
|
assert.equal(
|
|
41
47
|
getAiRunErrorMessage(details),
|
|
@@ -44,6 +50,41 @@ test("normalizes reCAPTCHA rejection without exposing request data", () => {
|
|
|
44
50
|
assert.doesNotMatch(JSON.stringify(details), /must-not-leak/);
|
|
45
51
|
});
|
|
46
52
|
|
|
53
|
+
test("normalizes a reCAPTCHA provider outage as retryable human verification", () => {
|
|
54
|
+
const details = normalizeAiRunError(
|
|
55
|
+
backendError(503, {
|
|
56
|
+
error: {
|
|
57
|
+
code: "HUMAN_VERIFICATION_UNAVAILABLE",
|
|
58
|
+
classification: "PROVIDER_UNAVAILABLE",
|
|
59
|
+
message: "reCAPTCHA verification is temporarily unavailable",
|
|
60
|
+
retryable: true,
|
|
61
|
+
requestId: "req-human-503",
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
assert.equal(details.kind, "human-verification");
|
|
67
|
+
assert.equal(details.classification, "PROVIDER_UNAVAILABLE");
|
|
68
|
+
assert.equal(details.retryable, true);
|
|
69
|
+
assert.equal(
|
|
70
|
+
getAiRunErrorMessage(details),
|
|
71
|
+
"Human verification is temporarily unavailable. Please try again.",
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("continues to recognize the legacy AI Kit reCAPTCHA response during rollout", () => {
|
|
76
|
+
const details = normalizeAiRunError(
|
|
77
|
+
backendError(403, {
|
|
78
|
+
code: "HUMAN_VERIFICATION_ERROR",
|
|
79
|
+
message: "Assessment rejected",
|
|
80
|
+
requestId: "req-human-legacy",
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
assert.equal(details.kind, "human-verification");
|
|
85
|
+
assert.equal(details.classification, undefined);
|
|
86
|
+
});
|
|
87
|
+
|
|
47
88
|
test("distinguishes authorization, throttling, network, and backend failures", () => {
|
|
48
89
|
assert.equal(
|
|
49
90
|
normalizeAiRunError(backendError(401, { code: "UNAUTHORIZED" })).kind,
|