From c0dadf2aa22ad952ce78761fb1a1ff2743976cd1 Mon Sep 17 00:00:00 2001 From: Karol-Stelmaczonek <118974926+Karol-Stelmaczonek@users.noreply.github.com> Date: Thu, 16 Mar 2023 09:51:39 +0100 Subject: [PATCH 1/2] feat: add fallback for new BE error response (#166) * feat: add fallback for new BE error response * feat: light refactor of function * feat: light refactor of function * feat: fix response check * feat: fix code problem * feat: change test * fix: add check for string for code and message * fix: wider type checks * fix: test message * fix: test message * fix: add only message version of error * fix: remove non-existing error code 0 --- src/core/request.js | 16 +++++++++++----- test/core/request/test.js | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/core/request.js b/src/core/request.js index d1d95c5a..578865a3 100644 --- a/src/core/request.js +++ b/src/core/request.js @@ -58,10 +58,16 @@ const logger = loggerFactory('core/request'); */ const createError = (response, fallbackMessage, httpCode, httpSent) => { let err; - if (response && response.errorCode) { - err = new Error(`${response.errorCode} : ${response.errorMsg || response.errorMessage || response.error}`); - } else { - err = new Error(fallbackMessage); + if (response) { + const code = response.errorCode || response.code; + const message = response.errorMsg || response.errorMessage || response.error || response.message; + if (code && message) { + err = new Error(`${code} : ${message}`); + } else if (message) { + err = new Error(`${message}`); + } else { + err = new Error(fallbackMessage); + } } err.response = response; err.sent = httpSent; @@ -301,7 +307,7 @@ export default function request(options) { sent: xhr.readyState > 0, type: 'error', textStatus: textStatus, - message: errorThrown || __('An error occurred!') + message: errorThrown || xhr.statusText || __('An error occurred!') }; const enhancedResponse = Object.assign({}, responseExtras, response); diff --git a/test/core/request/test.js b/test/core/request/test.js index 989bb0a6..9ccdf8ab 100644 --- a/test/core/request/test.js +++ b/test/core/request/test.js @@ -397,7 +397,7 @@ define(['jquery', 'lodash', 'core/request', 'core/tokenHandler', 'core/jwt/jwtTo title: 'disconnected', url: '//offline', reject: true, - err: new Error('0 : timeout'), + err: new Error('timeout'), reuseToken: true } ]) @@ -522,7 +522,7 @@ define(['jquery', 'lodash', 'core/request', 'core/tokenHandler', 'core/jwt/jwtTo code: 0, sent: false, source: 'network', - message: '0 : timeout' + message: 'timeout' }, '//202': { code: 202, From 67100a49c160196ae4b37d37fd864d9da292ddee Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Thu, 16 Mar 2023 12:02:26 +0100 Subject: [PATCH 2/2] chore: bump version --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 151c7452..af34e057 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@oat-sa/tao-core-sdk", - "version": "1.22.0", + "version": "1.23.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b45f64f6..75738970 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oat-sa/tao-core-sdk", - "version": "1.22.0", + "version": "1.23.0", "displayName": "TAO Core SDK", "description": "Core libraries of TAO", "homepage": "https://github.com/oat-sa/tao-core-sdk-fe#readme",