У меня проблемы с этим кодом:
fetch('/example.json')
.then(response => Promise.all([response, response.json()])) // This line throws unhandled exception: SyntaxError: Unexpected token < in JSON at position 0
.then(([response, json]) => {
if (!response.ok) {
throw new Error(JSON.stringify(json));
}
return json;
})
.catch(exception => {
const error = new Map([
[TypeError, ["There was a problem fetching the response."]],
[SyntaxError, ["There was a problem parsing the response."]],
[Error, JSON.parse(exception.message)]
]).get(exception.constructor);
return { error };
})
В большинстве случаев он работает нормально, но не получается, когда ответ сервера 200 OK, но фактическим содержимым является HTML.Я знаю, что это ошибка на сервере, но я просто хочу, чтобы мой клиент справился с этим правильно.То есть он должен перехватить исключение response.json () с окончательным уловом.
Что я делаю не так?