Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew E. Rhyne committed Nov 11, 2016
1 parent f7d919f commit 668819d
Show file tree
Hide file tree
Showing 15 changed files with 509 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"sourceMaps": true
}
10 changes: 10 additions & 0 deletions .editorconfig
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*.js]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
20 changes: 20 additions & 0 deletions .eslintrc
@@ -0,0 +1,20 @@
{
"ecmaFeatures": {
"modules": true
},
"env": {
"browser": true,
"node": true
},
"parser": "babel-eslint",
"rules": {
"quotes": [2, "single"],
"strict": [2, "never"],
"babel/new-cap": 1,
"babel/object-shorthand": 0,
"babel/arrow-parens": 0
},
"plugins": [
"babel"
]
}
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -35,3 +35,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# Yarn
yarn.lock
8 changes: 8 additions & 0 deletions .npmignore
@@ -0,0 +1,8 @@
src
test
.babelrc
.editorconfig
.eslintrc
.gitignore
circle.yml
Makefile
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,4 @@
1.0.1 / 2016-11-10
==================

* Initial release
45 changes: 45 additions & 0 deletions Makefile
@@ -0,0 +1,45 @@
PATH := node_modules/.bin:$(PATH)
SHELL := /bin/bash

UNAME_S := $(shell uname -s)

ifeq ($(UNAME_S),Linux)
OS_TYPE := linux
endif
ifeq ($(UNAME_S),Darwin)
OS_TYPE := osx
endif

.FORCE:

all: clean
babel src -d dist --source-maps

clean: .FORCE
rimraf npm-debug.log dist

osx-syspackages: .FORCE
brew update
brew install yarn

linux-syspackages: .FORCE
sudo apt-key adv --keyserver pgp.mit.edu --recv D101F7899D41F3C3
echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get -y update
sudo apt-get install yarn

environment: .FORCE
@if [ "${OS_TYPE}" = "osx" ]; then \
make osx-syspackages; \
else \
make linux-syspackages; \
fi

dependencies: .FORCE
yarn

test: all
mocha

lint: .FORCE
eslint src
104 changes: 103 additions & 1 deletion README.md
@@ -1,2 +1,104 @@
# apollo-errors
A sane way to create and throw custom errors with Apollo's graphql server
Machine-readable custom errors for Apollostack's GraphQL server

[![NPM](https://nodei.co/npm/apollo-errors.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/apollo-errors/)

[![CircleCI](https://circleci.com/gh/thebigredgeek/apollo-errors.svg?style=shield)](https://circleci.com/gh/thebigredgeek/apollo-errors/tree/master)


## Installation and usage

Install the package:

```bash
npm install apollo-errors
```

Create some errors:

```javascript
import { createError } from 'apollo-errors';

export const FooError = createError('FooError', {
message: 'A foo error has occurred'
});
```

Hook up formatting:

```javascript
import express from 'express';
import bodyParser from 'body-parser';
import { formatError } from 'apollo-errors';
import schema from './schema';

const app = express();

app.use('/graphql',
bodyParser.json(),
graphqlExpress({
formatError,
schema
})
);

app.listen(8080)
```

Throw some errors:

```javascript
import { FooError } from './errors';

const resolverThatThrowsError = (root, params, context) => {
throw new FooError({
data: {
something: 'important'
}
});
}
```

Witness glorious simplicity:

`POST /graphql (200)`

```json
{
"data": {},
"errors": [
{
"message":"A foo error has occurred",
"name":"FooError",
"time_thrown":"2016-11-11T00:40:50.954Z",
"data":{
"something": "important"
}
}
]
}
```

## API

### ApolloError ({ [time_thrown: String, data: Object]})

Creates a new ApolloError object. Note that `ApolloError` in this context refers
to an error class created and returned by `createError` documented below. Error can be
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).


### createError(name, {message: String, [data: Object]}): ApolloError

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.

### formatError (error, strict = false): ApolloError|Error|null
If the error is a known ApolloError, returns the serialized form of said error.

**Otherwise**, *if strict is not truthy*, returns the original error passed into formatError.

**Otherwise**, *if strict is truthy*, returns null.

## TODO

- Add better docs
14 changes: 14 additions & 0 deletions circle.yml
@@ -0,0 +1,14 @@
machine:
node:
version: 5.5.0

dependencies:
override:
- make environment
- make dependencies

test:
override:
- make lint
- make
- make test
111 changes: 111 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions package.json
@@ -0,0 +1,40 @@
{
"name": "apollo-errors",
"version": "1.0.1",
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
"main": "dist/index.js",
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thebigredgeek/apollo-errors.git"
},
"keywords": [
"apollostack",
"graphql",
"error",
"api"
],
"author": "Andrew E. Rhyne <rhyneandrew@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/thebigredgeek/apollo-errors/issues"
},
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
"dependencies": {
"es6-error": "^4.0.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.17.0",
"babel-eslint": "^7.0.0",
"babel-preset-es2015": "^6.16.0",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"eslint": "^3.8.1",
"eslint-plugin-babel": "^3.3.0",
"mocha": "^3.1.2",
"rimraf": "^2.5.4"
}
}

0 comments on commit 668819d

Please sign in to comment.