fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error response type

jamalsoueidan opened this issue · comments

Hello,

Thanks for creating this cli codegen, I love it.

I faced a small issue with response model for status code 400:

"400":
      $ref: "../../responses/error.yaml"

which consist of object with 2 keys {success, error}

but when using the openapi-codegen it resolve them like this:

export type CreateUserApiError = Fetcher.ErrorWrapper<{
  status: 400;
  payload: Responses.ErrorResponse;
}>;

when I call createUserApi, and it throws an error, the error object is:

const { mutate, error } = useCreateUserApi();
console.log(error);
{name: 'unknown', message: 'Network error', stack: {…}}

I'm missing something?

Shouldn't error contain my ErrorResponse object?

In vscode the error is:
const error: CreateUserApiError | null

But this is not right?

Hey 👋
The generated type looks correct, the Fetcher.ErrorWrapper<> is adding this {status: "unknown"}, you can have a look into the generated {namespace}Fetcher.ts file.

You seams to have a Network error somewhere, I can’t really tell more from here. This is the default behaviour:

// {namespace}Fetcher.ts-71:86
if (!response.ok) {
      let error: ErrorWrapper<TError>;
      try {
        error = await response.json();
      } catch (e) {
        error = {
          status: "unknown" as const,
          payload:
            e instanceof Error
              ? `Unexpected error (${e.message})`
              : "Unexpected error",
        };
      }

      throw error;
      }

So for example, if the error doesn’t send back a json, this will fallback. try to add some console.log over here 😉

I can try to have a look on Tuesday 😉

I can try to have a look on Tuesday 😉

Happy Easter ;)

I just had a quick look, but I don’t really see where is the call in your codebase, you can ping me on https://xata.io/discord (@fabien0102#6706) so you can walk me through the codebase, this will be easier ;)

https://github.com/jamalsoueidan/booking-admin/blob/main/src/App.tsx

it's fresh react app, only this code, first call to login may take few sec to boot up the instance on azure function.

The login gives wrong phone number, to receive error :) the success is correct and works as expected.

Can you add the generated api in the versionning (remove this line https://github.com/jamalsoueidan/booking-admin/blob/main/.gitignore#L26)

It's usually a good idea to version it anyway 😉 So you can have a nice diff from one version to another and anybody jumping into your project doesn't need to bother with the codegen step.

Hi @fabien0102

I appreciate your time. But I switched to http://orval.dev, it's working out of the box.