Skip to content

Commit

Permalink
Fix: Zod errors missing stacktrace (#7380)
Browse files Browse the repository at this point in the history
* wip: figure out stack issue, weird JSON stringify bug on zod issue

* refactor: return data instead of mutating

* refactor: remove need for reassignment

* fix: e.stack only

* chore: changeset
  • Loading branch information
bholmesdev authored Jun 12, 2023
1 parent 0986a44 commit 1c7b635
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-dryers-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix missing stacktraces for Zod errors
35 changes: 23 additions & 12 deletions packages/astro/src/core/errors/dev/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export function collectErrorMetadata(e: any, rootFolder?: URL | undefined): Erro
? (e.errors as SSRError[])
: [e as SSRError];

err.forEach((error) => {
if (error.stack) {
error = collectInfoFromStacktrace(e);
err.forEach((error, idx) => {
if (e.stack) {
const stackInfo = collectInfoFromStacktrace(e);
error.stack = stackInfo.stack;
error.loc = stackInfo.loc;
error.plugin = stackInfo.plugin;
error.pluginCode = stackInfo.pluginCode;
}

// Make sure the file location is absolute, otherwise:
Expand Down Expand Up @@ -141,15 +145,22 @@ See https://docs.astro.build/en/guides/troubleshooting/#document-or-window-is-no
return err.hint;
}

function collectInfoFromStacktrace(error: SSRError): SSRError {
if (!error.stack) return error;
type StackInfo = Pick<SSRError, 'stack' | 'loc' | 'plugin' | 'pluginCode'>;

function collectInfoFromStacktrace(error: SSRError & { stack: string }): StackInfo {
let stackInfo: StackInfo = {
stack: error.stack,
plugin: error.plugin,
pluginCode: error.pluginCode,
loc: error.loc,
};

// normalize error stack line-endings to \n
error.stack = normalizeLF(error.stack);
stackInfo.stack = normalizeLF(error.stack);
const stackText = stripAnsi(error.stack);

// Try to find possible location from stack if we don't have one
if (!error.loc || (!error.loc.column && !error.loc.line)) {
if (!stackInfo.loc || (!stackInfo.loc.column && !stackInfo.loc.line)) {
const possibleFilePath =
error.loc?.file ||
error.pluginCode ||
Expand All @@ -168,7 +179,7 @@ function collectInfoFromStacktrace(error: SSRError): SSRError {
file = fileURLToPath(file);
} catch {}

error.loc = {
stackInfo.loc = {
file,
line: Number.parseInt(line),
column: Number.parseInt(column),
Expand All @@ -177,17 +188,17 @@ function collectInfoFromStacktrace(error: SSRError): SSRError {
}

// Derive plugin from stack (if possible)
if (!error.plugin) {
error.plugin =
if (!stackInfo.plugin) {
stackInfo.plugin =
/withastro\/astro\/packages\/integrations\/([\w-]+)/gim.exec(stackText)?.at(1) ||
/(@astrojs\/[\w-]+)\/(server|client|index)/gim.exec(stackText)?.at(1) ||
undefined;
}

// Normalize stack (remove `/@fs/` urls, etc)
error.stack = cleanErrorStack(error.stack);
stackInfo.stack = cleanErrorStack(error.stack);

return error;
return stackInfo;
}

function cleanErrorStack(stack: string) {
Expand Down

0 comments on commit 1c7b635

Please sign in to comment.