Skip to content

Commit

Permalink
Core: do not log errors about invalid XML when serializing XHR objects (
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Sep 27, 2023
1 parent e4c1349 commit 4f7fcc8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
24 changes: 15 additions & 9 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ export function fetcherFactory(timeout = 3000, {request, done} = {}) {

function toXHR({status, statusText = '', headers, url}, responseText) {
let xml = 0;
function getXML(onError) {
if (xml === 0) {
try {
xml = new DOMParser().parseFromString(responseText, headers?.get(CTYPE)?.split(';')?.[0])
} catch (e) {
xml = null;
onError && onError(e)
}
}
return xml;
}
return {
readyState: XMLHttpRequest.DONE,
status,
Expand All @@ -98,17 +109,12 @@ function toXHR({status, statusText = '', headers, url}, responseText) {
responseType: '',
responseURL: url,
get responseXML() {
if (xml === 0) {
try {
xml = new DOMParser().parseFromString(responseText, headers?.get(CTYPE)?.split(';')?.[0])
} catch (e) {
xml = null;
logError(e);
}
}
return xml;
return getXML(logError);
},
getResponseHeader: (header) => headers?.has(header) ? headers.get(header) : null,
toJSON() {
return Object.assign({responseXML: getXML()}, this)
}
}
}

Expand Down
22 changes: 17 additions & 5 deletions test/spec/unit/core/ajax_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {dep, attachCallbacks, fetcherFactory, toFetchRequest} from '../../../../src/ajax.js';
import {attachCallbacks, dep, fetcherFactory, toFetchRequest} from '../../../../src/ajax.js';
import {config} from 'src/config.js';
import {server} from '../../../mocks/xhr.js';
import {sandbox} from 'sinon';
import * as utils from 'src/utils.js';
import {logError} from 'src/utils.js';

const EXAMPLE_URL = 'https://www.example.com';

Expand Down Expand Up @@ -312,13 +313,24 @@ describe('attachCallbacks', () => {
const cbType = success ? 'success' : 'error';

describe(`for ${t}`, () => {
let response, body;
let sandbox, response, body;
beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.spy(utils, 'logError');
({response, body} = makeResponse());
});

afterEach(() => {
sandbox.restore();
})

function checkXHR(xhr) {
sinon.assert.match(xhr, {
utils.logError.resetHistory();
const serialized = JSON.parse(JSON.stringify(xhr))
// serialization of `responseXML` should not generate console messages
sinon.assert.notCalled(utils.logError);

sinon.assert.match(serialized, {
readyState: XMLHttpRequest.DONE,
status: response.status,
statusText: response.statusText,
Expand All @@ -330,7 +342,7 @@ describe('attachCallbacks', () => {
if (xml) {
expect(xhr.responseXML.querySelectorAll('*').length > 0).to.be.true;
} else {
expect(xhr.responseXML).to.not.exist;
expect(serialized.responseXML).to.not.exist;
}
Array.from(response.headers.entries()).forEach(([name, value]) => {
expect(xhr.getResponseHeader(name)).to.eql(value);
Expand Down

0 comments on commit 4f7fcc8

Please sign in to comment.