(response, {allowUnauthorized = false} = {})
| 38 | } |
| 39 | |
| 40 | export async function handleErrors(response, {allowUnauthorized = false} = {}) { |
| 41 | /* |
| 42 | * Handle the error of the response |
| 43 | * @param response [Response]: response from fetch |
| 44 | * @return: response |
| 45 | */ |
| 46 | if (response.status === 401) { |
| 47 | await emptyCache(); |
| 48 | if (allowUnauthorized) { |
| 49 | return response; |
| 50 | } |
| 51 | if (!["/agreement", "/login", "/register", "/reset"].includes(window.location.pathname)) { |
| 52 | window.location.assign("/login"); |
| 53 | } |
| 54 | throw new Response('', { |
| 55 | status: response.status, |
| 56 | statusText: response.statusText, |
| 57 | }); |
| 58 | } |
| 59 | if (!response.ok) { |
| 60 | const content = await response.json().catch(() => ({})); |
| 61 | throw new Response('', { |
| 62 | status: response.status, |
| 63 | statusText: content.error ?? response.statusText, |
| 64 | }); |
| 65 | } |
| 66 | return response; |
| 67 | } |
| 68 | |
| 69 | export function blobToBase64(blob) { |
| 70 | return new Promise((resolve, reject) => { |
no test coverage detected