Closed ansarizafar closed 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.
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)
Are you sure you have polka 0.5.0? maybe specific commit from git?
Can you also tell what is the message of error
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}
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 🙌
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.
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.
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!
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
Thank you, Luke.
I am using polka v0..5.0 and here is my code
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.