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

refactor: support koa 2 #27

Merged
merged 2 commits into from
Nov 9, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ const fs = require('fs');
const koa = require('koa');
const onerror = require('koa-onerror');

const app = koa();
const app = new koa();

onerror(app);

app.use(function*(){
app.use(ctx => {
// foo();
this.body = fs.createReadStream('not exist');
ctx.body = fs.createReadStream('not exist');
});
```

Expand Down
7 changes: 3 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -11,6 +10,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
27 changes: 11 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ module.exports = function onerror(app, options) {
// don't do anything if there is no error.
// this allows you to pass `this.onerror`
// to node-style callbacks.
if (err == null) {
return;
}
if (err == null) return;

// wrap non-error object
if (!(err instanceof Error)) {
Expand Down Expand Up @@ -57,17 +55,14 @@ module.exports = function onerror(app, options) {
if (headerSent) return;

// ENOENT support
if (err.code === 'ENOENT') {
err.status = 404;
}
if (err.code === 'ENOENT') err.status = 404;

if (typeof err.status !== 'number' || !http.STATUS_CODES[err.status]) {
err.status = 500;
}
this.status = err.status;

this.set(err.headers);

let type = 'text';
if (options.accepts) {
type = options.accepts.call(this, 'html', 'text', 'json');
Expand Down Expand Up @@ -100,12 +95,12 @@ module.exports = function onerror(app, options) {
* @param {Error} err
*/

function text(err) {
function text(err, ctx) {
// unset all headers, and set those specified
this.res._headers = {};
this.set(err.headers);
ctx.res._headers = {};
ctx.set(err.headers);

this.body = (isDev || err.expose) && err.message
ctx.body = (isDev || err.expose) && err.message
? err.message
: http.STATUS_CODES[this.status];
}
Expand All @@ -115,22 +110,22 @@ function text(err) {
* @param {Error} err
*/

function json(err) {
function json(err, ctx) {
const message = (isDev || err.expose) && err.message
? err.message
: http.STATUS_CODES[this.status];

this.body = { error: message };
ctx.body = { error: message };
}

/**
* default html error handler
* @param {Error} err
*/

function html(err) {
this.body = defaultTemplate
function html(err, ctx) {
ctx.body = defaultTemplate
.replace('{{status}}', err.status)
.replace('{{stack}}', err.stack);
this.type = 'html';
ctx.type = 'html';
}
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "koa error handler, hack ctx.onerror",
"main": "index.js",
"scripts": {
"test": "mocha test/*.test.js",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- test/*.test.js",
"test": "NODE_ENV=development egg-bin test",
"test-cov": "NODE_ENV=development egg-bin cov",
"ci": "npm run lint && npm run test-cov",
"lint": "eslint test *.js --fix"
},
Expand All @@ -30,20 +30,19 @@
"homepage": "https:/koajs/onerror",
"devDependencies": {
"autod": "*",
"ko-sleep": "*",
"egg-bin": "^4.3.5",
"egg-ci": "1",
"eslint": "3",
"eslint-config-egg": "3",
"istanbul": "*",
"koa": "1",
"mocha": "*",
"eslint": "4",
"eslint-config-egg": "5",
"ko-sleep": "*",
"koa": "2",
"pedding": "1",
"supertest": "3"
},
"engines": {
"node": ">= 4.0.0"
"node": ">= 8.0.0"
},
"ci": {
"version": "4, 6, 7"
"version": "8, 9"
}
}
6 changes: 3 additions & 3 deletions test/accepts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const onerror = require('..');
describe('accepts.test.js', function() {
it('should return json response', function(done) {
done = pedding(2, done);
const app = koa();
const app = new koa();
app.on('error', function() {});
onerror(app, {
accepts() {
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('accepts.test.js', function() {
});

it('should redrect when accepts type not json', function(done) {
const app = koa();
const app = new koa();
app.on('error', function() {});
onerror(app, {
accepts() {
Expand All @@ -59,7 +59,7 @@ describe('accepts.test.js', function() {
});
});

function* commonError() {
function commonError() {
// eslint-disable-next-line
foo();
}
2 changes: 1 addition & 1 deletion test/fluid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const onerror = require('..');

describe('fluid.test.js', function() {
it('should return app reference', function() {
const app = koa();
const app = new koa();
const res = onerror(app);
assert(res instanceof koa);
assert(res === app);
Expand Down
16 changes: 8 additions & 8 deletions test/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const onerror = require('..');

describe('html.test.js', function() {
it('should common error ok', function(done) {
const app = koa();
const app = new koa();
app.on('error', function() {});
onerror(app);
app.use(commonError);
Expand All @@ -20,7 +20,7 @@ describe('html.test.js', function() {
});

it('should common error after sleep a little while ok', function(done) {
const app = koa();
const app = new koa();
app.on('error', function() {});
onerror(app);
app.use(commonSleepError);
Expand All @@ -32,7 +32,7 @@ describe('html.test.js', function() {
});

it('should stream error ok', function(done) {
const app = koa();
const app = new koa();
app.on('error', function() {});
onerror(app);
app.use(streamError);
Expand All @@ -45,17 +45,17 @@ describe('html.test.js', function() {
});
});

function* commonError() {
function commonError() {
// eslint-disable-next-line
foo();
}

function* commonSleepError() {
yield sleep(50);
async function commonSleepError() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async function test case is here

await sleep(50);
// eslint-disable-next-line
fooAfterSleep();
}

function* streamError() {
this.body = fs.createReadStream('not exist');
function streamError(ctx) {
ctx.body = fs.createReadStream('not exist');
}
32 changes: 16 additions & 16 deletions test/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const onerror = require('..');

describe('json.test.js', () => {
it('should common error ok', done => {
const app = koa();
const app = new koa();
app.on('error', () => {});
onerror(app);
app.use(commonError);
Expand All @@ -22,7 +22,7 @@ describe('json.test.js', () => {
});

it('should stream error ok', done => {
const app = koa();
const app = new koa();
app.on('error', () => {});
onerror(app);
app.use(streamError);
Expand All @@ -39,7 +39,7 @@ describe('json.test.js', () => {
});

it('should custom handler', done => {
const app = koa();
const app = new koa();
app.on('error', () => {});
onerror(app, {
json() {
Expand All @@ -59,7 +59,7 @@ describe('json.test.js', () => {
});

it('should show status error when err.message not present', done => {
const app = koa();
const app = new koa();
app.on('error', () => {});
onerror(app);
app.use(emptyError);
Expand All @@ -72,10 +72,10 @@ describe('json.test.js', () => {
});

it('should wrap non-error object', done => {
const app = koa();
const app = new koa();
app.on('error', () => {});
onerror(app);
app.use(function* () {
app.use(() => {
throw 1;
});

Expand All @@ -88,7 +88,7 @@ describe('json.test.js', () => {

it('should wrap mock error obj instead of Error instance', done => {
done = pedding(2, done);
const app = koa();
const app = new koa();
app.on('error', err => {
assert(err instanceof Error);
assert(err.name === 'TypeError');
Expand All @@ -97,7 +97,7 @@ describe('json.test.js', () => {
done();
});
onerror(app);
app.use(function* () {
app.use(() => {
const err = {
name: 'TypeError',
message: 'mock error',
Expand All @@ -117,7 +117,7 @@ describe('json.test.js', () => {
});

it('should custom handler with ctx', done => {
const app = koa();
const app = new koa();
app.on('error', () => {});
onerror(app, {
json: (err, ctx) => {
Expand All @@ -137,7 +137,7 @@ describe('json.test.js', () => {
});

it('should get headerSent in error listener', done => {
const app = koa();
const app = new koa();
app.on('error', err => {
assert(err.headerSent);
done();
Expand All @@ -151,8 +151,8 @@ describe('json.test.js', () => {
},
});

app.use(function* () {
this.res.flushHeaders();
app.use(ctx => {
ctx.res.flushHeaders();
throw new Error('mock error');
});

Expand All @@ -164,17 +164,17 @@ describe('json.test.js', () => {
});
});

function* emptyError() {
function emptyError() {
const err = new Error('');
err.expose = true;
throw err;
}

function* commonError() {
function commonError() {
// eslint-disable-next-line
foo();
}

function* streamError() {
this.body = fs.createReadStream('not exist');
function streamError(ctx) {
ctx.body = fs.createReadStream('not exist');
}
Loading