Skip to content

Commit 668819d

Browse files
author
Andrew E. Rhyne
committed
initial
1 parent f7d919f commit 668819d

15 files changed

+509
-1
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015"],
3+
"sourceMaps": true
4+
}

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*.js]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintrc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"ecmaFeatures": {
3+
"modules": true
4+
},
5+
"env": {
6+
"browser": true,
7+
"node": true
8+
},
9+
"parser": "babel-eslint",
10+
"rules": {
11+
"quotes": [2, "single"],
12+
"strict": [2, "never"],
13+
"babel/new-cap": 1,
14+
"babel/object-shorthand": 0,
15+
"babel/arrow-parens": 0
16+
},
17+
"plugins": [
18+
"babel"
19+
]
20+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
# Yarn
40+
yarn.lock

.npmignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
src
2+
test
3+
.babelrc
4+
.editorconfig
5+
.eslintrc
6+
.gitignore
7+
circle.yml
8+
Makefile

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1.0.1 / 2016-11-10
2+
==================
3+
4+
* Initial release

Makefile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
PATH := node_modules/.bin:$(PATH)
2+
SHELL := /bin/bash
3+
4+
UNAME_S := $(shell uname -s)
5+
6+
ifeq ($(UNAME_S),Linux)
7+
OS_TYPE := linux
8+
endif
9+
ifeq ($(UNAME_S),Darwin)
10+
OS_TYPE := osx
11+
endif
12+
13+
.FORCE:
14+
15+
all: clean
16+
babel src -d dist --source-maps
17+
18+
clean: .FORCE
19+
rimraf npm-debug.log dist
20+
21+
osx-syspackages: .FORCE
22+
brew update
23+
brew install yarn
24+
25+
linux-syspackages: .FORCE
26+
sudo apt-key adv --keyserver pgp.mit.edu --recv D101F7899D41F3C3
27+
echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
28+
sudo apt-get -y update
29+
sudo apt-get install yarn
30+
31+
environment: .FORCE
32+
@if [ "${OS_TYPE}" = "osx" ]; then \
33+
make osx-syspackages; \
34+
else \
35+
make linux-syspackages; \
36+
fi
37+
38+
dependencies: .FORCE
39+
yarn
40+
41+
test: all
42+
mocha
43+
44+
lint: .FORCE
45+
eslint src

README.md

+103-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,104 @@
11
# apollo-errors
2-
A sane way to create and throw custom errors with Apollo's graphql server
2+
Machine-readable custom errors for Apollostack's GraphQL server
3+
4+
[![NPM](https://nodei.co/npm/apollo-errors.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/apollo-errors/)
5+
6+
[![CircleCI](https://circleci.com/gh/thebigredgeek/apollo-errors.svg?style=shield)](https://circleci.com/gh/thebigredgeek/apollo-errors/tree/master)
7+
8+
9+
## Installation and usage
10+
11+
Install the package:
12+
13+
```bash
14+
npm install apollo-errors
15+
```
16+
17+
Create some errors:
18+
19+
```javascript
20+
import { createError } from 'apollo-errors';
21+
22+
export const FooError = createError('FooError', {
23+
message: 'A foo error has occurred'
24+
});
25+
```
26+
27+
Hook up formatting:
28+
29+
```javascript
30+
import express from 'express';
31+
import bodyParser from 'body-parser';
32+
import { formatError } from 'apollo-errors';
33+
import schema from './schema';
34+
35+
const app = express();
36+
37+
app.use('/graphql',
38+
bodyParser.json(),
39+
graphqlExpress({
40+
formatError,
41+
schema
42+
})
43+
);
44+
45+
app.listen(8080)
46+
```
47+
48+
Throw some errors:
49+
50+
```javascript
51+
import { FooError } from './errors';
52+
53+
const resolverThatThrowsError = (root, params, context) => {
54+
throw new FooError({
55+
data: {
56+
something: 'important'
57+
}
58+
});
59+
}
60+
```
61+
62+
Witness glorious simplicity:
63+
64+
`POST /graphql (200)`
65+
66+
```json
67+
{
68+
"data": {},
69+
"errors": [
70+
{
71+
"message":"A foo error has occurred",
72+
"name":"FooError",
73+
"time_thrown":"2016-11-11T00:40:50.954Z",
74+
"data":{
75+
"something": "important"
76+
}
77+
}
78+
]
79+
}
80+
```
81+
82+
## API
83+
84+
### ApolloError ({ [time_thrown: String, data: Object]})
85+
86+
Creates a new ApolloError object. Note that `ApolloError` in this context refers
87+
to an error class created and returned by `createError` documented below. Error can be
88+
initialized with a custom `time_thrown` ISODate (default is current ISODate) and `data` object (which will be merged with data specified through `createError`, if it exists).
89+
90+
91+
### createError(name, {message: String, [data: Object]}): ApolloError
92+
93+
Creates and returns an error class with the given `name` and `message`, optionally initialized with the given `data`. `data` passed to `createError` will later be merged with any data passed to the constructor.
94+
95+
### formatError (error, strict = false): ApolloError|Error|null
96+
If the error is a known ApolloError, returns the serialized form of said error.
97+
98+
**Otherwise**, *if strict is not truthy*, returns the original error passed into formatError.
99+
100+
**Otherwise**, *if strict is truthy*, returns null.
101+
102+
## TODO
103+
104+
- Add better docs

circle.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
machine:
2+
node:
3+
version: 5.5.0
4+
5+
dependencies:
6+
override:
7+
- make environment
8+
- make dependencies
9+
10+
test:
11+
override:
12+
- make lint
13+
- make
14+
- make test

dist/index.js

+111
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "apollo-errors",
3+
"version": "1.0.1",
4+
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"test": "make test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/thebigredgeek/apollo-errors.git"
12+
},
13+
"keywords": [
14+
"apollostack",
15+
"graphql",
16+
"error",
17+
"api"
18+
],
19+
"author": "Andrew E. Rhyne <[email protected]>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/thebigredgeek/apollo-errors/issues"
23+
},
24+
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
25+
"dependencies": {
26+
"es6-error": "^4.0.0"
27+
},
28+
"devDependencies": {
29+
"babel-cli": "^6.18.0",
30+
"babel-core": "^6.17.0",
31+
"babel-eslint": "^7.0.0",
32+
"babel-preset-es2015": "^6.16.0",
33+
"babel-register": "^6.18.0",
34+
"chai": "^3.5.0",
35+
"eslint": "^3.8.1",
36+
"eslint-plugin-babel": "^3.3.0",
37+
"mocha": "^3.1.2",
38+
"rimraf": "^2.5.4"
39+
}
40+
}

0 commit comments

Comments
 (0)