From b429bb16d7b1bcf64b26f7b3773c08f56b052fe2 Mon Sep 17 00:00:00 2001 From: Harel M Date: Tue, 21 Jan 2025 16:25:03 +0200 Subject: [PATCH] Fix network issue (#944) ## Launch Checklist In case of non 200 response the font and glyphs metadata would return the ajax error object instead of the default value. Fixes #935 - #935 - [x] Briefly describe the changes in this PR. - [x] Link to related issues. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [ ] Write tests for all new functionality. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. --- CHANGELOG.md | 2 ++ src/libs/metadata.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10acbe02..a7d15402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ - _...Add new stuff here..._ ### 🐞 Bug fixes + +- Fix incorrect handing of network error response (#944) - _...Add new stuff here..._ ## 2.1.1 diff --git a/src/libs/metadata.ts b/src/libs/metadata.ts index f724661f..dcc01243 100644 --- a/src/libs/metadata.ts +++ b/src/libs/metadata.ts @@ -5,14 +5,17 @@ function loadJSON(url: string, defaultValue: any, cb: (...args: any[]) => void) mode: 'cors', credentials: "same-origin" }) - .then(function(response) { + .then((response) => { + if (!response.ok) { + throw new Error('Failed to load metadata for ' + url); + } return response.json(); }) - .then(function(body) { + .then((body) => { cb(body) }) - .catch(function() { - console.warn('Can not metadata for ' + url) + .catch(() => { + console.warn('Can not load metadata for ' + url + ', using default value ' + defaultValue); cb(defaultValue) }) }