Skip to content

Commit

Permalink
chore(#296): appendError() first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
tmedwards committed Apr 19, 2024
1 parent 8efb8ab commit 5522ab5
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/util/appenderror.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,45 @@
/*
Appends an error view to the given DOM element or fragment and logs a message to the console.
*/
function appendError(output, message, source) { // eslint-disable-line no-unused-vars
function appendError(output, error, source) { // eslint-disable-line no-unused-vars
let message;

if (error instanceof Error) {
message = error.message;

// // NOTE: I'm unsure if I trust Babel to do optional chaining sanely, so….
// for (
// let err = error?.cause?.origin;
// err;
// err = err?.cause?.origin
// ) {
// message += `: ${err.message}`;
// }
if (error.cause && error.cause.origin) {
for (
let err = error.cause.origin;
/* empty */;
err = err.cause.origin
) {
message += `: ${err.message}`;

if (!err.cause || !err.cause.origin) {
break;
}
}
}

// TODO: Handle `hint` properties somehow.
}
else {
message = String(error);
}

message = `${L10n.get('errorViewTitle')}: ${message || 'unknown error'}`;

const $wrapper = jQuery(document.createElement('div'));
const $toggle = jQuery(document.createElement('button'));
const $source = jQuery(document.createElement('pre'));
const mesg = `${L10n.get('errorViewTitle')}: ${message || 'unknown error'}`;

$toggle
.addClass('error-toggle')
Expand All @@ -37,7 +71,7 @@ function appendError(output, message, source) { // eslint-disable-line no-unused
.appendTo($wrapper);
jQuery(document.createElement('span'))
.addClass('error')
.text(mesg)
.text(message)
.appendTo($wrapper);
jQuery(document.createElement('code'))
.text(source)
Expand All @@ -53,7 +87,7 @@ function appendError(output, message, source) { // eslint-disable-line no-unused
.addClass('error-view')
.appendTo(output);

console.warn(`${mesg}\n\t${source.replace(/\n/g, '\n\t')}`);
console.warn(`${message}\n\t${source.replace(/\n/g, '\n\t')}`);

return false;
}
Expand Down

0 comments on commit 5522ab5

Please sign in to comment.