question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

originalError not available in formatError

See original GitHub issue

Hello,

I am using graphql-constraint-directive with apollo-server-express But in formatError, when I call error.originalError it always return undefined. Here my code:

const { constraintDirective, constraintDirectiveTypeDefs } = require('graphql-constraint-directive')
const express = require('express')
const { ApolloServer } = require('apollo-server-express')
const { makeExecutableSchema } = require('graphql-tools')
const typeDefs = `
  type Query {
    books: [Book]
  }
  type Book {
    title: String
  }
  type Mutation {
    createBook(input: BookInput): Book
  }
  input BookInput {
    title: String! @constraint(minLength: 5, format: "email")
  }`
const schema = makeExecutableSchema({
  typeDefs: [constraintDirectiveTypeDefs, typeDefs],
  schemaTransforms: [constraintDirective()]
})

const formatError = function (error) {
  if (error.originalError && error.originalError.code === 'ERR_GRAPHQL_CONSTRAINT_VALIDATION') {
    // return a custom object
  }

  console.log(error);

  return error
}

const app = express()
const server = new ApolloServer({ schema, formatError })

server.applyMiddleware({ app })

app.listen({ port: 3000 }, () =>
  console.log(`🚀 Server ready at http://localhost:3000${server.graphqlPath}`)
);

Please help me how can I fix it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
JonathanCallewaertcommented, Sep 30, 2021

I fixed returning my errors to the frontend by adding the following code:

    formatError: error => {
      // @ts-ignore
      if (error.originalError?.originalError?.code === 'ERR_GRAPHQL_CONSTRAINT_VALIDATION') {
        // @ts-ignore
        return {
          // @ts-ignore
          ...error.originalError?.originalError,
          message: error.originalError?.message,
          name: 'UserInputError',
          code: 'BAD_USER_INPUT',
        };
      }
      return error;
    },
1reaction
confusercommented, Jul 17, 2020

How are you using it? The debugger or console.log(util.inspect(error, {showHidden: true, depth: null})) should show the details. From what I recall, either GraphQL or Apollo defines them as hidden properties so a simple console.log won’t show them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling - Apollo GraphQL Docs
The formatError function does not modify errors that are sent to Apollo Studio as part of usage reporting. See For Apollo Studio reporting....
Read more >
Custom Error Object with Apollo Server - Stack Overflow
I'm having trouble because when I log error.code from within formatError it's not available. formatError: function (error) { console.
Read more >
Unreasonably Opaque GraphQL Errors (non-Error thrown as ...
So, I have a fix that does not require making modifications to useRedwoodLogger or formatError. Essentially, what I think happened was, in 0.50....
Read more >
apollo-error-converter - npm
There are no other projects in the npm registry using ... assign it to the formatError option in ApolloError constructor. new ApolloServer({.
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
Our function readUser(json) will not only read JSON, but check (“validate”) the data. If there are no required fields, or the format is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found