Skip to content

Commit

Permalink
Add cause to turbopack-node error (#70456)
Browse files Browse the repository at this point in the history
Occurs for example in
```
Failed to load chunk server/chunks/08b5e__pnpm_560cbe._.js
	....
Caused by SyntaxError: Identifier 'require' has already been declared
	....
```
  • Loading branch information
mischnic authored Sep 25, 2024
1 parent 039f580 commit 6c47eaa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions turbopack/crates/turbopack-node/js/src/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type StructuredError = {
name: string;
message: string;
stack: StackFrame[];
cause: StructuredError | undefined
};

export function structuredError(e: Error): StructuredError {
Expand All @@ -16,6 +17,7 @@ export function structuredError(e: Error): StructuredError {
name: e.name,
message: e.message,
stack: typeof e.stack === "string" ? parseStackTrace(e.stack!) : [],
cause: e.cause ? structuredError(getProperError(e.cause)) : undefined,
};
}

Expand Down
15 changes: 15 additions & 0 deletions turbopack/crates/turbopack-node/src/source_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ pub struct StructuredError {
pub message: String,
#[turbo_tasks(trace_ignore)]
stack: Vec<StackFrame<'static>>,
cause: Option<Box<StructuredError>>,
}

impl StructuredError {
Expand Down Expand Up @@ -305,6 +306,20 @@ impl StructuredError {
formatting_mode,
)?;
}

if let Some(cause) = &self.cause {
message.write_str("\nCaused by: ")?;
message.write_str(
&Box::pin(cause.print(
assets_for_source_mapping,
root,
project_dir,
formatting_mode,
))
.await?,
)?;
}

Ok(message)
}
}
Expand Down

0 comments on commit 6c47eaa

Please sign in to comment.