gpt4 book ai didi

javascript - fetch 返回 ReadableStream 作为错误主体。我如何获取该流的内容?

转载 作者:行者123 更新时间:2023-11-28 03:41:37 25 4
gpt4 key购买 nike

我正在使用 ODATA NPM 包通过 REST 调用获取 ODATA。 (我认为这基本上只是一个用于获取的包装器)。

嗯,我正在使用await/try/catch来捕获错误,并且我想获取错误的正文。一定有比我正在做的更好的方法:

如果我这样做:

const setOperations = (async (jobNum) => {
let reply = {}
let payload = { ...OpData }
payload.JobNum = jobNum
try {
reply = await o(JOBURL, options).post('JobOpers', payload).query()
} catch (error) {
console.log(`Error Code: ${error.status}`)
console.log(`Error URL: ${error.url}`)
const reader = error.body.getReader()
const text = await reader.read()
console.log(`Error body: ${text.value}`)
}
return reply
})

那么显示的text.value就是一个Uint8Array字节流:

Error Code: 400
ERPStore.js:219 Error URL: https://pwsepicorapp.mycorp.com/ERP10.2Test/api/v1/Erp.BO.JobEntrySvc/JobOpers
ERPStore.js:222 Error body: 123,34,72,116,116,112,83,116,97,116,117,115,34,58,52,48,48,44,34,82,101,97,115,111,110,80,104,114,97,115,101,34,58,34,82,69,83,84,32,65,80,73,32,69,120,99,101,112,116,105,111,110,34,44,34,69,114,114,111,114,77,101,115,115,97,103,101,34,58,34,79,112,101,114,97,116,105,111,110,32,114,101,102,101,114,101,110,99,101,115,32,105,110,118,97,108,105,100,32,118,97,108,117,101,46,34,44,34,69,114,114,111,114,84,121,112,101,34,58,34,73,99,101,46,67,111,109,109,111,110,46,66,117,115,105,110,101,115,115,79,98,106,101,99,116,69,120,99,101,112,116,105,111,110,34,44,34,69,114,114,111,114,68,101,116,97,105,108,115,34,58,91,123,34,77,101,115,115,97,103,101,34,58,34,79,112,101,114,97,116,105,111,110,32,114,101,102,101,114,101,110,99,101,115,32,105,110,118,97,108,105,100,32,118,97,108,117,101,46,34,44,34,84,121,112,101,34,58,34,69,114,114,111,114,34,44,34,84,97,98,108,101,34,58,34,74,111,98,79,112,101,114,34,44,34,80,114,111,103,114,97,109,34,58,34,69,112,105,99,111,114,46,82,69,83,84,65,112,105,46,100,108,108,34,44,34,77,101,116,104,111,100,34,58,34,84,104,114,111,119,85,112,100,97,116,101,69,120,116,69,120,99,101,112,116,105,111,110,34,44,34,67,111,108,117,109,110,78,117,109,98,101,114,34,58,49,55,44,34,76,105,110,101,78,117,109,98,101,114,34,58,52,48,125,93,125

我想看到的是一个 JSON 对象,因为这就是这个字节流对应的。

最佳答案

我需要另一个等待来读取错误:

/**
* EPICOR invoke ODATA Post command
* @param {string} serviceBO - Busines Object that provides service
* @param {string} args - the ODATA command and any arguments
* @param {json} payload - and data to send in POST
* @return {string} The next job number
* @public
*/
const post = (async (serviceBO, args, payload) => {
let reply = {}
const url = SERVERURL + serviceBO
const options = window.STORE.getState().identity.options
try {
reply = await o(url, options).post(args, payload).query()
} catch (error) {
console.log(`Error Code: ${error.status}`)
console.log(`Error URL: ${error.url}`)
const json = await error.json()
const body = JSON.stringify(json, ' ', 4)
console.log(`Error body: ${body}`)
return error
}
return reply
})

关于javascript - fetch 返回 ReadableStream 作为错误主体。我如何获取该流的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com