Skip to content

Commit

Permalink
chore: assertRoute error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Jan 9, 2025
1 parent d063355 commit 4b2862e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type RouterContext, addRoute, createRouter, findRoute } from "rou3";
import { methodSymbol, optionsToSymbols, orderSymbol, pathSymbol, universalSymbol } from "./const";
import { methodSymbol, nameSymbol, optionsToSymbols, orderSymbol, pathSymbol, universalSymbol } from "./const";
import { pipe } from "./pipe";
import type {
AnyFn,
Expand Down Expand Up @@ -109,13 +109,21 @@ function ordered(middlewares: EnhancedMiddleware[]) {

function assertRoute(middleware: EnhancedMiddleware) {
const path = getUniversalProp(middleware, pathSymbol);
const method = getUniversalProp(middleware, methodSymbol);
// TODO better error message names, using `name` when possible
if (!path) {
throw new Error("decorate: at least one route is missing a `path`");
const name = getUniversalProp(middleware, nameSymbol);
throw new TypeError(assertRouteErrorMessage("path", name));
}
const method = getUniversalProp(middleware, methodSymbol);
if (!method) {
throw new Error("decorate: at least one route is missing a `method`");
const name = getUniversalProp(middleware, nameSymbol);
throw new TypeError(assertRouteErrorMessage("method", name));
}
return { path, method };
}

function assertRouteErrorMessage(key: string, name: string | undefined) {
if (name) {
return `Route ${name} is defined without a "${key}". See https://universal-middleware.dev/helpers/enhance for details.`;
}
return `Unnamed route is defined without a "${key}". See https://universal-middleware.dev/helpers/enhance for details.`;
}

0 comments on commit 4b2862e

Please sign in to comment.