auto(bfc0cfa2c3a4): session end @ 2026-07-20T22:28:44 (+1f)
This commit is contained in:
1 parent
e10086fcea
commit
7997d73db9
1 file changed
+58
-7
+58
-7
@@ -195,7 +195,7 @@ export class JnpfClient {
|
||||
}
|
||||
log.debug('GET', url)
|
||||
const res = await fetch(url, { method: 'GET', headers: this.headers() })
|
||||
return parseJson<T>(await res.text())
|
||||
return parseJson<T>(await res.text(), path, res.status, 'GET')
|
||||
}
|
||||
|
||||
async postJson<T = unknown>(path: string, body: unknown): Promise<JnpfResp<T>> {
|
||||
@@ -206,7 +206,7 @@ export class JnpfClient {
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
return parseJson<T>(await res.text())
|
||||
return parseJson<T>(await res.text(), path, res.status, 'POST')
|
||||
}
|
||||
|
||||
async putJson<T = unknown>(path: string, body: unknown): Promise<JnpfResp<T>> {
|
||||
@@ -217,7 +217,7 @@ export class JnpfClient {
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
return parseJson<T>(await res.text())
|
||||
return parseJson<T>(await res.text(), path, res.status, 'PUT')
|
||||
}
|
||||
|
||||
// ----- OnlineDev CRUD wrappers (BS-53 fix: data 必须 JSON.stringify 后包成 {data: ...}) -----
|
||||
@@ -257,7 +257,7 @@ export class JnpfClient {
|
||||
method: 'DELETE',
|
||||
headers: this.headers(),
|
||||
})
|
||||
return parseJson<T>(await res.text())
|
||||
return parseJson<T>(await res.text(), path, res.status, 'DELETE')
|
||||
}
|
||||
|
||||
/** 业务码非 200 直接抛 */
|
||||
@@ -474,10 +474,61 @@ export class JnpfClient {
|
||||
}
|
||||
}
|
||||
|
||||
function parseJson<T>(text: string): JnpfResp<T> {
|
||||
/**
|
||||
* 解析 HTTP 响应 body 为 JnpfResp<T>. 严格校验 JNPF 信封形状 + HTTP 状态.
|
||||
*
|
||||
* 背景 (BS-新\datascreen 2026-07-20): 旧版 parseJson 只调 JSON.parse 然后 cast 成
|
||||
* JnpfResp<T>, 当上游返 5xx + Spring Boot 错误 JSON `{timestamp,status,error,path}`
|
||||
* (无 JNPF 信封字段) 或 HTML 错误页 (非 JSON) 时, 旧版 "code=undefined" 沉默失败,
|
||||
* 让运维瞎猜上游微服务 / nginx / jnpf-admin 哪里坏.
|
||||
*
|
||||
* 新版:
|
||||
* - HTTP 4xx/5xx + JSON: 抽 Spring Boot error.message/path, throw "HTTP 503 from POST /path: ..."
|
||||
* - HTTP 4xx/5xx + 非 JSON (HTML): throw "HTTP 502 from POST /path: <body[:200]>"
|
||||
* - HTTP 2xx + 非信封 JSON (无 code+data): throw "non-Jnpf envelope" 含 path + body[:200]
|
||||
* - HTTP 2xx + 信封 (有 code): 正常路径 (返 unwrapped data 时由 shim.unwrap 接管)
|
||||
* - fetch 网络错 (上层 fetch throw): 不进 parseJson, 直接透传 (场景 8 测)
|
||||
*/
|
||||
function parseJson<T>(text: string, path = '<unknown>', httpStatus = 0, method = 'POST'): JnpfResp<T> {
|
||||
// 1. JSON 解析失败 — 多半是 HTML 错误页 / nginx upstream 502 / 504
|
||||
let parsed: unknown
|
||||
try {
|
||||
return JSON.parse(text) as JnpfResp<T>
|
||||
parsed = JSON.parse(text)
|
||||
} catch {
|
||||
return { code: -1, msg: `JSON parse error: ${text.slice(0, 200)}`, data: null }
|
||||
if (httpStatus >= 400) {
|
||||
throw new Error(
|
||||
`HTTP ${httpStatus} from ${method} ${path}: ${text.slice(0, 200)}`,
|
||||
)
|
||||
}
|
||||
// 2xx + 非 JSON: 也是异常形状 (例如 Spring 返回了 [1,2,3] 但客户端期望 envelope)
|
||||
throw new Error(
|
||||
`unexpected response shape from ${method} ${path} (HTTP ${httpStatus || '?'}, non-JSON): ${text.slice(0, 200)}`,
|
||||
)
|
||||
}
|
||||
|
||||
// 2. JSON 解析成功 — 必须含 JNPF 信封字段 (code + data)
|
||||
if (
|
||||
parsed !== null &&
|
||||
typeof parsed === 'object' &&
|
||||
!Array.isArray(parsed) &&
|
||||
'code' in parsed
|
||||
) {
|
||||
return parsed as JnpfResp<T>
|
||||
}
|
||||
|
||||
// 3. 非信封 JSON. HTTP 4xx/5xx + 可能是 Spring Boot 错误 JSON (已知字段: error/message/path/timestamp)
|
||||
const sb = parsed as { error?: string; message?: string; path?: string; status?: number }
|
||||
if (httpStatus >= 400) {
|
||||
// Spring Boot 错误体默认字段 {timestamp,status,error,path,message}, 用 message/error/path 拼诊断
|
||||
const errMsg = sb.message || sb.error || text.slice(0, 200)
|
||||
const errPath = sb.path || path
|
||||
throw new Error(
|
||||
`HTTP ${httpStatus} from ${method} ${errPath}: ${errMsg}`,
|
||||
)
|
||||
}
|
||||
|
||||
// 4. HTTP 2xx + 非信封 JSON — 调用方完全无法 unwrap data, 直接抛
|
||||
throw new Error(
|
||||
`unexpected response shape from ${method} ${path} (non-Jnpf envelope, HTTP ${httpStatus || '?'}): ${text.slice(0, 200)}`,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user