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

Allows micro cli to use custom onError. #99

Merged
merged 1 commit into from Oct 7, 2016
Merged

Allows micro cli to use custom onError. #99

merged 1 commit into from Oct 7, 2016

Conversation

rickharrison
Copy link

@rickharrison rickharrison commented Oct 1, 2016

The micro cli now will read in exports.onError and pass it to serve if it exists. Here is an example:

module.exports = exports = async function (request, response) {
  throw createError(405, 'Method Not Allowed');
};

exports.onError = function (req, res, { statusCode, message, stack }) {
  send(res, statusCode, {
    statusCode,
    message
  });
}

Please let me know if this is something you would be open to merging. If so, I will add tests and documentation. Thanks!

@leo leo merged commit 61c0a50 into vercel:master Oct 7, 2016
@leo
Copy link
Contributor

leo commented Oct 7, 2016

Thanks!

@rickharrison
Copy link
Author

Oops, I didn't write the tests yet. I'll write the tests this weekend and submit another PR.

@rauchg
Copy link
Member

rauchg commented Oct 8, 2016

There's a more elegant solution for this: just set up your own try/catch at the top level.

const withErrors = require('my-app-on-error')
module.exports = withErrors((req, res) => {
  throw new Error('voila!')
})

In fact, @nkzawa has brought up before that onError is a bit of a stink, and I'm all for deprecating it in the future. Fewer features.

@leo
Copy link
Contributor

leo commented Oct 8, 2016

@rauchg Got it! Let's remove it...

@leo leo mentioned this pull request Oct 8, 2016
@rickharrison rickharrison deleted the export-error branch October 8, 2016 22:15
@rickharrison
Copy link
Author

Cool, that sounds good. I just published micro-json-error as a small package for my use case using the method you described: https://github.com/rickharrison/micro-json-error

@rauchg
Copy link
Member

rauchg commented Oct 9, 2016

@rickharrison that's really cool!

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