Skip to content

Commit

Permalink
Responding with 405: draft implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Jan 31, 2025
1 parent e1bb762 commit cea730f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/result-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ export const defaultResultHandler = new ResultHandler({
if (error) {
const httpError = ensureHttpError(error);
logServerError(httpError, logger, request, input);
return void response.status(httpError.statusCode).json({
status: "error",
error: { message: getPublicErrorMessage(httpError) },
});
return void response
.status(httpError.statusCode)
.setHeaders(new Map(Object.entries(httpError.headers || {})))
.json({
status: "error",
error: { message: getPublicErrorMessage(httpError) },
});
}
response
.status(defaultStatusCodes.positive)
Expand Down
30 changes: 26 additions & 4 deletions src/server-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,32 @@ export const createParserFailureHandler =
export const createNotFoundHandler =
({ errorHandler, getLogger }: HandlerCreatorParams): RequestHandler =>
async (request, response) => {
const error = createHttpError(
404,
`Can not ${request.method} ${request.path}`,
);
const supportedMethods = request.app.router.stack

Check warning on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (20.9.0)

flatMap() is about 1.3x slower than R.chain()

Check failure on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (20.9.0)

Unhandled error

TypeError: Cannot read properties of undefined (reading 'router') ❯ src/server-helpers.ts:50:42 ❯ tests/unit/server-helpers.spec.ts:116:7 ❯ node_modules/@vitest/runner/dist/index.js:174:14 ❯ node_modules/@vitest/runner/dist/index.js:561:28 ❯ node_modules/@vitest/runner/dist/index.js:61:24 ❯ runWithTimeout node_modules/@vitest/runner/dist/index.js:41:12 ❯ runTest node_modules/@vitest/runner/dist/index.js:1140:17 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 This error originated in "tests/unit/server-helpers.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "tests/unit/server-helpers.spec.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unhandled error

TypeError: Cannot read properties of undefined (reading 'router') ❯ src/server-helpers.ts:50:42 ❯ tests/unit/server-helpers.spec.ts:116:7 ❯ node_modules/@vitest/runner/dist/index.js:174:14 ❯ node_modules/@vitest/runner/dist/index.js:561:28 ❯ node_modules/@vitest/runner/dist/index.js:61:24 ❯ runWithTimeout node_modules/@vitest/runner/dist/index.js:41:12 ❯ runTest node_modules/@vitest/runner/dist/index.js:1140:17 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 This error originated in "tests/unit/server-helpers.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "tests/unit/server-helpers.spec.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check warning on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

flatMap() is about 1.3x slower than R.chain()

Check warning on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (22.0.0)

flatMap() is about 1.3x slower than R.chain()

Check failure on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (22.0.0)

Unhandled error

TypeError: Cannot read properties of undefined (reading 'router') ❯ src/server-helpers.ts:50:42 ❯ tests/unit/server-helpers.spec.ts:116:7 ❯ node_modules/@vitest/runner/dist/index.js:174:14 ❯ node_modules/@vitest/runner/dist/index.js:561:28 ❯ node_modules/@vitest/runner/dist/index.js:61:24 ❯ runWithTimeout node_modules/@vitest/runner/dist/index.js:41:12 ❯ runTest node_modules/@vitest/runner/dist/index.js:1140:17 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 This error originated in "tests/unit/server-helpers.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "tests/unit/server-helpers.spec.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check warning on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

flatMap() is about 1.3x slower than R.chain()

Check failure on line 50 in src/server-helpers.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Unhandled error

TypeError: Cannot read properties of undefined (reading 'router') ❯ src/server-helpers.ts:50:42 ❯ tests/unit/server-helpers.spec.ts:116:7 ❯ node_modules/@vitest/runner/dist/index.js:174:14 ❯ node_modules/@vitest/runner/dist/index.js:561:28 ❯ node_modules/@vitest/runner/dist/index.js:61:24 ❯ runWithTimeout node_modules/@vitest/runner/dist/index.js:41:12 ❯ runTest node_modules/@vitest/runner/dist/index.js:1140:17 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 ❯ runSuite node_modules/@vitest/runner/dist/index.js:1294:15 This error originated in "tests/unit/server-helpers.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "tests/unit/server-helpers.spec.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
.map(({ route, ...rest }) =>
route && "matchers" in rest && Array.isArray(rest.matchers)
? { route, matchers: rest.matchers }
: undefined,
)
.filter((entry) =>
entry?.matchers?.some(
(fn) => typeof fn === "function" && fn(request.path),
),
)
.flatMap((entry) =>
entry?.route &&
"methods" in entry.route &&
typeof entry.route.methods === "object" &&
entry.route.methods !== null
? Object.keys(entry.route.methods).map((method) =>
method.toUpperCase(),
)
: [],
);
const error = supportedMethods.length
? createHttpError(405, `${request.method} is not allowed`, {
headers: { Allowed: supportedMethods.join(", ") },
})
: createHttpError(404, `Can not ${request.method} ${request.path}`);
const logger = getLogger(request);
try {
errorHandler.execute({
Expand Down
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export const createServer = async (config: ServerConfig, routing: Routing) => {
initRouting({ app, routing, getLogger, config, parsers });
app.use(parserFailureHandler, notFoundHandler);

logger.info("stack", app.router.stack);

const created: Array<http.Server | https.Server> = [];
const makeStarter =
(server: (typeof created)[number], subject: HttpConfig["listen"]) => () =>
Expand Down

0 comments on commit cea730f

Please sign in to comment.