Skip to content

Commit

Permalink
fix: the req data should be consumed on error (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Aug 19, 2018
1 parent 7ce47c6 commit d4291c2
Show file tree
Hide file tree
Showing 14 changed files with 56,076 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: false
language: node_js
node_js:
- '8'
- '9'
- '10'
install:
- npm i npminstall && npminstall
script:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 node_modules.
Copyright (c) 2015 - 2016 koajs and other contributors.
Copyright (c) 2015 - present koajs and other contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '9'
- nodejs_version: '10'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const http = require('http');
const path = require('path');
const fs = require('fs');
const escapeHtml = require('escape-html');
const sendToWormhole = require('stream-wormhole');

const env = process.env.NODE_ENV || 'development';
const isDev = env === 'development';
Expand All @@ -30,6 +31,9 @@ module.exports = function onerror(app, options) {
// to node-style callbacks.
if (err == null) return;

// ignore all pedding request stream
if (this.req) sendToWormhole(this.req);

// wrap non-error object
if (!(err instanceof Error)) {
const newError = new Error('non-error thrown: ' + err);
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@
"homepage": "https://github.com/koajs/onerror",
"devDependencies": {
"autod": "*",
"co-busboy": "^1.4.0",
"egg-bin": "^4.3.5",
"egg-ci": "1",
"eslint": "4",
"eslint-config-egg": "5",
"ko-sleep": "*",
"formstream": "^1.1.0",
"koa": "2",
"mz-modules": "^2.1.0",
"pedding": "1",
"supertest": "3"
"supertest": "3",
"urllib": "^2.29.1"
},
"engines": {
"node": ">= 8.0.0"
},
"ci": {
"version": "8, 9"
"version": "8, 10"
},
"dependencies": {
"escape-html": "^1.0.3"
"escape-html": "^1.0.3",
"stream-wormhole": "^1.1.0"
}
}
32 changes: 16 additions & 16 deletions test/accepts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ describe('accepts.test.js', function() {
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);
.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);
.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) {
Expand All @@ -50,12 +50,12 @@ describe('accepts.test.js', function() {
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);
.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);
});
});

Expand Down
Loading

0 comments on commit d4291c2

Please sign in to comment.