Ajax promise reject should return Error instead of message

This commit is contained in:
Bartek Fabiszewski 2019-11-29 17:24:27 +01:00
parent 2086d20192
commit f2e887c23d
2 changed files with 11 additions and 11 deletions

View File

@ -45,7 +45,7 @@ export default class uAjax {
* @param {Object|HTMLFormElement} [data] Optional request parameters: key/value pairs or form element * @param {Object|HTMLFormElement} [data] Optional request parameters: key/value pairs or form element
* @param {Object} [options] Optional options * @param {Object} [options] Optional options
* @param {string} [options.method='GET'] Optional query method, default 'GET' * @param {string} [options.method='GET'] Optional query method, default 'GET'
* @return {Promise<Object, string>} * @return {Promise<Object, Error>}
*/ */
static ajax(url, data, options) { static ajax(url, data, options) {
const params = []; const params = [];
@ -78,10 +78,10 @@ export default class uAjax {
message = `HTTP error ${xhr.status}`; message = `HTTP error ${xhr.status}`;
} }
if (error && reject && typeof reject === 'function') { if (error && reject && typeof reject === 'function') {
reject(message); reject(new Error(message));
} }
}; };
let body = null; let body;
if (data instanceof HTMLFormElement) { if (data instanceof HTMLFormElement) {
if (method === 'POST') { if (method === 'POST') {
body = new FormData(data); body = new FormData(data);

View File

@ -109,8 +109,8 @@ describe('Ajax tests', () => {
// then // then
uAjax.get(url) uAjax.get(url)
.then(() => done.fail('resolve callback called')) .then(() => done.fail('resolve callback called'))
.catch((message) => { .catch((e) => {
expect(message).toBe(errorResponse.message); expect(e.message).toBe(errorResponse.message);
done(); done();
}); });
}); });
@ -122,8 +122,8 @@ describe('Ajax tests', () => {
// then // then
uAjax.get(url) uAjax.get(url)
.then(() => done.fail('resolve callback called')) .then(() => done.fail('resolve callback called'))
.catch((message) => { .catch((e) => {
expect(message).toBe(''); expect(e.message).toBe('');
done(); done();
}); });
}); });
@ -135,8 +135,8 @@ describe('Ajax tests', () => {
// then // then
uAjax.get(url) uAjax.get(url)
.then(() => done.fail('resolve callback called')) .then(() => done.fail('resolve callback called'))
.catch((message) => { .catch((e) => {
expect(message).toBe(`HTTP error ${status}`); expect(e.message).toBe(`HTTP error ${status}`);
done(); done();
}); });
}); });
@ -148,8 +148,8 @@ describe('Ajax tests', () => {
// then // then
uAjax.get(url) uAjax.get(url)
.then(() => done.fail('resolve callback called')) .then(() => done.fail('resolve callback called'))
.catch((message) => { .catch((e) => {
expect(message).toContain('JSON'); expect(e.message).toContain('JSON');
done(); done();
}); });
}); });