Skip to content

Commit

Permalink
Better errors when onRequest hasn't been implemented or connection.ro…
Browse files Browse the repository at this point in the history
…om isn't available
  • Loading branch information
threepointone committed May 28, 2024
1 parent dc47922 commit 5c97caf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-pots-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"partyflare": patch
---

Better errors when onRequest hasn't been implemented or connection.room isn't available
16 changes: 13 additions & 3 deletions packages/partyflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export class Party<Env> extends DurableObject<Env> {
this.connectionManager = this.ParentClass.options.hibernate
? new HibernatingConnectionManager(ctx)
: new InMemoryConnectionManager();

// TODO: throw error if any of
// broadcast/getConnection/getConnections/getConnectionTags
// have been overridden
}
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
Expand Down Expand Up @@ -337,10 +341,12 @@ export class Party<Env> extends DurableObject<Env> {
): void | Promise<void> {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onError(connection: Connection, error: unknown): void | Promise<void> {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onRequest(request: Request): Response | Promise<Response> {
// default to 404
return new Response("Not Found", { status: 404 });
return new Response(
`onRequest hasn't been implemented on the Durable Object responding to ${request.url}`,
{ status: 404 }
);
}

#getRoomFromRequest(req: Request): string {
Expand All @@ -356,6 +362,10 @@ export class Party<Env> extends DurableObject<Env> {
return room;
}
#getRoomFromConnection(connection: Connection): string {
return connection.room;
const { room } = connection;
if (!room) {
throw new Error("Room not found in connection");
}
return room;
}
}

0 comments on commit 5c97caf

Please sign in to comment.