koa-body icon indicating copy to clipboard operation
koa-body copied to clipboard

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

Open ilovefafa opened this issue 6 years ago • 5 comments

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)

ilovefafa avatar Aug 31 '18 08:08 ilovefafa

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

KleinMaximus avatar Sep 07 '18 09:09 KleinMaximus

koa-bodyparser solved my problem

KleinMaximus avatar Sep 07 '18 09:09 KleinMaximus

The same problem

csvwolf avatar Oct 11 '18 09:10 csvwolf

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

csvwolf avatar Oct 12 '18 09:10 csvwolf

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)

katanacrimson avatar Mar 07 '19 17:03 katanacrimson