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

set onError function will get error:'next() called multiple times' #112

Open
ilovefafa opened this issue Aug 31, 2018 · 5 comments
Open

Comments

@ilovefafa
Copy link

ilovefafa commented Aug 31, 2018

Promblem/Feature Request Summary

set onError function will get error:'next() called multiple times'

Environment Information

  • koa-body: "^2.5.0"
  • Koa: "^2.5.2"
  • Node.js: v8.11.1

Current Behavior

get follow error

 Error: next() called multiple times
application.js:190
      at dispatch (c:\Users\Administrator\Desktop\github-items\1-MyItems\ele-backend\node_modules\koa-router\node_modules\koa-compose\index.js:38:45)
      at next (c:\Users\Administrator\Desktop\github-items\1-MyItems\ele-backend\node_modules\koa-router\node_modules\koa-compose\index.js:45:18)
      at c:\Users\Administrator\Desktop\github-items\1-MyItems\ele-backend\node_modules\koa-body\index.js:98:14
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Steps to Reproduce

router.post(
        '/upLoadHeadPicture',
        koaBody({
            multipart: true,
            onError: (error, ctx) => {
                ctx.body = {
                    code: 400,
                    error
                };
            },
            formidable: {
                uploadDir: './storage/img/userHeadImg',
                maxFieldsSize: 1
            }
        }),
        async (ctx) => {
            ctx.body = {
                code: 200
            };
        }
    );

1.i am testing the maxFieldsSize,i upload a file which file size lager than 1kb.
2.when no onError function,koa app just thorw a excceed size error message.
3.when i set the onError function to catch error,i get the error above.

Expected Behavior

Respone excceed size error message to client,and koa app get no error

Possible Solution

Maybe koa-router conflict with koa-body? i can't find the solution.

Context (Environment)

@KleinMaximus
Copy link

I have the same problem. If I add onError option the console shows Error: next() called multiple times

@KleinMaximus
Copy link

koa-bodyparser solved my problem

@csvwolf
Copy link

csvwolf commented Oct 11, 2018

The same problem

@csvwolf
Copy link

csvwolf commented Oct 12, 2018

Solved. If you have an error handler, there will throw this error. Use you error handler for error handling instead.

@katanacrimson
Copy link
Contributor

katanacrimson commented Mar 7, 2019

It appears that where onError may be called, koa-body does not bail out after calling the onerror function and instead continues execution.

Of note:

https://github.com/dlau/koa-body/blob/8b0d77931ca2fdf031c0e37c34b372efc4d091a5/index.js#L96-L102

https://github.com/dlau/koa-body/blob/8b0d77931ca2fdf031c0e37c34b372efc4d091a5/index.js#L106-L113

This is likely a BC-breaker, but we should consider returning after calling the onError function (and NOT calling next() at all, as this is an error scenario)

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

No branches or pull requests

4 participants