Skip to content

Instantly share code, notes, and snippets.

@barmatz
Last active September 22, 2015 10:05
Show Gist options
  • Select an option

  • Save barmatz/86ab2687c00cb62dc5d5 to your computer and use it in GitHub Desktop.

Select an option

Save barmatz/86ab2687c00cb62dc5d5 to your computer and use it in GitHub Desktop.
JavaScript error parser
function errorParser(err) {
if (err) {
if (typeof err === 'function') {
err = err();
}
if (typeof err === 'object') {
if ('responseText' in err) {
err = err.responseText;
try {
err = JSON.parse(err);
} catch (e) {
} finally {
return this.parseError(err);
}
} else if ('message' in err) {
return err.message;
} else if ('error' in err) {
return this.parseError(err.error);
}
}
if (typeof err !== 'string') {
err = err.toString();
}
try {
err = Ember.$(new DOMParser().parseFromString(err, 'text/xml')).find('parsererror > div').html();
} catch (e) {}
return err;
} else {
return 'Unknown error';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment