-
-
Notifications
You must be signed in to change notification settings - Fork 128
Closed
Description
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!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels