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

How to catch error throw in the middleware? #22

Open
raphaelsoul opened this issue May 19, 2018 · 4 comments
Open

How to catch error throw in the middleware? #22

raphaelsoul opened this issue May 19, 2018 · 4 comments

Comments

@raphaelsoul
Copy link

Just set file size limit can server throws an error with 500 status code.

I need to catch Error:File too large` and throw 416 code.

router.post('/upload', upload.single('file'), async ctx => {
    // how to catch error here ?
})
@naughtyB
Copy link

naughtyB commented Jun 7, 2018

i also need to know how to catch error

@raphaelsoul
Copy link
Author

@naughtyB

router.post('/upload', async ctx => {
    // how to catch error here 
    try {
        await upload.single('file')
    } catch (e) {
        // this might works. I haven't tried.
    }
})

I am using busboy to handle uploaded file now.

@shenjo
Copy link

shenjo commented Jul 17, 2018

@raphaelsoul Obviously, this cannot work ,thus upload.single('file') is a function , and the function doesn't execute. Maybe you can await upload.single('file')(ctx,next) , (I have't try)

@alexdeia
Copy link

It works:

const upload = multer({ storage }).single('zip')

router.post('/upload', async (ctx, next) => {
  try {
    await upload(ctx, next)
  } catch (e) {
    console.log('error')
    ctx.throw(400, 'Error loading file')
  }
}, Ctrl.loadZip)

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