Skip to content

Commit 6ac4506

Browse files
committed
Support ErrorResponse for error cases in all specified messages
Return the ErrorResponse message in the getStatusMessage() of TspClientResponse. Contributes to fix issue: eclipse-cdt-cloud/trace-server-protocol#122 Signed-off-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
1 parent dc7ccd1 commit 6ac4506

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Model of an error response
3+
*/
4+
export interface ErrorResponse {
5+
/**
6+
* Trace's unique identifier
7+
*/
8+
message: string;
9+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetch, { Headers } from 'node-fetch';
22
import { Deserialized, Normalizer } from './serialization';
33
import { TspClientResponse } from './tsp-client-response';
44
import JSONBigConfig = require('json-bigint');
5+
import { ErrorResponse } from '../models/error-response';
56

67
const JSONBig = JSONBigConfig({
78
useNativeBigInt: true,
@@ -125,6 +126,12 @@ export class RestClient {
125126
if (response.headers?.get('Content-Type') === 'application/json') {
126127
try {
127128
const parsed = this.jsonParse(response.text);
129+
130+
// Check if the server sent an error
131+
if (response.status >= 400) {
132+
const message = (parsed as ErrorResponse).message || 'Unknown error';
133+
return new TspClientResponse(response.text, response.status, message);
134+
}
128135
try {
129136
const responseModel = normalizer ? normalizer(parsed) : parsed;
130137
return new TspClientResponse(response.text, response.status, response.statusText, responseModel);

0 commit comments

Comments
 (0)