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

Add status fallback to error handling #164

Merged
merged 3 commits into from Mar 6, 2017
Merged

Conversation

dotcypress
Copy link
Contributor

@dotcypress dotcypress commented Feb 26, 2017

Add status fallback to error handling.
This change makes some useful libraries(http-assert) compatible with micro 🚀

Add `status` fallback to error handling.
This change make some useful libraries(http-assert) compatible with `micro`
@timneutkens
Copy link
Member

Alternative approach:

function wrap (fn) {
  return async (req, res) => {
    try {
      const value = await fn(req, res)

      return value
    } catch (err) {
      if(err.status) {
        err.statusCode = err.status
      }

      throw err
    }
  }
}

const assert = require('http-assert')

module.exports = wrap(async () => {
  const test = 'abc'
  assert(test === 'abc', 404, 'Not found')

  return 'Ok'
})

@leo leo merged commit 9b33adb into vercel:master Mar 6, 2017
@leo
Copy link
Contributor

leo commented Mar 6, 2017

👍 Thanks!

floatdrop added a commit to floatdrop/micro that referenced this pull request Mar 14, 2017
@dotcypress dotcypress deleted the patch-1 branch March 14, 2017 22:11
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

3 participants