-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow stacking error handlers (#3085)
- Loading branch information
Showing
7 changed files
with
61 additions
and
6 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
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,36 @@ | ||
import type { Nitro } from "nitropack/types"; | ||
import { virtual } from "./virtual"; | ||
|
||
export function errorHandler(nitro: Nitro) { | ||
return virtual( | ||
{ | ||
"#nitro-internal-virtual/error-handler": () => { | ||
const errorHandlers = Array.isArray(nitro.options.errorHandler) | ||
? nitro.options.errorHandler | ||
: [nitro.options.errorHandler]; | ||
|
||
return /* js */ ` | ||
${errorHandlers.map((h, i) => `import errorHandler$${i} from "${h}";`).join("\n")} | ||
const errorHandlers = [${errorHandlers.map((_, i) => `errorHandler$${i}`).join(", ")}]; | ||
export default async function(error, event) { | ||
for (const handler of errorHandlers) { | ||
try { | ||
await handler(error, event); | ||
if (event.handled) { | ||
return; // Response handled | ||
} | ||
} catch(error) { | ||
// Handler itself thrown, log and continue | ||
console.error(error); | ||
} | ||
} | ||
// H3 will handle fallback | ||
} | ||
`; | ||
}, | ||
}, | ||
nitro.vfs | ||
); | ||
} |
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,5 @@ | ||
export default defineNitroErrorHandler((error, event) => { | ||
if (event.path.includes("?custom_error_handler")) { | ||
return send(event, "custom_error_handler"); | ||
} | ||
}); |
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