[REQ] Feature Request Description
Created by: javier-garcia-meteologica
Is your feature request related to a problem? Please describe.
In javascript/typescript throwing anything but an error is considered an antipattern. Only errors should be thrown because:
- Only errors can provide a reliable stacktraces
- Makes error handling easier, since 99.99% of caught "things" will have a common interface
{ message: string }
.
Right now typescript-fetch throws a bare Response
which is undesirable.
https://github.com/OpenAPITools/openapi-generator/blob/60b29e1f8e267e90d4d81f30d3fb3fe69161e59b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L38-L42
Describe the solution you'd like
Typescript-fetch should throw a custom error in which the response is just a property, just like it does with RequiredError. https://github.com/OpenAPITools/openapi-generator/blob/60b29e1f8e267e90d4d81f30d3fb3fe69161e59b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L104-L109
E.g.
const response = await this.fetchApi(url, init);
if (response.status >= 200 && response.status < 300) {
return response;
}
throw new ResponseError(response, 'Response returned an error code');
...
export class ResponseError extends Error {
name: "ResponseError" = "ResponseError";
constructor(public response: Response, msg?: string) {
super(msg);
}
}