Skip to content

OpenAPI Contract First Error Question #709

@zacharyhansen

Description

@zacharyhansen

Hi again!

I am not sure how to properly add the zod errors to the return of contract first implementation. For example I have a router defined like so

import { oc } from '@orpc/contract';
import { datasetContract } from './dataset/dataset.contract';

export const configurationContract = oc.router({
  dataset: oc.prefix('/dataset').router(datasetContract),
});

Then I implemented by contract using Nestjs

@Controller()
@Injectable()
export class DatasetController {
  constructor(private readonly datasetService: DatasetService) {}

  @Implement(configurationContract.dataset)
  dataset(
    @ClerkContext() clerk_claims: ClerkClaims,
    @AuthorizationContext() authorization: string,
  ) {
    return {
      byId: implement(configurationContract.dataset.byId).handler(
        async ({ input }) => {
          const dataset = await this.datasetService.dataset({
            datasetId: input.datasetId,
            organization_id: clerk_claims.organization_id,
            role_name: clerk_claims.o.rol,
          });
          console.log({ dataset: dataset.dataview?.dataview_column });
          return dataset;
        },
      ),

However I am getting a generic error like

{
  "defined": false,
  "code": "INTERNAL_SERVER_ERROR",
  "status": 500,
  "message": "Output validation failed"
}

Which makes it difficult to debug. I can see how to define errors and throw errors in the contract implementation but I am not sure how to attach the zodErrors as data (ideally defining this once for my whole router) similar to this code in the docs

clientInterceptors: [
    onError((error) => {
      if (
        error instanceof ORPCError
        && error.code === 'BAD_REQUEST'
        && error.cause instanceof ValidationError
      ) {
        // If you only use Zod you can safely cast to ZodIssue[]
        const zodError = new ZodError(error.cause.issues as ZodIssue[])

        throw new ORPCError('INPUT_VALIDATION_FAILED', {
          status: 422,
          data: zodError.flatten(),
          cause: error.cause,
        })
      }
    }),
  ],

Thanks for the help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions