Skip to content

Commit a1fdd4c

Browse files
committed
Support ErrorResponse for error cases in all specified messages
Return optional ErrorResponse in new method getErrorResponse() of TspClientResponse. Contributes to fix issue: eclipse-cdt-cloud/trace-server-protocol#122 Signed-off-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
1 parent 46afa19 commit a1fdd4c

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Model of an error response
3+
*/
4+
export interface ErrorResponse {
5+
/**
6+
* The short, human-readable description of the error.
7+
*/
8+
title: string;
9+
10+
/**
11+
* The optional human-readable explanation of the error with details helping the client to correct the error.
12+
*/
13+
details?: string;
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ErrorResponse } from "./error-response";
2+
import { Experiment } from "./experiment";
3+
4+
/**
5+
* Model of an error response
6+
*/
7+
export interface TraceErrorResponse extends ErrorResponse {
8+
/**
9+
* The experiment that this error corresponds to.
10+
*/
11+
experiment: Experiment;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ErrorResponse } from "./error-response";
2+
import { Trace } from "./trace";
3+
4+
/**
5+
* Model of an error response
6+
*/
7+
export interface TraceErrorResponse extends ErrorResponse {
8+
/**
9+
* The trace that this error corresponds to.
10+
*/
11+
trace: Trace;
12+
}

tsp-typescript-client/src/protocol/rest-client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class RestClient {
103103
url: string,
104104
body?: any,
105105
normalizer?: Normalizer<T>,
106-
): Promise<TspClientResponse<Deserialized<T>>> {
106+
): Promise<TspClientResponse<Deserialized<T|undefined>>> {
107107
let response: HttpResponse;
108108
try {
109109
response = await this.httpRequest({
@@ -125,6 +125,11 @@ export class RestClient {
125125
if (response.headers?.get('Content-Type') === 'application/json') {
126126
try {
127127
const parsed = this.jsonParse(response.text);
128+
129+
// Check if the server sent an error
130+
if (response.status >= 400) {
131+
return new TspClientResponse(response.text, response.status, response.statusText, undefined, parsed);
132+
}
128133
try {
129134
const responseModel = normalizer ? normalizer(parsed) : parsed;
130135
return new TspClientResponse(response.text, response.status, response.statusText, responseModel);

tsp-typescript-client/src/protocol/tsp-client-response.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ErrorResponse } from "../models/error-response";
2+
13
/**
24
* Trace Server Protocol response.
35
* The response includes the response model from the server if available,
@@ -17,6 +19,7 @@ export class TspClientResponse<T> {
1719
private readonly statusCode: number,
1820
private readonly statusMessage: string,
1921
private readonly responseModel?: T,
22+
private readonly errorResponse?: ErrorResponse
2023
) {}
2124

2225
/**
@@ -47,6 +50,13 @@ export class TspClientResponse<T> {
4750
return this.text;
4851
}
4952

53+
/**
54+
* Returns the error response from the server in an error case
55+
*/
56+
public getErrorResponse(): ErrorResponse | undefined {
57+
return this.errorResponse;
58+
}
59+
5060
/**
5161
* Check if the status code is 200
5262
*/

tsp-typescript-client/src/protocol/tsp-client.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,15 @@ describe('HttpTspClient Deserialization', () => {
574574
const output = response.getModel()!;
575575
expect(output).toBeDefined();
576576
});
577+
578+
it('postTrace-failure', async () => {
579+
httpRequestMock.mockResolvedValueOnce({ status: 404, statusText: 'Not Found', text: '{"title": "No trace at /some/path"}', headers: new Headers({ 'Content-Type': 'application/json' }) });
580+
const response = await client.openTrace(new Query({}));
581+
582+
expect(response.getStatusCode()).toEqual(404);
583+
expect(response.getStatusMessage()).toEqual('Not Found');
584+
expect(response.getText()).toEqual('{"title": "No trace at /some/path"}');
585+
expect(response.getModel()).toBeUndefined();
586+
expect(response.getErrorResponse()).toEqual({ title: 'No trace at /some/path' });
587+
});
577588
});

0 commit comments

Comments
 (0)