Skip to content

Commit 8feb1f8

Browse files
committed
fix(typescript-fetch): ResponseError prototype chain
1 parent 5b89b47 commit 8feb1f8

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

  • modules/openapi-generator/src/main/resources/typescript-fetch
  • samples/client/petstore/typescript-fetch/tests/default/test

modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,38 @@ export class ResponseError extends Error {
251251
override name: "ResponseError" = "ResponseError";
252252
constructor(public response: Response, msg?: string) {
253253
super(msg);
254+
255+
// restore prototype chain
256+
const actualProto = new.target.prototype;
257+
if (Object.setPrototypeOf) {
258+
Object.setPrototypeOf(this, actualProto);
259+
}
254260
}
255261
}
256262

257263
export class FetchError extends Error {
258264
override name: "FetchError" = "FetchError";
259265
constructor(public cause: Error, msg?: string) {
260266
super(msg);
267+
268+
// restore prototype chain
269+
const actualProto = new.target.prototype;
270+
if (Object.setPrototypeOf) {
271+
Object.setPrototypeOf(this, actualProto);
272+
}
261273
}
262274
}
263275

264276
export class RequiredError extends Error {
265277
override name: "RequiredError" = "RequiredError";
266278
constructor(public field: string, msg?: string) {
267279
super(msg);
280+
281+
// restore prototype chain
282+
const actualProto = new.target.prototype;
283+
if (Object.setPrototypeOf) {
284+
Object.setPrototypeOf(this, actualProto);
285+
}
268286
}
269287
}
270288

samples/client/petstore/typescript-fetch/tests/default/test/PetApi.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import { PetApi, Pet, PetStatusEnum, Category } from '@swagger/typescript-fetch-petstore';
2+
import { PetApi, Pet, PetStatusEnum, Category, ResponseError } from '@swagger/typescript-fetch-petstore';
33
import { config } from '../configuration';
44

55
describe('PetApi', () => {
@@ -49,6 +49,13 @@ describe('PetApi', () => {
4949
});
5050
});
5151

52+
it('instanceof ResponseError not Error', () => {
53+
const response = new Response();
54+
const err = new ResponseError(response, 'test error message');
55+
expect(err instanceof ResponseError).to.be.true;
56+
expect(err instanceof Error).to.be.true;
57+
});
58+
5259
});
5360
}
5461

0 commit comments

Comments
 (0)