Giter Site home page Giter Site logo

graphql-middleware-sentry's Introduction

graphql-middleware-sentry

CircleCI npm version

GraphQL Middleware plugin for Sentry.

Usage

With GraphQL Yoga

import { GraphQLServer } from 'graphql-yoga'
import { sentry } from 'graphql-middleware-sentry'

const typeDefs = `
  type Query {
    hello: String!
    bug: String!
  }
`

const resolvers = {
  Query: {
    hello: () => `Hey there!`
    bug: () => {
      throw new Error(`Many bugs!`)
    }
  }
}

const sentryMiddleware = sentry({
  config: {
    dsn: process.env.SENTRY_DSN,
    environment: process.env.NODE_ENV
  },
  withScope: (scope, error, context) => {
    scope.setUser({
      id: context.authorization.userId,
    });
    scope.setExtra('body', context.request.body)
    scope.setExtra('origin', context.request.headers.origin)
    scope.setExtra('user-agent', context.request.headers['user-agent'])
  },
})

const server = GraphQLServer({
  typeDefs,
  resolvers,
  middlewares: [sentryMiddleware]
})

server.start(() => `Server running on http://localhost:4000`)

Using a Sentry instance

In cases where you want to use your own instance of Sentry to use it in other places in your application you can pass the sentryInstance. The config property should not be passed as an option.

Example usage with a Sentry instance

Sentry.init({
  dsn: process.env.SENTRY_DSN,
})

const sentryMiddleware = sentry({
  sentryInstance: Sentry,
  withScope: (scope, error, context) => {
    scope.setExtra('origin', context.request.headers.origin)
  },
})

API & Configuration

export interface Options<Context> {
  sentryInstance?: Sentry
  config?: Sentry.NodeOptions
  withScope?: ExceptionScope<Context>
  captureReturnedErrors?: boolean
  forwardErrors?: boolean
  reportError?: (res: Error | any) => boolean
}

function sentry<Context>(options: Options<Context>): IMiddlewareFunction

Sentry context

To enrich events sent to Sentry, you can modify the context. This can be done using the withScope configuration option.

The withScope option is a function that is called with the current Sentry scope, the error, and the GraphQL Context.

type ExceptionScope<Context> = (
  scope: Sentry.Scope,
  error: Error,
  context: Context,
) => void

Filtering Out Custom Errors

To filter out custom errors thrown by your server (such as "You Are Not Logged In"), use the reportError option and return a boolean for whether or not the error should be sent to sentry.

class CustomError extends Error {}

const sentryMiddleware = sentry({
  reportError: (res) => {
    // you can check the error message strings
    if (res.message === 'You Are Not Logged In') {
      return false;
    }

    // or extend the error type and create a custom error
    if (res instanceof CustomError) {
      return false;
    }

    return true;
  }
})

Options

property required description
sentryInstance false Sentry's instance
config false Sentry's config object
withScope false Function to modify the Sentry context to send with the captured error.
captureReturnedErrors false Capture errors returned from other middlewares, e.g., graphql-shield returns errors from rules and resolvers
forwardErrors false Should middleware forward errors to the client or block them.
reportError false Function that passes res as the parameter and accepts a boolean callback for whether or not the error should be captured

Note

If sentryInstance is not passed then config.dsn is required and vice-versa.

License

This project is licensed under the MIT License.

graphql-middleware-sentry's People

Watchers

 avatar  avatar

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.