jlalmes / trpc-openapi

OpenAPI support for tRPC 🧩
https://www.npmjs.com/package/trpc-openapi
MIT License
2.23k stars 152 forks source link

errorFormatter returns invalid openapi schema #205

Open matannahmani opened 1 year ago

matannahmani commented 1 year ago

I've been using errorFormatter to include Zod errors and more custom errors, but the returned error type in openapi schema doesn't synchronize trpc error formatter:

    errorFormatter({ shape, error }) {
        return {
            ...shape,
            data: {
                ...shape.data,
                errCode: error.cause instanceof CustomTRPCError ? error.cause.errCode : null,
                zodError:
                    error.code === 'BAD_REQUEST' &&
                        error.cause instanceof ZodError
                        ? error.cause.flatten()
                        : null,
            },
        };
    }

return open-api schema:

{
  "message": "string",
  "code": "string",
  "issues": [
    {
      "message": "string"
    }
  ]
}

real response:

{
  "message": "Input validation failed",
  "code": "BAD_REQUEST",
  "issues": [
    {
      "validation": "uuid",
      "code": "invalid_string",
      "message": "Invalid uuid",
      "path": [
        "keyRingKeyId"
      ]
    }
  ]
}
matannahmani commented 1 year ago

I would suggest adding an option to override errorResponseObject with your own error object

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

aagwali commented 1 year ago

Hello,

Any news on this topic ?

I'm having a use case where I try to add some data to a 409 error (the conflicting object). I cannot find a way to return anything attached to the error.

cgyrock commented 11 months ago

image unfortunately,they hard code the condition,while zodError occur,force set message as ‘Input validation failed’

image we can just change “error.caus.name” to break the condition.

I had tried it works!

@aagwali

ulyr commented 8 months ago

is there even a reason to limit the shape of the response body? I think that's the purpose of the trpc errorFormatter that we're all using, I think trpc-openapi should just take the error object as it is and pass it on...