Skip to content

Commit ebdc8e6

Browse files
committed
create throwable response class
1 parent 3493ba7 commit ebdc8e6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/response/ThrowableResponse.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {Response} from "./Response.js";
2+
3+
/**
4+
* An (error) response that is thrown. Will be caught by the server and sent to the client.
5+
*/
6+
export class ThrowableResponse<T extends Response> extends Error {
7+
public override name = ThrowableResponse.name;
8+
9+
/**
10+
* The response to send to the client.
11+
*/
12+
protected readonly response: T;
13+
14+
/**
15+
* An optional error to emit on the server’s error event.
16+
*/
17+
protected readonly error: Error | null;
18+
19+
/**
20+
* Create a new throwable response.
21+
* @param response The response to send to the client.
22+
* @param [error] An optional error to emit on the server’s error event.
23+
*/
24+
public constructor(response: T, error?: Error) {
25+
super();
26+
this.response = response;
27+
this.error = error ?? null;
28+
}
29+
30+
public getResponse(): T {
31+
return this.response;
32+
}
33+
34+
public getError(): Error | null {
35+
return this.error;
36+
}
37+
}

0 commit comments

Comments
 (0)