OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] middleware.onError does not handle errors from statuses like 401

Open
#17,979 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Pre-question: Regarding semantic errors, should we classify a response status such as 401 as an error? If yes, there seems to be a bug.

I observed that the middleware.onError does not handle errors from response status codes like 401. Upon reviewing the TypeScript code, I noticed how the response is validated

    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');
    }

but middleware.onError is inside fetchApi function, so it can't be handled properly.

Steps to reproduce

Setup any onError middleware and get response.status == 401 from network call

Suggest a fix

If we consider response.status == 401 as an error, we should verify statuses within the fetchApi function. This way, in the catch block, the response can be sent to middleware.onError.

If the suggestion will be approved, I can work on this issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript fetch implementation's fetchApi and request methods shown in the report, then reproduce a network response with status 401 using an onError middleware. Confirm whether non-2xx responses should be treated as errors, and consider the issue done when the middleware receives the resulting ResponseError and the relevant behavior is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.