Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom accepts #4

Merged
merged 2 commits into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- '0.11'
script: "make test-travis"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ TESTS = test/*.test.js
REPORTER = spec
TIMEOUT = 3000
MOCHA_OPTS =
REGISTRY = --registry=http://r.cnpmjs.org
REGISTRY = --registry=https://registry.npm.taobao.org

install:
@npm install $(REGISTRY) \
--disturl=http://dist.cnpmjs.org
--disturl=http://npm.taobao.org/dist

jshint: install
@-./node_modules/.bin/jshint ./

test: install
@./node_modules/.bin/mocha \
--harmony \
--bail \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
Expand All @@ -31,10 +30,20 @@ test-cov: install
--require should \
$(MOCHA_OPTS) \
$(TESTS)
@./node_modules/.bin/cov coverage

test-travis: install
@node --harmony \
node_modules/.bin/istanbul cover --preserve-comments \
./node_modules/.bin/_mocha \
--report lcovonly \
-- \
--reporter dot \
--timeout $(TIMEOUT) \
$(MOCHA_OPTS) \
$(TESTS)

autod: install
@./node_modules/.bin/autod $(REGISTRY) -w
@./node_modules/.bin/autod $(REGISTRY) -w --prefix "~" -e example.js
@$(MAKE) install

.PHONY: test test-all
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
koa-onerror
=================
[![Build Status](https://travis-ci.org/koajs/onerror.svg?branch=master)](https://travis-ci.org/koajs/onerror)

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Gittip][gittip-image]][gittip-url]
[![David deps][david-image]][david-url]

[npm-image]: https://img.shields.io/npm/v/koa-onerror.svg?style=flat
[npm-url]: https://npmjs.org/package/koa-onerror
[travis-image]: https://img.shields.io/travis/koajs/onerror.svg?style=flat
[travis-url]: https://travis-ci.org/koajs/onerror
[coveralls-image]: https://img.shields.io/coveralls/koajs/onerror.svg?style=flat
[coveralls-url]: https://coveralls.io/r/koajs/onerror?branch=master
[gittip-image]: https://img.shields.io/gittip/dead_horse.svg?style=flat
[gittip-url]: https://www.gittip.com/dead_horse/
[david-image]: https://img.shields.io/david/koajs/onerror.svg?style=flat
[david-url]: https://david-dm.org/koajs/onerror

an error handler for koa, hack ctx.onerror.

Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function onerror(app, options) {
json: json,
redirect: null,
template: path.join(__dirname, 'error.html'),
accepts: null,
};

copy(defaultOptions).to(options);
Expand Down Expand Up @@ -66,8 +67,13 @@ function onerror(app, options) {
}

this.status = err.status || 500;

var type = this.accepts('html', 'text', 'json') || 'text';
var type = 'text';
if (options.accepts) {
type = options.accepts.call(this, 'html', 'text', 'json');
} else {
type = this.accepts('html', 'text', 'json');
}
type = type || 'text';
if (options.all) {
options.all.call(this, err);
} else {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
},
"homepage": "https://github.com/koajs/onerror",
"devDependencies": {
"autod": "^0.1.3",
"co-sleep": "0.0.1",
"cov": "*",
"autod": "*",
"co-sleep": "~0.0.1",
"istanbul-harmony": "*",
"jshint": "*",
"koa": "^0.5.5",
"koa": "~0.8.2",
"mocha": "*",
"pedding": "~1.0.0",
"should": "*",
"supertest": "0.11.0"
"supertest": "~0.13.0"
},
"dependencies": {
"copy-to": "^1.0.1",
"swig": "^1.3.2"
"copy-to": "~1.0.1",
"swig": "~1.4.2"
}
}
79 changes: 79 additions & 0 deletions test/accepts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**!
* koa-onerror - test/accepts.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var fs = require('fs');
var koa = require('koa');
var request = require('supertest');
var pedding = require('pedding');
var onerror = require('..');

describe('accepts.test.js', function () {
it('should return json response', function (done) {
done = pedding(2, done);
var app = koa();
app.on('error', function () {});
onerror(app, {
accepts: function () {
if (this.url.indexOf('.json') > 0) {
return 'json';
}
return 'text';
}
});
app.use(commonError);

request(app.callback())
.get('/user.json')
.set('Accept', '*/*')
.expect(500)
.expect('Content-Type', 'application/json; charset=utf-8')
.expect({ error: 'foo is not defined' }, done);

request(app.callback())
.get('/user')
.set('Accept', 'application/json')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('foo is not defined', done);
});

it('should redrect when accepts type not json', function (done) {
var app = koa();
app.on('error', function () {});
onerror(app, {
accepts: function () {
if (this.url.indexOf('.json') > 0) {
return 'json';
}
return 'text';
},
redirect: 'http://foo.com/500.html'
});
app.use(commonError);

request(app.callback())
.get('/user')
.set('Accept', '*/*')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect('Location', 'http://foo.com/500.html')
.expect('Redirecting to <a href="http://foo.com/500.html">http://foo.com/500.html</a>.')
.expect(302, done);
});
});

function* commonError() {
foo();
}
6 changes: 3 additions & 3 deletions test/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var onerror = require('..');
describe('html.test.js', function () {
it('should common error ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(commonError);

Expand All @@ -31,7 +31,7 @@ describe('html.test.js', function () {

it('should common error after sleep a little while ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(commonSleepError);

Expand All @@ -43,7 +43,7 @@ describe('html.test.js', function () {

it('should stream error ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(streamError);

Expand Down
6 changes: 3 additions & 3 deletions test/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var request = require('supertest');
describe('json.test.js', function () {
it('should common error ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(commonError);

Expand All @@ -31,7 +31,7 @@ describe('json.test.js', function () {

it('should stream error ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(streamError);

Expand All @@ -44,7 +44,7 @@ describe('json.test.js', function () {

it('should custom handler', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app, {
json: function () {
this.status = 500;
Expand Down
8 changes: 4 additions & 4 deletions test/redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var onerror = require('..');
describe('redirect.test.js', function () {
it('should handle error and redirect to real error page', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app, {
redirect: 'http://example/500.html'
});
Expand All @@ -38,7 +38,7 @@ describe('redirect.test.js', function () {

it('should got text/plain header', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app, {
redirect: 'http://example/500.html'
});
Expand All @@ -54,7 +54,7 @@ describe('redirect.test.js', function () {

it('should show json when accept is json', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app, {
redirect: 'http://example/500.html'
});
Expand All @@ -63,7 +63,7 @@ describe('redirect.test.js', function () {
request(app.callback())
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect({"error":"foo is not defined"}, done);
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var onerror = require('..');
describe('text.test.js', function () {
it('should common error ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(commonError);

Expand All @@ -31,7 +31,7 @@ describe('text.test.js', function () {

it('should show error message ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(exposeError);

Expand All @@ -44,7 +44,7 @@ describe('text.test.js', function () {

it('should stream error ok', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app);
app.use(streamError);

Expand All @@ -57,7 +57,7 @@ describe('text.test.js', function () {

it('should custom handler', function (done) {
var app = koa();
app.outputErrors = false;
app.on('error', function () {});
onerror(app, {
text: function () {
this.status = 500;
Expand Down