npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-error-tracking-extension

v0.1.0

Published

GraphQL server extension to track requests with errors

Downloads

38

Readme

graphql-error-tracking-extension

npm version Build Status

This GraphQL extension for Apollo Server 2 adds two functionalities:

  1. It helps you to log the request context in case of an Exception is happening in your GraphQL API. A trace-id will additionally help you to find the corresponding log entries.
  2. You can define which Exception types you do reveal the real error to the client or just want to return a generic "internal server error"

Example log using this extension:

[dded97442947d] {"content-type":"application/json","cache-control":"no-cache","postman-token":"c824faa8-0287-406a-b326-bdbb173ee30d","authorization":"***","user-agent":"PostmanRuntime/7.6.0","accept":"*/*","host":"localhost:8080","accept-encoding":"gzip, deflate","content-length":"103","connection":"keep-alive"}
[dded97442947d] Syntax Error: Expected Name, found {
[dded97442947d] Original error body:  query {getSetting(settingsId:"123", language: "en") { features { {onPremises} }}

In this case you can easily see that the Exception was just caused by a syntax error. However, if you need to find a real bug the corresponding query can be crucial.

Usage

  • This package needs graphql-extensions to be installed: npm i graphql-extensions --save
  • Install the npm package as a dependency npm i graphql-error-tracking-extension --save
  • Add this extension and the request to the GraphQL context like this:
import {ApolloServer} from 'apollo-server-express';
import {GraphQLErrorTrackingExtension} from 'graphql-error-tracking-extension';

const server = new ApolloServer({
    schema,
    extensions: [() => new GraphQLErrorTrackingExtension()],
    context: ({req}) => ({
        request: req
    })
});

Configuration

The GraphQLErrorTrackingExtension class takes an optional configuration object new GraphQLErrorTrackingExtension(config).

maskHeaders

Replaces http headers with sensitive information with '***'.

Default: ['authorization']

revealErrorTypes

Define which error types (classes) should be revealed to the client. Default is to reveal all original errors to the client. If you set this option, all errors not in the list will be mapped to an Internal Server Error before sending a response to the client. Also have a look to the already available error types defined by Apollo Server 2 link.

Default: null

Example

import {ApolloServer, SyntaxError, UserInputError, AuthenticationError, ForbiddenError} from 'apollo-server-express';
import {GraphQLErrorTrackingExtension} from 'graphql-error-tracking-extension';

const server = new ApolloServer({
    schema,
    extensions: [() => new GraphQLErrorTrackingExtension({
        revealErrorTypes: [SyntaxError, UserInputError, AuthenticationError, ForbiddenError]
    })],
    context: ({req}) => ({
        request: req
    })
});

Important: the array takes the JS classes, not strings!

onUnrevealedError

If you have defined revealErrorTypes, this callback gets called if an error was mapped to Internal Server Error. Whatever you do in this callback, the Internal Server Error is send to the client, but you can use it to e.g. forward this unexpected error to another monitoring system.

Default: null

Example

import {ApolloServer, SyntaxError, UserInputError, AuthenticationError, ForbiddenError} from 'apollo-server-express';
import {GraphQLErrorTrackingExtension} from 'graphql-error-tracking-extension';
import {ErrorReporting} from '@google-cloud/error-reporting';
const errorReporting = new ErrorReporting();

const server = new ApolloServer({
    schema,
    extensions: [() => new GraphQLErrorTrackingExtension({
        revealErrorTypes: [SyntaxError, UserInputError, AuthenticationError, ForbiddenError],
        onUnrevealedError: (err, originalError) => {
            if (originalError) {
                errorReporting.report(err.originalError.stack);
            } else {
                errorReporting.report(err.stack);
            }
        }
    })],
    context: ({req}) => ({
        request: req
    })
});