JSFiddle - React, Tailwind, and code Playground
by brigand
JavaScript
const error = {
"id": "CpfeA1Q<-2WHSMo5",
"status": 500,
"type": "fetch_self_votes_failed",
"message": "Request to \"http://localhost:80/api/get-all-user-actions\" failed with 403. Response text: {\"error\":\"Not authorized.\"}",
"etc": {
"req_method": "GET",
"req_url": "http://localhost:80/api/get-all-user-actions",
"req_body": null,
"classic_res_body": {
"error": "Not authorized."
},
"from": [{
"id": "2WHSMo5",
"type": "backend_4xx",
"message": "Request to \"http://localhost:80/api/get-all-user-actions\" failed with 403. Response text: {\"error\":\"Not authorized.\"}",
"status": 403,
"etc": {
"req_method": "GET",
"req_url": "http://localhost:80/api/get-all-user-actions",
"req_body": null,
"classic_res_body": {
"error": "Not authorized."
}
}
}]
}
};
const iterError = (err, fn) => {
if (err.etc && err.etc.from) {
iterError(err.etc.from, fn);
} else if (Array.isArray(err)) {
err.forEach(x => iterError(x, fn));
} else {
fn(err);
}
};
const getClassicRes = (err) => {
let res = null;
iterError(err, (e) => {
if (e && e.etc && e.etc.classic_res_body) {
res = e.etc.classic_res_body;
}
})
return res;
};
const x = getClassicRes(error);
if (x && x.error === 'Not authorized.') {
console.log('do logout here')
}