ecmascript 6 - How to capture the json text inside fetch's then() -
this how call fetch
fetchopts = {...} return fetch(url, fetchopts) .then(checkstatus)
the url call return json data, in error condition:
{error_message:"encountered issue update" status:"error", status_code:400}
i hold of error_message
field value , pass error
constructor.
inside checkstatus, handles exception way:
function checkstatus(response) { let error = null; if (!response.headers.get('content-type').includes('application/json')) { error = new error(response.statustext); error.response = response; throw error; } if (response.status >= 200 && response.status < 300) { return response; } if (response.ok) { return response; } const jsondata = response.json() ; // <- `jsondata` promise error = new error(/* how extract error_message json data? */); error.response = response; throw error; }
i have checked question not address need fetch: reject promise json error object. accepted answer seems, based on comments, not resolve op's question entirely.
i may misunderstanding problem, error_message
member exposed property of data object promise returns, right? seems this:
response.json() .then(jsondata => { if (jsondata.error_message) { error = new error(jsondata.error_message) } }
Comments
Post a Comment