Skip to content

Commit

Permalink
error overlay: show cause if available (#6052)
Browse files Browse the repository at this point in the history
* show `cause` in error overlay

* add extra check for string

* add changeset
  • Loading branch information
mayank99 authored Jan 31, 2023
1 parent 43ec7f3 commit 9793f19
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-panthers-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Error overlay will now show the error's `cause` if available.
2 changes: 2 additions & 0 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface AstroErrorPayload {
line?: number;
column?: number;
};
cause?: unknown;
};
}

Expand Down Expand Up @@ -174,6 +175,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
},
plugin,
stack: err.stack,
cause: err.cause
},
};

Expand Down
29 changes: 26 additions & 3 deletions packages/astro/src/core/errors/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ const style = /* css */ `
#message-hints,
#stack,
#code {
#code,
#cause {
border-radius: var(--roundiness);
background-color: var(--box-background);
}
Expand Down Expand Up @@ -471,7 +472,8 @@ const style = /* css */ `
color: var(--error-text);
}
#stack h2 {
#stack h2,
#cause h2 {
color: var(--title-text);
font-family: var(--font-normal);
font-size: 22px;
Expand All @@ -480,14 +482,19 @@ const style = /* css */ `
border-bottom: 1px solid var(--border);
}
#stack-content {
#stack-content,
#cause-content {
font-size: 14px;
white-space: pre;
line-height: 21px;
overflow: auto;
padding: 24px;
color: var(--stack-text);
}
#cause {
display: none;
}
`;

const overlayTemplate = /* html */ `
Expand Down Expand Up @@ -552,6 +559,11 @@ ${style.trim()}
<h2>Stack Trace</h2>
<div id="stack-content"></div>
</section>
<section id="cause">
<h2>Cause</h2>
<div id="cause-content"></div>
</section>
</div>
</div>
`;
Expand Down Expand Up @@ -593,6 +605,17 @@ class ErrorOverlay extends HTMLElement {
this.text('#title', err.title);
this.text('#message-content', err.message, true);

const cause = this.root.querySelector<HTMLElement>('#cause');
if (cause && err.cause) {
if (typeof err.cause === 'string') {
this.text('#cause-content', err.cause);
cause.style.display = 'block';
} else {
this.text('#cause-content', JSON.stringify(err.cause, null, 2));
cause.style.display = 'block';
}
}

const hint = this.root.querySelector<HTMLElement>('#hint');
if (hint && err.hint) {
this.text('#hint-content', err.hint, true);
Expand Down

0 comments on commit 9793f19

Please sign in to comment.