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 "No Match For" error? #33

Open
today- opened this issue Apr 26, 2017 · 1 comment
Open

How to catch "No Match For" error? #33

today- opened this issue Apr 26, 2017 · 1 comment

Comments

@today-
Copy link

today- commented Apr 26, 2017

Hock server now fails on unmatched urls by default. How can I set my own behaviour for that?

I know about "throwOnUnmatched" option, but it makes server to respond with "500" status, and it's not what I want.

I've tried server.on("error", and try/catch, but it's not worked

@jseminck
Copy link

jseminck commented May 31, 2017

Note: I do not have anything to do with this project, I'm just a user.

Anyway judging by the code (see below) I am not sure if there is much that you can do. The code just logs the error using console.warn() and then returns with 500 status. I would have expected try/catch to work actually... should try it out!

I think that if you want some custom behaviour you will have to either make a PR or a fork. The last commit was in August 2016 so I am not sure if any PRs will get merged, but let's see. ;)

    if (matchIndex === null) {
      if (self._throwOnUnmatched) {
        throw new Error('No Match For: ' + req.method + ' ' + req.url);
      }

      console.error('No Match For: ' + req.method + ' ' + req.url);
      if (req.method === 'PUT' || req.method === 'PATCH' || req.method === 'POST') {
        console.error(req.body);
      }
      res.writeHead(500, { 'Content-Type': 'text/plain' });
      res.end('No Matching Response!\n');
    }
    else {
      if (self._assertions[matchIndex].sendResponse(res)) {
        self._assertions.splice(matchIndex, 1)[0];
      }
    }

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

2 participants