OpenAPI Generator生成的typescript-fetch客户端丢失请求错误信息
使用OpenAPI Generator的typescript-fetch生成器时的ResponseError问题
我在用OpenAPI Generator的typescript-fetch生成器根据Swagger定义生成代码时,碰到了异常情况:
生成的请求发送代码如下:
// 生成的请求发送代码 protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> { const { url, init } = await this.createFetchParams(context, initOverrides); const response = await this.fetchApi(url, init); if (response && (response.status >= 200 && response.status < 300)) { return response; } throw new ResponseError(response, 'Response returned an error code'); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 错误实例化 }
再看ResponseError的定义:
export class ResponseError extends Error { override name: "ResponseError" = "ResponseError"; constructor(public response: Response, msg?: string) { super(msg); } }
明明response对象已经作为构造函数参数传入,但最终好像被丢弃了。我拿到的错误信息完全没有参考价值,比如:
Error: {"response":{},"name":"ResponseError"}
通过浏览器网络面板能看到POST请求包含所有必要信息,响应对象里应该有完整内容,但我就是拿不到这个响应对象。我有几个疑问:
- 为什么会出现这种情况?
- 这是预期行为吗?有没有生成器选项可以修复?
- 有没有其他缓解方法?(我试过用
..RawAPI,但它用的还是同样的错误处理机制)
另外,也有可能是我打印API错误的方式不对,如果是这样,请指出来。
内容的提问来源于stack exchange,提问作者Lorah Attkins
相关产品推荐
相关产品推荐

