From f2e887c23ddc9e72916e6acf1648d05610b15548 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Fri, 29 Nov 2019 17:24:27 +0100 Subject: [PATCH] Ajax promise reject should return Error instead of message --- js/src/ajax.js | 6 +++--- js/test/ajax.test.js | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/js/src/ajax.js b/js/src/ajax.js index 8c615fe..b711873 100644 --- a/js/src/ajax.js +++ b/js/src/ajax.js @@ -45,7 +45,7 @@ export default class uAjax { * @param {Object|HTMLFormElement} [data] Optional request parameters: key/value pairs or form element * @param {Object} [options] Optional options * @param {string} [options.method='GET'] Optional query method, default 'GET' - * @return {Promise} + * @return {Promise} */ static ajax(url, data, options) { const params = []; @@ -78,10 +78,10 @@ export default class uAjax { message = `HTTP error ${xhr.status}`; } if (error && reject && typeof reject === 'function') { - reject(message); + reject(new Error(message)); } }; - let body = null; + let body; if (data instanceof HTMLFormElement) { if (method === 'POST') { body = new FormData(data); diff --git a/js/test/ajax.test.js b/js/test/ajax.test.js index f3e69bf..adae208 100644 --- a/js/test/ajax.test.js +++ b/js/test/ajax.test.js @@ -109,8 +109,8 @@ describe('Ajax tests', () => { // then uAjax.get(url) .then(() => done.fail('resolve callback called')) - .catch((message) => { - expect(message).toBe(errorResponse.message); + .catch((e) => { + expect(e.message).toBe(errorResponse.message); done(); }); }); @@ -122,8 +122,8 @@ describe('Ajax tests', () => { // then uAjax.get(url) .then(() => done.fail('resolve callback called')) - .catch((message) => { - expect(message).toBe(''); + .catch((e) => { + expect(e.message).toBe(''); done(); }); }); @@ -135,8 +135,8 @@ describe('Ajax tests', () => { // then uAjax.get(url) .then(() => done.fail('resolve callback called')) - .catch((message) => { - expect(message).toBe(`HTTP error ${status}`); + .catch((e) => { + expect(e.message).toBe(`HTTP error ${status}`); done(); }); }); @@ -148,8 +148,8 @@ describe('Ajax tests', () => { // then uAjax.get(url) .then(() => done.fail('resolve callback called')) - .catch((message) => { - expect(message).toContain('JSON'); + .catch((e) => { + expect(e.message).toContain('JSON'); done(); }); });