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

enable global error-handler again + some error-logging improvements #462

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion express.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,12 @@ setInterval(() => {
app.use(function (req, res) {
res.status(404).render('errors/404');
});
app.use(function (req, res) {
app.use(function (err, req, res, next) {
console.error('[error] Incoming request to"', req.originalUrl, '"failed with error "', err.toString(), '"')
if (res.headersSent) {
return next(err);
}
Comment on lines +327 to +329
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an issue if the error is thrown after the server already sent the http-header, in this case it's better to let express handle the error, see https://expressjs.com/en/guide/error-handling.html
There are some pending issues with the general approach.. how to handle errors if the caller expects json, js etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you missed it, this function was never called (thus no error handling on production) bc. of express expecting 4 parameters:
Define error-handling middleware functions in the same way as other middleware functions, except error-handling functions have four arguments instead of three: (err, req, res, next)


res.status(500).render('errors/500');
});

14 changes: 7 additions & 7 deletions scripts/extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function getTournamentNews() {
let newshubData = sortedData.filter(article => article.category[1] !== 284);
return await newshubData;
} catch (e) {
console.log(e);
console.error(currentDate, '- [error] extractor::getTournamentNews failed with =>', e.toString());
return null;
}
}
Expand All @@ -52,7 +52,7 @@ async function flashMessage() {
}));
return await data;
} catch (e) {
console.log(e);
console.error(currentDate, '- [error] extractor::flashMessage failed with =>', e.toString());
return null;
}
}
Expand All @@ -72,7 +72,7 @@ async function news() {
}));
return await data;
} catch (e) {
console.log(e);
console.error(currentDate, '- [error] extractor::news failed with =>', e.toString());
return null;
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ async function newshub() {
let data = sortedData.filter(onlyActiveArticles);
return await data;
} catch (e) {
console.log(e);
console.error(currentDate, '- [error] extractor::newshub failed with =>', e.toString());
return null;
}
}
Expand All @@ -119,7 +119,7 @@ async function fafTeams() {
}));
return await data;
} catch (e) {
console.log(e);
console.error(currentDate, '- [error] extractor::fafTeams failed with =>', e.toString());
return null;
}

Expand All @@ -135,7 +135,7 @@ async function contentCreators() {
}));
return await data;
} catch (e) {
console.log(e);
console.error(currentDate, '- [error] extractor::contentCreators failed with =>', e.toString());
return null;
}

Expand Down Expand Up @@ -222,7 +222,7 @@ module.exports.run = function run() {
.then(data => {
fs.writeFile(`public/js/app/members/${fileName}.json`, JSON.stringify(data), error => {
if (error) {
console.log(error);
console.error(currentDate, '- [error] extractor::run', fileName, 'failed with =>', error.toString());
} else {
console.log(`${currentDate} - ${fileName} file created.`);
}
Expand Down