Skip to content

Commit

Permalink
[BUGFIX] Fix $.Deferred.fail() polyfill
Browse files Browse the repository at this point in the history
This patch adjusts the polyfill for $.Deferred.fail() to call the
jQuery-based error handling with the correct arguments. Since we don't
XHR in this context, we have to fake that object as good as possible.

Resolves: #90191
Releases: master
Change-Id: Icd9bc82c0fe81ae6326f1804802bd244afc32eb0
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63021
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
  • Loading branch information
andreaskienast committed Jan 24, 2020
1 parent e44e5d5 commit 7f79795
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Expand Up @@ -11,8 +11,10 @@
* The TYPO3 project - inspiring people to share!
*/

import {ResponseError} from '../Ajax/ResponseError';

/**
* Introduces a polyfill to support jQuery callbacks in native promises. This approach has been adopted from
* Introduces a polyfill to support jQuery callbacks in native promises.
*/
/*! Based on https://www.promisejs.org/polyfills/promise-done-7.0.4.js */
export default class JQueryNativePromises {
Expand All @@ -25,15 +27,31 @@ export default class JQueryNativePromises {

if (typeof Promise.prototype.fail !== 'function') {
Promise.prototype.fail = function (onRejected: Function): Promise<any> {
const self = arguments.length ? this.catch.apply(this, arguments) : Promise.prototype.catch;
self.catch(function (err: string) {
setTimeout(function () {
throw err
}, 0)
this.catch(async (err: ResponseError): Promise<void> => {
const response = err.response;
onRejected(await JQueryNativePromises.createFakeXhrObject(response), 'error', response.statusText);
});

return self;
return this;
};
}
}

private static async createFakeXhrObject(response: Response): Promise<any> {
const xhr: { [key: string ]: any } = {};
xhr.readyState = 4;
xhr.responseText = await response.text();
xhr.responseURL = response.url;
xhr.status = response.status;
xhr.statusText = response.statusText;

if (response.headers.has('Content-Type') && response.headers.get('Content-Type').includes('application/json')) {
xhr.responseType = 'json';
xhr.contentJSON = await response.json();
} else {
xhr.responseType = 'text';
}

return xhr;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f79795

Please sign in to comment.