Skip to content

Instantly share code, notes, and snippets.

@butchler
Created September 12, 2017 06:46
Show Gist options
  • Save butchler/51391ff9d799caec55e8e5a760aa20b6 to your computer and use it in GitHub Desktop.
Save butchler/51391ff9d799caec55e8e5a760aa20b6 to your computer and use it in GitHub Desktop.
Reproduce Parse Error when sending notices to Honeybadger via POST requests inside JSDOM instance
const jsdom = require('jsdom');
const jsdomInstance = new jsdom.JSDOM('', {
url: 'http://example.com/',
runScripts: 'outside-only',
});
jsdomInstance.window.eval(`
// Make POST request to Honeybager Notice API.
const request = new XMLHttpRequest();
// Fails with 'Parse Error' from Node's internal TLS implementation:
request.open('POST', 'https://api.honeybadger.io/v1/notices/js', true);
// Fails with 'Cross origin http://example.com forbidden' from JSDOM's XMLHttpRequest
// implementation:
//request.open('POST', 'https://api.honeybadger.io/v1/notices', true);
request.setRequestHeader('X-API-Key', /*'Your API key here'*/);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Accept', 'text/json, application/json');
request.addEventListener('error', () => {
console.error('Honeybadger error payload failed to send.');
});
request.addEventListener('load', () => {
if (request.status !== 201) {
console.error('Expected 201 status from Honeybadger, but got ' + request.status);
} else {
console.log('Success');
}
});
let backtrace;
try {
throw new Error('');
} catch (error) {
backtrace = error.stack;
}
const payload = {
error: {
class: 'MyError',
message: 'This is only a test.',
backtrace,
generator: 'throw',
},
request: {
cgi_data: {
HTTP_USER_AGENT: navigator.userAgent,
},
context: {},
url: document.URL,
},
server: {
environment_name: 'test',
project_root: window.location.protocol + '//' + window.location.host,
},
notifier: {
language: 'javascript',
name: 'honeybadger-in-jsdom',
url: 'https://github.com/butchler',
version: '1.0.0',
},
};
request.send(JSON.stringify(payload));
console.log('Sending request...');
`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment