lukeed / polka

A micro web server so fast, it'll make you dance! :dancers:
MIT License
5.42k stars 174 forks source link

Polka always running onError handler #68

Closed ansarizafar closed 6 years ago

ansarizafar commented 6 years ago

I am using polka v0..5.0 and here is my code

import polka from 'polka';

const httpserver = polka({
        onError: (e) => {
          console.log('Error handling request');
          console.log(e);
           }
      });
 httpserver.get('/', (req, res) => {
      console.log('Request received');
      res.end('Hello World!');
    })
 httpserver.listen(3000, err => {
   if(err) {
    console.log(err);
   }
  })

When I access the route '/', I can see "Request received" in terminal and "Hello world" in browser but Polka is also running onError handler each time I access '/' route with error 404. Why polka is running onError handler even there is no un-handled exception in route handler.

lukeed commented 6 years ago

Hey, how's it going?

Thanks for the snippet – I ran it exactly as you've presented & I do not see the onError logging anywhere. I'd look elsewhere in your app to see what may be throwing.

screen shot 2018-09-19 at 2 41 18 am
ansarizafar commented 6 years ago

To isolate the issue, I am using a single index.js file. Previously I was using backpack to run that file now I am just using node src/index.js here is the result (Nodejs ver 10.7.0) image

ghost commented 6 years ago

Are you sure you have polka 0.5.0? maybe specific commit from git?

Can you also tell what is the message of error

ansarizafar commented 6 years ago

Here is my package.json

{
  "name": "webeotest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "backpack",
    "build": "backpack build"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "backpack-core": "^0.7.0"
  },
  "dependencies": {
    "polka": "^0.5.0"
  }
}

The message of error is {code: 404}

lukeed commented 6 years ago

Oh, I bet I know what it is!

If you log your req.path in the error handler, I'm pretty positive it'll be favicon.ico. This is your browser requesting it on every request.

The onNoMatch is bound to your onError (unless you provide one specifically). It wouldn't be running for ever GET / request because that would throw a 500 error as it tried to res.end the same response twice.

If you can log that path, that'd be great 🙌

ansarizafar commented 6 years ago

You are absolutely right console.log(req.path) prints /favicon.ico in terminal on each request.Don't know why browser is requesting this file on each request.

lukeed commented 6 years ago

Great! Because it wants to have an icon to display for your tab 😜

Closing as this is expected since you don't have a favicon route defined.

zolotokrylin commented 4 years ago

Hi @lukeed , I am trying to work with onError handler, but it never being triggered. Here is an example of my snippet.

const app = polka(onError: function(){
  console.log("test")
  }) // You can also use Expres
    .use(
        function(req,res,next){new Error() res.end(); }
        }))
    .listen(PORT, err => {
        if (err) console.log('error', err);
    });

export default app;

Thank you in advance!

lukeed commented 4 years ago

Hi @zolotokrylin,

You're creating an Error object but you never throw it. It's like new Cat but never assigning it to a variable.

You should check out these docs for info

zolotokrylin commented 4 years ago

Thank you, Luke.