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

error overlay: show cause if available #6052

Merged
merged 3 commits into from
Jan 31, 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
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));
Copy link
Contributor Author

@mayank99 mayank99 Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally wanted to check if this is instanceof Error and display it, but for some reason err.cause is missing info and just returns an empty object when i do this:

try {
  console.log(something.that.doesnt.exist);
} catch (err) {
  throw new Error('oh no!', { cause: err });
}

cause.style.display = 'block';
}
}

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