Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: do not log errors about invalid XML when serializing XHR objects #10483

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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