Skip to content

Commit 2c0c62f

Browse files
committed
Support unsuccessful request responses
When a TSP request response is received with an unsuccessful status code and without a json object model, the parsing of the body as json was throwing an error and the status code was unavailable to the caller. Create the TspClientResponse with the plain text of the response, and make the model optional. Make the plain text available to the caller. Do not throw an error when the json object is absent. Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
1 parent 0b60632 commit 2c0c62f

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/protocol/rest-client.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { TspClientResponse } from './tsp-client-response';
33

44
/**
55
* Rest client helper to make request.
6-
* Errors are thrown when the request response is not 200.
6+
* The request response status code indicates if the request is successful.
7+
* The json object in the response may be undefined when an error occurs.
78
*/
89
export class RestClient {
910
private static async performRequest<T>(verb: string, url: string, body?: any): Promise<TspClientResponse<T>> {
@@ -14,10 +15,9 @@ export class RestClient {
1415
'Content-Type': 'application/json'
1516
},
1617
method: verb,
17-
body: jsonBody
18-
});
19-
const json = await response.json() as T;
20-
return new TspClientResponse(json, response.status, response.statusText);
18+
body: jsonBody});
19+
const text = await response.text();
20+
return new TspClientResponse(text, response.status, response.statusText);
2121
}
2222

2323
/**

src/protocol/tsp-client-response.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
/**
22
* Trace Server Protocol response.
3-
* The response includes the response model from the server, the status code of the HTTP response and the message attached to this response.
3+
* The response includes the response model from the server if available,
4+
* the status code and message of the HTTP response, and the plain text attached to this response.
45
*/
56
export class TspClientResponse<T> {
6-
private readonly responseModel: T;
7+
private readonly responseModel: T | undefined;
78
private readonly statusCode: number;
89
private readonly statusMessage: string;
10+
private readonly text: string;
911

1012
/**
1113
* Constructor
12-
* @param responseModel Model of type T from the server
14+
* @param text Plain text of the response from the server
1315
* @param statusCode Status code from the HTTP response
1416
* @param statusMessage Status message from the HTTP response
1517
*/
16-
constructor(responseModel: T, statusCode: number, statusMessage: string) {
17-
this.responseModel = responseModel;
18+
constructor(text: string, statusCode: number, statusMessage: string) {
19+
this.text = text;
1820
this.statusCode = statusCode;
1921
this.statusMessage = statusMessage;
22+
try {
23+
this.responseModel = JSON.parse(text) as T;
24+
} catch (error) {
25+
}
2026
}
2127

2228
/**
23-
* Get the model from the server
29+
* Get the model from the server, or undefined
2430
*/
25-
public getModel(): T {
31+
public getModel(): T | undefined {
2632
return this.responseModel;
2733
}
2834

@@ -34,12 +40,19 @@ export class TspClientResponse<T> {
3440
}
3541

3642
/**
37-
* Get the HTTP status code
43+
* Get the HTTP status message
3844
*/
3945
public getStatusMessage(): string {
4046
return this.statusMessage;
4147
}
4248

49+
/**
50+
* Get the plain text of the response from the server
51+
*/
52+
public getText(): string {
53+
return this.text;
54+
}
55+
4356
/**
4457
* Check if the status code is 200
4558
*/

0 commit comments

Comments
 (0)