From 5dca00d772219490a341fae7bca803bd5a70fa50 Mon Sep 17 00:00:00 2001 From: pk Date: Thu, 5 Oct 2023 12:28:51 +0200 Subject: [PATCH] Send event if request errors --- fetch-mixin.js | 14 ++++++++------ package.json | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/fetch-mixin.js b/fetch-mixin.js index f56c039..7ebf9db 100644 --- a/fetch-mixin.js +++ b/fetch-mixin.js @@ -40,15 +40,17 @@ export const fetchMixin = function(superClass) { document.dispatchEvent(new CustomEvent('before-request', { detail: reqOptions, bubbles: true, composed: true })); - const result = await fetch(url, reqOptions).then(response => response.json()); - + const resp = await fetch(url, reqOptions); this.__abortControllers.delete(`${method}:${url}`); - - if (result.statusCode === 500) { - console.error(result); + if (resp.status === 500) { + console.error(resp); } - return result; + if (resp.status !== 200) { + document.dispatchEvent(new CustomEvent('request-error', { detail: resp, bubbles: true, composed: true })); + } + + return await resp.json(); } catch (err) { if (err.name === 'AbortError') { return { statusCode: -1, error: err }; diff --git a/package.json b/package.json index ae5b213..c97481d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tp/helpers", - "version": "2.1.0", + "version": "2.2.0", "description": "", "main": "closest.js", "scripts": {