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

feat: better errors #3002

Merged
merged 16 commits into from
Jan 15, 2025
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
"semver": "^7.6.3",
"serve-placeholder": "^2.0.2",
"serve-static": "^1.16.2",
"source-map": "^0.7.4",
"std-env": "^3.8.0",
"ufo": "^1.5.4",
"uncrypto": "^0.1.3",
Expand All @@ -164,7 +165,9 @@
"unimport": "^3.14.5",
"unstorage": "^1.14.4",
"untyped": "^1.5.2",
"unwasm": "^0.3.9"
"unwasm": "^0.3.9",
"youch": "4.1.0-beta.4",
"youch-core": "^0.3.1"
},
"devDependencies": {
"@azure/functions": "^3.5.1",
Expand Down
86 changes: 86 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/core/config/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { runtimeDir } from "nitropack/runtime/meta";
import type { NitroConfig } from "nitropack/types";
import { join, resolve } from "pathe";
import { resolve } from "pathe";
import { isDebug, isTest } from "std-env";

export const NitroDefaults: NitroConfig = {
Expand Down Expand Up @@ -58,7 +58,7 @@ export const NitroDefaults: NitroConfig = {
baseURL: process.env.NITRO_APP_BASE_URL || "/",
handlers: [],
devHandlers: [],
errorHandler: join(runtimeDir, "internal/error/handler"),
errorHandler: undefined,
routeRules: {},
prerender: {
autoSubfolderIndex: true,
Expand Down
2 changes: 2 additions & 0 deletions src/core/config/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { resolveRouteRulesOptions } from "./resolvers/route-rules";
import { resolveRuntimeConfigOptions } from "./resolvers/runtime-config";
import { resolveStorageOptions } from "./resolvers/storage";
import { resolveURLOptions } from "./resolvers/url";
import { resolveErrorOptions } from "./resolvers/error";

const configResolvers = [
resolveCompatibilityOptions,
Expand All @@ -41,6 +42,7 @@ const configResolvers = [
resolveURLOptions,
resolveAssetsOptions,
resolveStorageOptions,
resolveErrorOptions,
] as const;

export async function loadOptions(
Expand Down
12 changes: 12 additions & 0 deletions src/core/config/resolvers/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { runtimeDir } from "nitropack/runtime/meta";
import type { NitroOptions } from "nitropack/types";
import { join } from "pathe";

export async function resolveErrorOptions(options: NitroOptions) {
if (!options.errorHandler) {
options.errorHandler = join(
runtimeDir,
`internal/error/${options.dev ? "dev" : "prod"}`
);
}
}
55 changes: 0 additions & 55 deletions src/core/dev-server/error.ts

This file was deleted.

20 changes: 9 additions & 11 deletions src/core/dev-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import { debounce } from "perfect-debounce";
import { servePlaceholder } from "serve-placeholder";
import serveStatic from "serve-static";
import { joinURL } from "ufo";
import defaultErrorHandler from "./error";
import consola from "consola";
import defaultErrorHandler, {
loadStackTrace,
} from "../../runtime/internal/error/dev";
import { createVFSHandler } from "./vfs";

function initWorker(filename: string): Promise<NitroWorker> | undefined {
Expand All @@ -49,14 +52,8 @@ function initWorker(filename: string): Promise<NitroWorker> | undefined {
)
);
});
worker.once("error", (error) => {
const newError = new Error(`[worker init] ${filename} failed`, {
cause: error,
});
if (Error.captureStackTrace) {
Error.captureStackTrace(newError, initWorker);
}
reject(newError);
worker.once("error", async (error) => {
reject(error);
});
const addressListener = (event: any) => {
if (!event || !event?.address) {
Expand Down Expand Up @@ -149,8 +146,9 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
.then(() => {
lastError = undefined;
})
.catch((error) => {
console.error("[worker reload]", error);
.catch(async (error) => {
await loadStackTrace(error).catch(() => {});
consola.error(error);
lastError = error;
})
.finally(() => {
Expand Down
Loading
Loading