Search code examples
javascriptrestodatafetch

fetch returns ReadableStream as body of error. How do I get the contents of that stream?


I am using the ODATA NPM package to fetch ODATA via REST calls. (I assume this is basically just a wrapper for fetch).

Well, I am using await/try/catch to grab the errors, and I would like to get the body of the error. There must be a better way than what I am doing:

If I do this:

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
})

Then the text.value that is displayed is a Uint8Array byte stream:

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

What I want to see is a JSON object, since that is what this byte stream corresponds to.


Solution

  • I needed another await to read the error:

    /**
     * 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
    })