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

Fixed incorrect example of ctx.onerror usage #1459

Merged
merged 1 commit into from May 16, 2020

Conversation

PaulAnnekov
Copy link
Contributor

https://koajs.com/#stream provides the following example on how to handle errors in koa handler:

const PassThrough = require('stream').PassThrough;

app.use(async ctx => {
  ctx.body = someHTTPStream.on('error', ctx.onerror).pipe(PassThrough());
});

but this example is buggy. Consider the following code:

const request = require('request');
const { PassThrough } = require('stream');
const Koa = require('koa');
const route = require('koa-route');

function getFile(ctx) {
    ctx.body = request
        .get('http://asdasd.com/')
        .on('error', ctx.onerror)
        .pipe(new PassThrough());
}

const init = () => {
    const app = new Koa();

    app.use(route.get(`/get`, getFile));

    app.listen(12345);
};

init();

If you will execute curl http://localhost:12345/get it will result in exception:

/home/node/app/node_modules/koa/lib/context.js:117
    this.app.emit('error', err, this);
             ^

TypeError: Cannot read property 'emit' of undefined
    at Request.onerror (/home/node/app/node_modules/koa/lib/context.js:117:14)
    at Request.emit (events.js:203:15)
    at Request.onRequestError (/home/node/app/node_modules/request/request.js:877:8)
    at ClientRequest.emit (events.js:198:13)
    at Socket.socketErrorListener (_http_client.js:392:9)
    at Socket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

That's because .on('error', ctx.onerror) will call onerror with different context, not ctx. That context doesn't have app property (

this.app.emit('error', err, this);
), which results in exception.

I have updated docs to preserve context.

@codecov
Copy link

codecov bot commented May 12, 2020

Codecov Report

Merging #1459 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1459   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files           4        4           
  Lines         484      484           
  Branches      130      130           
=======================================
  Hits          481      481           
  Partials        3        3           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d52105...6c856c3. Read the comment docs.

@dead-horse dead-horse merged commit f7c732f into koajs:master May 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants