Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: recognize JSON device gateway dispatch errors (#19692)

Arvin Xuafc7b01320b6
diff --git a/apps/server/src/services/aiAgent/helpers/heteroErrors.test.ts b/apps/server/src/services/aiAgent/helpers/heteroErrors.test.ts
index 905be5d392e..ffef031ef0c 100644
--- a/apps/server/src/services/aiAgent/helpers/heteroErrors.test.ts
+++ b/apps/server/src/services/aiAgent/helpers/heteroErrors.test.ts
@@ -52,3 +52,29 @@ describe('resolveHeteroDispatchErrorType', () => {
expect(resolveHeteroDispatchErrorType()).toBe(ChatErrorType.ServerAgentRuntimeError);
});
});
+
+describe('JSON device-gateway dispatch errors', () => {
+ it.each(['DEVICE_NOT_FOUND', 'DEVICE_OFFLINE', 'DEVICE_CHANNEL_UNAVAILABLE'])(
+ 'explains %s and selects the localized device error',
+ (code) => {
+ const raw = JSON.stringify({ error: code, success: false });
+
+ expect(humanizeHeteroDispatchError(raw)).toBe(humanizeHeteroDispatchError(code));
+ expect(resolveHeteroDispatchErrorType(raw)).toBe(ChatErrorType.DeviceGatewayNotConfigured);
+ },
+ );
+
+ it.each([
+ '{"error":"UNKNOWN_DEVICE_ERROR","success":false}',
+ '{"error":{"code":"DEVICE_NOT_FOUND"}}',
+ '{"error":null}',
+ '{"error":42}',
+ '{"success":false}',
+ '{"error":"DEVICE_NOT_FOUND"',
+ 'null',
+ '[]',
+ ])('preserves unrecognized or malformed responses: %s', (raw) => {
+ expect(humanizeHeteroDispatchError(raw)).toBe(raw);
+ expect(resolveHeteroDispatchErrorType(raw)).toBe(ChatErrorType.ServerAgentRuntimeError);
+ });
+});
diff --git a/apps/server/src/services/aiAgent/helpers/heteroErrors.ts b/apps/server/src/services/aiAgent/helpers/heteroErrors.ts
index 68b91301777..4c2d34bd5e9 100644
--- a/apps/server/src/services/aiAgent/helpers/heteroErrors.ts
+++ b/apps/server/src/services/aiAgent/helpers/heteroErrors.ts
@@ -39,11 +39,26 @@ export const HETERO_DISPATCH_ERROR_HEADLINES: Record<string, string> = {
* The gateway may answer with a bare code (`DEVICE_OFFLINE`) or with a code the
* device-gateway client annotated with the status it came from
* (`DEVICE_CHANNEL_UNAVAILABLE (HTTP 503)`). Look the headline up by the code
- * itself so the annotation doesn't cost the user a readable message; `detail`
- * keeps the full raw string for diagnostics either way.
+ * itself so the annotation doesn't cost the user a readable message. Non-2xx
+ * responses can also carry a JSON envelope whose `error` field holds the code.
+ * `detail` keeps the full raw string for diagnostics in either format.
*/
-const toDispatchErrorCode = (raw?: string): string | undefined =>
- raw?.trim().match(/^([A-Z][\dA-Z_]*)/)?.[1];
+const toDispatchErrorCode = (raw?: string): string | undefined => {
+ let text = raw?.trim();
+
+ if (text?.startsWith('{')) {
+ try {
+ const body: unknown = JSON.parse(text);
+ if (body && typeof body === 'object' && 'error' in body && typeof body.error === 'string') {
+ text = body.error.trim();
+ }
+ } catch {
+ // Preserve malformed gateway responses as diagnostic text.
+ }
+ }
+
+ return text?.match(/^([A-Z][\dA-Z_]*)/)?.[1];
+};
export const humanizeHeteroDispatchError = (raw?: string): string => {
const code = toDispatchErrorCode(raw);