Skip to content

Commit

Permalink
fix: use UnrecognizedError
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Dec 12, 2024
1 parent ea9666b commit 0bd7dc5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
18 changes: 8 additions & 10 deletions packages/homeserver/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logger } from "@bogeychan/elysia-logger";
import { Elysia } from "elysia";

import { BadJSONError, MatrixError } from "./errors";
import { BadJSONError, MatrixError, UnrecognizedError } from "./errors";
import federationEndpoints from "./routes/federation";
import { keyV2Endpoints } from "./routes/key/server";
import type { ElysiaRoutes } from "./extractRouteTypings";
Expand All @@ -24,6 +24,12 @@ export const app = new Elysia({
set.status = newError.status;
return newError.toJSON();
}

if (code === "NOT_FOUND") {
const newError = UnrecognizedError.notImplemented("Unrecognized request");
set.status = newError.status;
return newError.toJSON();
}
})
.use(
logger({
Expand All @@ -32,14 +38,6 @@ export const app = new Elysia({
)
.use(keyV2Endpoints)
.use(federationEndpoints)
.use(wellKnownEndpoint)
.onError(async ({ code }) => {
if (code === "NOT_FOUND") {
return {
errcode: "M_UNRECOGNIZED",
error: "Unrecognized request",
};
}
});
.use(wellKnownEndpoint);

export type HomeServerRoutes = ElysiaRoutes<typeof app>;
13 changes: 12 additions & 1 deletion packages/homeserver/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,20 @@ export class LimitExceededError extends MatrixError<"M_LIMIT_EXCEEDED"> {
* This is expected to be returned with a 404 HTTP status code if the endpoint is not implemented or a 405 HTTP status code if the endpoint is implemented, but the incorrect HTTP method is used.
*/
export class UnrecognizedError extends MatrixError<"M_UNRECOGNIZED"> {
public constructor(message: string) {
private constructor(
message: string,
public readonly status: number,
) {
super("M_UNRECOGNIZED", message);
}

public static notImplemented(message: string) {
return new UnrecognizedError(message, 404);
}

public static methodNotAllowed(message: string) {
return new UnrecognizedError(message, 405);
}
}

/** An unknown error has occurred. */
Expand Down

0 comments on commit 0bd7dc5

Please sign in to comment.