Skip to content

Access error object when delegating a mutation through prisma-binding #45

Closed
@RishitKedia

Description

@RishitKedia

Hey! 👋

So, the Prisma API now returns error codes for errors, like when violating the unique constraint. But, when delegating a mutation through prisma-binding, the error code is part of a string error.message which is not machine-readable. I've also found error.originalError, but same story. How do I access the error code? 😛

Here's some more context:

try {
    const user = await context.prisma.mutation.createUser({ data });
} catch (error) {
    console.log(error);
}

If there is a violation of the unique constraint for email, this is what is logged:

Error: A unique constraint would be violated on User. Details: Field name = email: {"response":
{"data":null,"errors":[{"locations":[{"line":2,"column":3}],"path":
["createUser"],"code":3010,"message":"A unique constraint would be violated on User. Details: 
Field name =email","requestId":"api:api:cjdal8pwd0ep30180fc4r3tfk"}],"status":200},"request":
{"query":"mutation ($_data: UserCreateInput!) {\n createUser(data: $_data) {\n id\n name\n email\n 
}\n}\n","variables":{"_data":{"name":"name","email":"email"}}}}

This string can also be accessed through error.message and error.originalError.message. The error code 3010 is in there, but how do we access it? There’s no error object to access it from.

Here's an image with the properties of the caught error:

error-properties

Also, as per @marktani's suggestion, I tried to not catch the error, and console.log(user). We get the same string.

Thanks! 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions