Coder Social home page Coder Social logo

Comments (2)

jaydenseric avatar jaydenseric commented on May 30, 2024

Part of the philosophy of this package is to do things the Koa way, allowing people to use normal middleware and app error handlers to do the things they need.

here is how I log errors in one of my GraphQL APIs:

const app = new Koa()

// … Setup middleware, etc.

app.on('error', error => {
  if (error.graphqlErrors) {
    const unexposed = error.graphqlErrors.filter(
      ({ originalError }) =>
        // Originally thrown in resolvers (not a GraphQL validation error).
        originalError &&
        // Not specifically marked to be exposed to the client.
        !originalError.expose
    )
    if (unexposed.length) {
      console.group('Unexposed GraphQL errors:')
      unexposed.forEach(error => {
        console.log('\x1b[31m')
        console.log(error.toString())
        console.log('\x1b[2m', error.stack)
        console.log('\x1b[0m')
      })
      console.groupEnd()
    }
  } else console.error(error)
})

Don't worry about the \x looking codes, they are to colorize the console output.

If some errors are not exposed to the client, this error handler logs them on the server. The idea is that if any errors are exposed to the client, it is not a server error and as such should not be logged as an error on the server. Instead you should see the errors in your browser console, or however your GraphQL client reports errors.

from graphql-api-koa.

BigsonLvrocha avatar BigsonLvrocha commented on May 30, 2024

Thanks for the response, but that solution did not work
Since it's an exception thrown in the execute function, it doesn't fire and events or passes through any middleware, so I've managed to see the graphql errors by wrapping the route definition in a try catch block

try {
  router.post('/graphql', errorHandler(), execute({ schema }));
} catch (error) {
  console.error(error);
  throw error;
}

from graphql-api-koa.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.