• Stars
    star
    253
  • Rank 160,776 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Better error handling for GraphQL

GraphQL Errors

When an error occurs when processing GraphQL queries, graphql-js sends the complete error message to the client with the response. In most cases, sending error messages to the client without supervision is a bad idea since those might leak sensitive information.

The graphql-errors module fixes this issue by masking error messages sent to the client. This module intercepts GraphQL error messages and replaces them with "Internal error" and a UUID. It also logs the error on the server with the stacktrace and it's UUID, making user bug reports easy to cross-reference.

Usage

const { maskErrors } = require('graphql-errors');

const schema = new graphql.GraphQLSchema({
  // ...your schema here...
});

// Mask the error messages
maskErrors(schema);

// Use your schema like you normally would, for example:
app.use('/', graphqlHTTP({ schema: schema }));

User errors

Some error messages you do want to send to the user though, like permission errors, so graphql-errors exports a UserError class. Throwing a UserError will not mask the error message so your users sees the exact one you threw:

const { UserError } = require('graphql-errors')

const resolvers = {
  Query: {
    hiddenField() {
      // Your user sees: "Permission denied."
      throw new UserError('Permission denied.');
    }
  }
}

Example Error

Let's say your database throws an error because you exceeded some limit. Normally your user would see an error message saying "Database limit exceeded.", but not with graphql-errors!

What the user gets in the response

{
  "data": {
    "post": null
  },
  "errors": [
    {
      "message": "Internal Error: e553aaa4-47dc-47db-9bfc-314cc2cf5833",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "post"
      ]
    }
  ]
}

As you can see, no sensitive information is leaked to the user at all. You might think this'll make bug reports less useful, but note how a UUID is attached to the error message!

What you see in the server console

Error: Database limit exceeded.: e553aaa4-47dc-47db-9bfc-314cc2cf5833
    at post (/project/server/queries/post.js:10:35)
    at _callee$ (/project/node_modules/graphql-errors/dist/index.js:140:36)
    at tryCatch (/project/node_modules/regenerator-runtime/runtime.js:64:40)
    at Generator.invoke [as _invoke] (/project/node_modules/regenerator-runtime/runtime.js:355:22)
    at Generator.prototype.(anonymous function) [as next] (/project/node_modules/regenerator-runtime/runtime.js:116:21)
    at step (/project/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /project/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
    at F (/project/node_modules/babel-runtime/node_modules/core-js/library/modules/_export.js:35:28)
    at /project/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12
    at /project/node_modules/graphql-errors/dist/index.js:160:18
    at resolveOrError (/project/node_modules/graphql/execution/execute.js:475:12)
    at resolveField (/project/node_modules/graphql/execution/execute.js:461:16)
    at /project/node_modules/graphql/execution/execute.js:275:18
    at Array.reduce (native)
    at executeFields (/project/node_modules/graphql/execution/execute.js:272:42)
    at executeOperation (/project/node_modules/graphql/execution/execute.js:212:10)

Note how the same UUID ("e553aaa4-47dc-47db-9bfc-314cc2cf5833") is sent to the user and logged together with the stack trace, making it easy to cross-reference user bug reports to your server logs.

More Repositories

1

lokka

Simple JavaScript Client for GraphQL
JavaScript
1,532
star
2

flow-router

Carefully Designed Client Side Router for Meteor
JavaScript
1,089
star
3

mantra

Mantra - An Application Architecture for Meteor
Shell
980
star
4

fast-render

Render you app even before the DDP connection is live. - magic?
JavaScript
559
star
5

subs-manager

Subscriptions Manager for Meteor
JavaScript
358
star
6

blaze-layout

Layout Manager for Blaze (works well with Meteor FlowRouter)
JavaScript
198
star
7

npm-base

A base package for creating NPM packages with ES2015
JavaScript
161
star
8

meteor-debug

Full Stack Debugging Solution for Meteor
JavaScript
152
star
9

meteor-react-layout

Simple React Layout Manager for Meteor with SSR Support
JavaScript
138
star
10

meteor-dochead

Isomorphic way to manipulate document.head for Meteor apps
JavaScript
134
star
11

react-mounter

A simple way to mount React components
JavaScript
130
star
12

meteor-graphql

GraphQL Support for Meteor with Lokka
JavaScript
90
star
13

graphql-blog-schema

GraphQL Schema for a Blog App
CSS
84
star
14

graphqlify

Build GraphQL queries with JavaScript
JavaScript
56
star
15

react-simple-di

Simple Dependancy Injection Solution for React
JavaScript
56
star
16

react-stubber

Simple but useful stubbing solution React
JavaScript
50
star
17

blaze-plus

Adds Props and State Management functionality to Meteor's Blaze Template Engine.
JavaScript
50
star
18

meteor-login-state

Share Login State Between the Domain
JavaScript
45
star
19

meteor-reaktor

Easy to use React Frontend for FlowRouter
JavaScript
42
star
20

lokka-transport-http

HTTP Transport Layer for Lokka
JavaScript
41
star
21

lokka-transport-jwt-auth

Lokka GraphQL Transport with JWT Support
JavaScript
22
star
22

graphql-utils-js

A set of utilities for apps building with graphql-js
JavaScript
20
star
23

mongo-sharded-cluster

Shard MongoDB in the app layer
JavaScript
13
star
24

meteor-stripe-konnect

Stripe for Meteor Apps
JavaScript
12
star
25

mantra-tutor-lessons

11
star
26

kadiyadb

KadiraDB is a low level database for storing time series data
Go
10
star
27

node-eventloop-monitor

Simple way to monitor eventloop blockness in Node.js
JavaScript
9
star
28

node-base

Base app for all the server side node apps at Kadira
JavaScript
6
star
29

nofat

Set of tools to kick start server side ES2016 development
JavaScript
6
star
30

kadira-core

JavaScript
5
star
31

regex-query-filter

JavaScript
3
star
32

storybook-addon-hello

A simple hello-world example addon for storybook
JavaScript
3
star
33

lokka-transport-http-auth

HTTP Transport for Lokka with Basic Auth
JavaScript
2
star
34

meteor-string-highlighter

String Highlighter for Meteor Apps
JavaScript
2
star
35

fastcall

A fast bi-directional communication layer over tcp
Go
2
star
36

storybook-ping-receiver

Listen to anonymous usage pings sent from storybooks
JavaScript
2
star
37

url-mailer

JavaScript
2
star
38

kadira-debug-toy

MeteorToys integration for kadira:debug Package
JavaScript
2
star
39

kadira.io

Kadira Blog
JavaScript
2
star
40

go-tools

Go tools is a collection of re-usable go packages used by multiple KadiraHQ projects
Go
2
star
41

mongo-mask

Replaces data with masked values in mongo queries
JavaScript
1
star
42

kadiradb-node

NodejS client for KadiraDB metrics database.
JavaScript
1
star
43

kadiradb

A real time metrics database which uses KadiraDB under the hood
Go
1
star