fix(providers): add safe json response helper · openclaw/openclaw@d16f79f
vincentkoc
·
2026-05-15
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import {
|
4 | 4 | assertOkOrThrowHttpError, |
5 | 5 | extractProviderErrorDetail, |
6 | 6 | extractProviderRequestId, |
| 7 | +readProviderJsonResponse, |
7 | 8 | } from "./provider-http-errors.js"; |
8 | 9 | |
9 | 10 | describe("provider error utils", () => { |
@@ -54,4 +55,15 @@ describe("provider error utils", () => {
|
54 | 55 | "Legacy provider error (HTTP 400): Bad request [code=invalid_request] [request_id=req_legacy]", |
55 | 56 | ); |
56 | 57 | }); |
| 58 | + |
| 59 | +it("wraps malformed successful JSON responses with provider labels", async () => { |
| 60 | +const response = new Response("{ nope", { |
| 61 | +status: 200, |
| 62 | +headers: { "content-type": "application/json" }, |
| 63 | +}); |
| 64 | + |
| 65 | +await expect(readProviderJsonResponse(response, "Provider catalog failed")).rejects.toThrow( |
| 66 | +"Provider catalog failed: malformed JSON response", |
| 67 | +); |
| 68 | +}); |
57 | 69 | }); |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -163,3 +163,11 @@ export async function assertOkOrThrowHttpError(response: Response, label: string
|
163 | 163 | } |
164 | 164 | throw await createProviderHttpError(response, label, { statusPrefix: "HTTP " }); |
165 | 165 | } |
| 166 | + |
| 167 | +export async function readProviderJsonResponse<T>(response: Response, label: string): Promise<T> { |
| 168 | +try { |
| 169 | +return (await response.json()) as T; |
| 170 | +} catch (cause) { |
| 171 | +throw new Error(`${label}: malformed JSON response`, { cause }); |
| 172 | +} |
| 173 | +} |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ export {
|
9 | 9 | extractProviderRequestId, |
10 | 10 | formatProviderErrorPayload, |
11 | 11 | formatProviderHttpErrorMessage, |
| 12 | +readProviderJsonResponse, |
12 | 13 | readResponseTextLimited, |
13 | 14 | truncateErrorDetail, |
14 | 15 | } from "../agents/provider-http-errors.js"; |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。