Skip to content

Commit 77f1a36

Browse files
committed
Merge branch 'main' into auth
2 parents cb02574 + 535f57d commit 77f1a36

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/Server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Server<A> {
5656
}
5757
catch (e) {
5858
if (e instanceof Request.BadUrlError) {
59-
await this.errors._get(ServerErrorRegistry.ErrorCodes.BAD_URL)._send(res);
59+
await this.errors._get(ServerErrorRegistry.ErrorCodes.BAD_URL, null)._send(res, this);
6060
return;
6161
}
6262
if (e instanceof Request.SocketClosedError)
@@ -78,10 +78,10 @@ class Server<A> {
7878
}
7979
catch (e) {
8080
if (e instanceof RouteRegistry.NoRouteError)
81-
response = this.errors._get(ServerErrorRegistry.ErrorCodes.NO_ROUTE);
81+
response = this.errors._get(ServerErrorRegistry.ErrorCodes.NO_ROUTE, apiRequest);
8282
else {
8383
console.error("Internal Server Error:", e);
84-
response = this.errors._get(ServerErrorRegistry.ErrorCodes.INTERNAL);
84+
response = this.errors._get(ServerErrorRegistry.ErrorCodes.INTERNAL, apiRequest);
8585
}
8686
}
8787
await response._send(res, apiRequest);

src/ServerErrorRegistry.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {Response} from "./response/Response.js";
2+
import {Request} from "./Request.js";
23
import {TextResponse} from "./response/TextResponse.js";
34

45
/**
56
* A registry for server errors.
67
*/
78
class ServerErrorRegistry<A> {
8-
private readonly responses: Record<ServerErrorRegistry.ErrorCodes, Response<A>>;
9+
private readonly responses: Record<ServerErrorRegistry.ErrorCodes, Response<A> | ((req?: Request<A>) => Response<A>)>;
910

1011
/**
1112
* Create a new server error registry initialised with default responses.
@@ -28,13 +29,15 @@ class ServerErrorRegistry<A> {
2829
* @param code The server error code.
2930
* @param response The response to send.
3031
*/
31-
public register(code: ServerErrorRegistry.ErrorCodes, response: Response<A>) {
32+
public register(code: ServerErrorRegistry.ErrorCodes, response: Response<A> | ((req?: Request<A>) => Response<A>)) {
3233
this.responses[code] = response;
3334
}
3435

3536
/** @internal */
36-
public _get(code: ServerErrorRegistry.ErrorCodes): Response<A> {
37-
return this.responses[code];
37+
public _get(code: ServerErrorRegistry.ErrorCodes, req: Request<A> | null): Response<A> {
38+
const r = this.responses[code];
39+
if (typeof r === "function") return r(req ?? void 0);
40+
return r;
3841
}
3942
}
4043

src/response/JsonResponse.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class JsonResponse<T, A> extends TextResponse<A> {
99
*/
1010
public constructor(json: T, statusCode = 200, headers?: HeadersInit) {
1111
super(JsonResponse.serialise(json), statusCode, headers);
12+
this.headers.set("content-type", "application/json");
1213
}
1314

1415
protected static serialise(value: any): string {

0 commit comments

Comments
 (0)