Skip to content

Errors from Zod validator does not trigger the error interceptor #3150

@ElSalvo96

Description

@ElSalvo96

Description

Open API configuration

export default defineConfig({
	input: {
		path: 'https://my.api/swagger',
		fetch: swaggerFetch
	},
	output: { path: './backend-api/generated', format: 'prettier' },
	plugins: [
		{
			name: '@hey-api/client-next',
			throwOnError: true
		},
		{
			name: 'zod',
			comments: true
		},
		{
			name: '@hey-api/sdk',
			validator: {
				request: false,
				response: true
			}
		}
	]
});

The file client.gen.ts is generated with this slice of code:

      if (parseAs === 'json') {
        if (opts.responseValidator) {
          await opts.responseValidator(data);
        }

        if (opts.responseTransformer) {
          data = await opts.responseTransformer(data);
        }
      }

Any errors that come from the response validator does not trigger the interceptor logic below

    const textError = await response.text();
    let jsonError: unknown;

    try {
      jsonError = JSON.parse(textError);
    } catch {
      // noop
    }

    const error = jsonError ?? textError;
    let finalError = error;

    for (const fn of interceptors.error.fns) {
      if (fn) {
        finalError = (await fn(error, response, opts)) as string;
      }
    }

While throwing the error, it would also be nice to have the data to be able to log it properly.
Right now if you use the interceptor to monitor the errors, this will be skipped.
If you want to log every error, you need to remove the interceptor; otherwise, you will end up logging the same error 2 times.

Expected behavior

If responseValidator throws, the error should pass through interceptors.error with access to:

• the original response
• the parsed data
• opts

This would make response validation errors observable and consistent with other client errors.

Reproducible example or configuration

No response

OpenAPI specification (optional)

No response

System information (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Broken or incorrect behavior.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions