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.

How to implement provide context on global error handlers

See original GitHub issue

I just check the below PR but still do not know how to implement. Any else can help me? https://github.com/apollographql/apollo-server/issues/1343

Thank you so much

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
Whoaa512commented, Jan 17, 2019

@theduyetmai, if you use the extensions API (which is not well documented, so I suggest reading the source). You can create a custom error tracking extension that will be passed the context to each of the lifecycle methods.

const { ApolloServer } = require('apollo-server')

import { GraphQLExtension } from 'graphql-extensions'

class MyErrorTrackingExtension extends GraphQLExtension {
    willSendResponse(o) {
        const { context, graphqlResponse } = o

        context.trackErrors(graphqlResponse.errors)

        return o
    }
    // Other lifecycle methods include
    // requestDidStart
    // parsingDidStart
    // validationDidStart
    // executionDidStart
    // willSendResponse
}

const server = new ApolloServer({
    typeDefs,
    resolvers,
    extensions: [() => new MyErrorTrackingExtension()],
    context({ req }) {
        return {
            req,
            trackErrors(errors) {
                // Track the errors
            },
        }
    },
})

server.listen().then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`)
})
1reaction
omgriefcommented, Oct 11, 2018

Bumping this issue, the fix brought in to v2.0.8 does seem like it would be the fix to allow for the context to be accessible from within the formatError function, but it is not the case as it never gets passed to the actual formatter, and is just returned by the extension.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global Error Handling in ASP.NET Core Web API - Code Maze
To start off with this example, let's open the Values Controller from the starting project (Global-Error-Handling-Start project). In this ...
Read more >
How to Implement Global Exception Handling in Asp.NET 5.0 ...
1. Process the request in InvokeAsync method · 2. If it threw an unhandled exception, anywhere in the pipeline, log it · 3....
Read more >
Configuring a global error handler for camel? - Stack Overflow
Instantiate a default camel context; Retrieve a list of RouteDefinition from a spring context; Add these definitions to the camel context by calling...
Read more >
Implement Global Exception Handling In ASP.NET Core ...
Use the UseExceptionHandler middleware in ASP.NET Core. So, to implement the global exception handler, we can use the benefits of the ASP.NET ...
Read more >
Handling global notifications with React's Context API - YLD
In this article, we aim to present a use case on how to use React Hooks and Context API in order to show...
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