1
1
import type { IResult } from '@unioc/shared'
2
2
import type { IRestfulConnect } from '@unioc/web'
3
+ import { HttpException } from '@nestjs/common'
3
4
4
5
export class EndingHandler {
6
+ /**
7
+ * ### Send a response to the client `if the user not send a response`.
8
+ *
9
+ * 🫘 It will check currently the response is already sent, if not, it will send a response.
10
+ *
11
+ * @param ctx - The web context of the request.
12
+ * @param result - The result of the request.
13
+ */
5
14
async sendConnectEndingResponse ( ctx : IRestfulConnect . WebContext , result : IResult ) : Promise < void > {
6
15
if ( ctx . response . writableEnded || ctx . response . writableFinished )
7
16
return
8
17
9
18
if ( 'send' in ctx . response && typeof ctx . response . send === 'function' && 'status' in ctx . response && typeof ctx . response . status === 'function' ) {
10
19
if ( result . type === 'result' ) {
11
- ctx . response . send ( result . value )
20
+ return ctx . response . send ( result . value )
12
21
}
13
- else {
14
- ctx . response . status ( 500 ) . send ( {
15
- statusCode : 500 ,
16
- message : 'Internal Server Error' ,
17
- error : await this . _toReadableError ( result . error ) ,
18
- } )
22
+
23
+ if ( result . error instanceof HttpException ) {
24
+ const response = result . error . getResponse ( )
25
+ return ctx . response . status ( result . error . getStatus ( ) ) . send (
26
+ typeof response === 'string'
27
+ ? {
28
+ statusCode : result . error . getStatus ( ) ,
29
+ message : response ,
30
+ }
31
+ : response ,
32
+ )
19
33
}
34
+
35
+ ctx . response . status ( 500 ) . send ( {
36
+ statusCode : 500 ,
37
+ message : 'Internal Server Error' ,
38
+ error : await this . _toReadableError ( result . error ) ,
39
+ } )
20
40
return
21
41
}
22
42
23
43
if ( result . type === 'result' ) {
24
44
ctx . response . end ( await this . _toSendableString ( result . value ) )
45
+ return
25
46
}
26
- else {
27
- ctx . response . statusCode = 500
47
+
48
+ if ( result . error instanceof HttpException ) {
49
+ ctx . response . statusCode = result . error . getStatus ( )
50
+ const response = result . error . getResponse ( )
28
51
ctx . response . end (
29
- await this . _toSendableString ( {
30
- statusCode : 500 ,
31
- message : 'Internal Server Error' ,
32
- error : await this . _toReadableError ( result . error ) ,
33
- } ) ,
52
+ typeof response === 'string'
53
+ ? {
54
+ statusCode : result . error . getStatus ( ) ,
55
+ message : response ,
56
+ }
57
+ : response ,
34
58
)
59
+ return
35
60
}
61
+
62
+ ctx . response . end (
63
+ await this . _toSendableString ( {
64
+ statusCode : 500 ,
65
+ message : 'Internal Server Error' ,
66
+ error : await this . _toReadableError ( result . error ) ,
67
+ } ) ,
68
+ )
36
69
}
37
70
38
71
private async _stringifyAsync ( data : unknown ) : Promise < string > {
@@ -43,17 +76,11 @@ export class EndingHandler {
43
76
if ( typeof data === 'string' )
44
77
return data
45
78
if ( typeof data === 'object' && data !== null ) {
46
- return await this . _stringifyAsync ( data )
47
- . catch ( async error => this . _stringifyAsync ( {
48
- statusCode : 500 ,
49
- message : 'Internal Server Error' ,
50
- error : await this . _toReadableError ( error ) ,
51
- } ) )
52
- . catch ( async error => this . _stringifyAsync ( {
53
- statusCode : 500 ,
54
- message : 'Internal Server Error' ,
55
- error : await this . _toReadableError ( error ) ,
56
- } ) )
79
+ return await this . _stringifyAsync ( data ) . catch ( async error => this . _stringifyAsync ( {
80
+ statusCode : 500 ,
81
+ message : 'Internal Server Error' ,
82
+ error : await this . _toReadableError ( error ) ,
83
+ } ) )
57
84
}
58
85
return String ( data )
59
86
}
0 commit comments