Skip to content

Commit a72c143

Browse files
committed
feat: add processError option
1 parent 787750e commit a72c143

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

docs/interfaces/openapi_client.CommonHttpClientOptions.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Options for the common HTTP client.
2121
- [headers](openapi_client.CommonHttpClientOptions.md#headers)
2222
- [preprocessFetchResponse](openapi_client.CommonHttpClientOptions.md#preprocessfetchresponse)
2323
- [preprocessRequest](openapi_client.CommonHttpClientOptions.md#preprocessrequest)
24+
- [processError](openapi_client.CommonHttpClientOptions.md#processerror)
2425
- [shouldRetryOnError](openapi_client.CommonHttpClientOptions.md#shouldretryonerror)
2526

2627
### Methods
@@ -208,6 +209,28 @@ Preprocess the request before sending it.
208209

209210
___
210211

212+
### processError
213+
214+
`Optional` **processError**: (`error`: `Error`) => `Error`
215+
216+
#### Type declaration
217+
218+
▸ (`error`): `Error`
219+
220+
Process the error before throwing it. Can be used to add additional information to the error.
221+
222+
##### Parameters
223+
224+
| Name | Type |
225+
| :------ | :------ |
226+
| `error` | `Error` |
227+
228+
##### Returns
229+
230+
`Error`
231+
232+
___
233+
211234
### shouldRetryOnError
212235

213236
`Optional` **shouldRetryOnError**: (`error`: `Error`, `attemptNumber`: `number`) => `boolean` \| `Promise`\<`boolean`\>

src/schema-to-typescript/common/core/common-http-client.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export interface CommonHttpClientOptions {
7575
* Determine whether to retry on error.
7676
*/
7777
shouldRetryOnError?: (error: Error, attemptNumber: number) => boolean | Promise<boolean>;
78+
/**
79+
* Process the error before throwing it. Can be used to add additional information to the error.
80+
*/
81+
processError?: (error: Error) => Error;
7882
}
7983

8084
/**
@@ -770,9 +774,23 @@ export class CommonHttpClient {
770774
}
771775

772776
/**
773-
* Perform a request.
777+
* Request wrapper. It performs the request and handles errors.
774778
*/
775779
public async request(request: CommonHttpClientRequest): Promise<CommonHttpClientFetchResponse> {
780+
try {
781+
return await this.performRequest(request);
782+
} catch (e) {
783+
if (this.options.processError) {
784+
throw this.options.processError(e instanceof Error ? e : new Error(String(e)));
785+
}
786+
throw e;
787+
}
788+
}
789+
790+
/**
791+
* Perform a request.
792+
*/
793+
protected async performRequest(request: CommonHttpClientRequest): Promise<CommonHttpClientFetchResponse> {
776794
this.logDeprecationWarningIfNecessary(request);
777795
try {
778796
request = await this.preprocessRequest(request);

test/pet-store/store-service.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@ describe('store-service', () => {
3838
fail('Unexpected media type.');
3939
}
4040
});
41-
it('should get and delete the placed order', async () => {
41+
it('should process errors', async () => {
4242
const orderId = 123123123;
43-
await client.store.placeOrder({
44-
order: {
45-
id: orderId,
46-
petId: 123,
47-
quantity: 1,
48-
shipDate: '2021-09-01T00:00:00.000Z',
49-
status: 'placed',
50-
complete: true
51-
}
43+
const processError = (error: Error) => {
44+
error.message = `Processed error: ${error.message}`;
45+
return error;
46+
};
47+
const client = new PetStoreApiClient({
48+
baseUrl: 'https://petstore.swagger.io/v2',
49+
shouldRetryOnError,
50+
processError
5251
});
53-
const orderResponse = await client.store.getOrderById({orderId});
54-
if (checkReponseMediaType(orderResponse, 'application/json')) {
55-
expect(orderResponse.body.petId).toEqual(123);
56-
} else {
57-
throw new Error('Unexpected media type.');
52+
try {
53+
await client.store.getOrderById({orderId});
54+
} catch (error) {
55+
if (!(error instanceof CommonHttpClientError)) {
56+
fail('Unexpected error type.');
57+
}
58+
expect((error as CommonHttpClientError).response?.status).toEqual(404);
59+
expect(error.message).toContain('Processed error:');
5860
}
59-
await client.store.deleteOrder({orderId});
6061
});
6162
it('should fail in case of non-existing orders', async () => {
6263
try {

0 commit comments

Comments
 (0)