-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle unhandledrejections in the dev server (#9424)
* Handle unhandledrejections in the dev server * Adding changeset * Update .changeset/curvy-lobsters-crash.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Use AsyncLocalStorage * Return errorWithMetadata * Send the error to the browser --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
8c9fe00
commit e1a5a2d
Showing
5 changed files
with
82 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Prevents dev server from crashing on unhandled rejections, and adds a helpful error message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { ModuleLoader } from '../core/module-loader/index.js' | ||
import type { AstroConfig } from '../@types/astro.js'; | ||
import type DevPipeline from './devPipeline.js'; | ||
|
||
import { collectErrorMetadata } from '../core/errors/dev/index.js'; | ||
import { createSafeError } from '../core/errors/index.js'; | ||
import { formatErrorMessage } from '../core/messages.js'; | ||
import { eventError, telemetry } from '../events/index.js'; | ||
|
||
export function recordServerError(loader: ModuleLoader, config: AstroConfig, pipeline: DevPipeline, _err: unknown) { | ||
const err = createSafeError(_err); | ||
|
||
// This could be a runtime error from Vite's SSR module, so try to fix it here | ||
try { | ||
loader.fixStacktrace(err); | ||
} catch {} | ||
|
||
// This is our last line of defense regarding errors where we still might have some information about the request | ||
// Our error should already be complete, but let's try to add a bit more through some guesswork | ||
const errorWithMetadata = collectErrorMetadata(err, config.root); | ||
|
||
telemetry.record(eventError({ cmd: 'dev', err: errorWithMetadata, isFatal: false })); | ||
|
||
pipeline.logger.error( | ||
null, | ||
formatErrorMessage(errorWithMetadata, pipeline.logger.level() === 'debug') | ||
); | ||
|
||
return { | ||
error: err, | ||
errorWithMetadata | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters