Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tsp-typescript-client/src/models/error-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Model of an error response
*/
export interface ErrorResponse {
/**
* The short, human-readable description of the error.
*/
title: string;

/**
* The optional human-readable explanation of the error with details helping the client to correct the error.
*/
details?: string;
}
12 changes: 12 additions & 0 deletions tsp-typescript-client/src/models/experiment-error-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ErrorResponse } from "./error-response";
import { Experiment } from "./experiment";

/**
* Model of an error response
*/
export interface TraceErrorResponse extends ErrorResponse {
/**
* The experiment that this error corresponds to.
*/
experiment: Experiment;
}
12 changes: 12 additions & 0 deletions tsp-typescript-client/src/models/trace-error-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ErrorResponse } from "./error-response";
import { Trace } from "./trace";

/**
* Model of an error response
*/
export interface TraceErrorResponse extends ErrorResponse {
/**
* The trace that this error corresponds to.
*/
trace: Trace;
}
7 changes: 6 additions & 1 deletion tsp-typescript-client/src/protocol/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class RestClient {
url: string,
body?: any,
normalizer?: Normalizer<T>,
): Promise<TspClientResponse<Deserialized<T>>> {
): Promise<TspClientResponse<Deserialized<T|undefined>>> {
let response: HttpResponse;
try {
response = await this.httpRequest({
Expand All @@ -125,6 +125,11 @@ export class RestClient {
if (response.headers?.get('Content-Type') === 'application/json') {
try {
const parsed = this.jsonParse(response.text);

// Check if the server sent an error
if (response.status >= 400) {
return new TspClientResponse(response.text, response.status, response.statusText, undefined, parsed);
}
try {
const responseModel = normalizer ? normalizer(parsed) : parsed;
return new TspClientResponse(response.text, response.status, response.statusText, responseModel);
Expand Down
10 changes: 10 additions & 0 deletions tsp-typescript-client/src/protocol/tsp-client-response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ErrorResponse } from "../models/error-response";

/**
* Trace Server Protocol response.
* The response includes the response model from the server if available,
Expand All @@ -17,6 +19,7 @@ export class TspClientResponse<T> {
private readonly statusCode: number,
private readonly statusMessage: string,
private readonly responseModel?: T,
private readonly errorResponse?: ErrorResponse
) {}

/**
Expand Down Expand Up @@ -47,6 +50,13 @@ export class TspClientResponse<T> {
return this.text;
}

/**
* Returns the error response from the server in an error case
*/
public getErrorResponse(): ErrorResponse | undefined {
return this.errorResponse;
}

/**
* Check if the status code is 200
*/
Expand Down
11 changes: 11 additions & 0 deletions tsp-typescript-client/src/protocol/tsp-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,15 @@ describe('HttpTspClient Deserialization', () => {
const output = response.getModel()!;
expect(output).toBeDefined();
});

it('postTrace-failure', async () => {
httpRequestMock.mockResolvedValueOnce({ status: 404, statusText: 'Not Found', text: '{"title": "No trace at /some/path"}', headers: new Headers({ 'Content-Type': 'application/json' }) });
const response = await client.openTrace(new Query({}));

expect(response.getStatusCode()).toEqual(404);
expect(response.getStatusMessage()).toEqual('Not Found');
expect(response.getText()).toEqual('{"title": "No trace at /some/path"}');
expect(response.getModel()).toBeUndefined();
expect(response.getErrorResponse()).toEqual({ title: 'No trace at /some/path' });
});
});