myheats

Live heats, scoring and leaderboard for sport events
git clone https://git.in0rdr.ch/myheats.git
Log | Files | Refs | Pull requests | README | LICENSE

commit d53fb37a68cbb63a0e689be291a0b066fb47f924
parent 631550d0cec9e0763191b44be611dd5861c60c95
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Sat, 28 Sep 2024 10:10:24 +0200

feat(api): improve error handling

Diffstat:
Msrc/api/db.cjs | 46+++++++++++++++++++++++-----------------------
Msrc/api/server.cjs | 8++++----
2 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/api/db.cjs b/src/api/db.cjs @@ -1,4 +1,4 @@ -const pg = require('postgres'); +const postgres = require('postgres'); require('dotenv').config({ // Configure common config files and modes @@ -13,9 +13,9 @@ require('dotenv').config({ ] }); -// will use psql environment variables +// Use psql environment variables // https://github.com/porsager/postgres?tab=readme-ov-file#environmental-variables -const sql = pg({ +sql = postgres({ publications: [ 'myheats_realtime', ] @@ -44,7 +44,7 @@ async function invalidateToken(token) { ` return users } catch (error) { - console.log('Error occurred in invalidateToken:', error); + console.error('Error occurred in invalidateToken:', error); throw error } } @@ -57,7 +57,7 @@ async function lookupToken(token) { ` return users } catch (error) { - console.log('Error occurred in lookupToken:', error); + console.error('Error occurred in lookupToken:', error); throw error } } @@ -72,7 +72,7 @@ async function saveToken(id, token, exp) { ` return users } catch (error) { - console.log('Error occurred in saveToken:', error); + console.error('Error occurred in saveToken:', error); throw error } } @@ -93,7 +93,7 @@ async function getUser(email) { ` return users } catch (error) { - console.log('Error occurred in getUser:', error); + console.error('Error occurred in getUser:', error); throw error } } @@ -105,7 +105,7 @@ async function allHeats() { ` return heats } catch (error) { - console.log('Error occurred in allHeats:', error); + console.error('Error occurred in allHeats:', error); throw error } } @@ -118,7 +118,7 @@ async function getHeat(heatId) { ` return heat } catch (error) { - console.log('Error occurred in getHeat:', error); + console.error('Error occurred in getHeat:', error); throw error } } @@ -130,7 +130,7 @@ async function allAthletes() { ` return athletes } catch (error) { - console.log('Error occurred in allHeats:', error); + console.error('Error occurred in allHeats:', error); throw error } } @@ -150,7 +150,7 @@ async function addAthleteToHeat(athlete, heat) { ` return startlist } catch (error) { - console.log('Error occurred in addAthleteToHeat:', error); + console.error('Error occurred in addAthleteToHeat:', error); throw error } } @@ -176,7 +176,7 @@ async function addAthlete(nr, firstname, lastname, birthday, school) { ` return athlete } catch (error) { - console.log('Error occurred in addAthlete:', error); + console.error('Error occurred in addAthlete:', error); throw error } } @@ -189,7 +189,7 @@ async function removeAthlete(id) { ` return athlete } catch (error) { - console.log('Error occurred in removeAthlete:', error); + console.error('Error occurred in removeAthlete:', error); throw error } } @@ -203,7 +203,7 @@ async function removeAthleteFromHeat(startlistId) { ` return startlist } catch (error) { - console.log('Error occurred in removeAthleteFromHeat:', error); + console.error('Error occurred in removeAthleteFromHeat:', error); throw error } } @@ -225,7 +225,7 @@ async function newHeat(name, heatLocation, plannedStart) { ` return heat } catch (error) { - console.log('Error occurred in newHeat:', error); + console.error('Error occurred in newHeat:', error); throw error } } @@ -238,7 +238,7 @@ async function removeHeat(heatId) { ` return heat } catch (error) { - console.log('Error occurred in removeHeat:', error); + console.error('Error occurred in removeHeat:', error); throw error } } @@ -258,7 +258,7 @@ async function distinctStartlist(heatIds) { ` return startlist } catch (error) { - console.log('Error occurred in distinctStartlist:', error); + console.error('Error occurred in distinctStartlist:', error); throw error } } @@ -285,7 +285,7 @@ async function startlistWithAthletes(heatId) { ` return startlist } catch (error) { - console.log('Error occurred in startlistWithAthletes:', error); + console.error('Error occurred in startlistWithAthletes:', error); throw error } } @@ -302,7 +302,7 @@ async function scoresForHeatAndAthlete(heat, athlete) { ` return score } catch (error) { - console.log('Error occurred in scoresForHeatAndAthlete:', error); + console.error('Error occurred in scoresForHeatAndAthlete:', error); throw error } } @@ -315,7 +315,7 @@ async function scoreSummaryForHeatAndAthlete(heat, athlete) { ` return summary } catch (error) { - console.log('Error occurred in scoreSummaryForHeatAndAthlete:', error); + console.error('Error occurred in scoreSummaryForHeatAndAthlete:', error); throw error } } @@ -328,7 +328,7 @@ async function getScore(heat, athlete, judge) { ` return scores } catch (error) { - console.log('Error occurred in getScore:', error); + console.error('Error occurred in getScore:', error); throw error } } @@ -347,7 +347,7 @@ async function setScore(heat, athlete, judge, score) { ` return scores } catch (error) { - console.log('Error occurred in setScore:', error); + console.error('Error occurred in setScore:', error); throw error } } @@ -372,7 +372,7 @@ async function watchScores(clients) { ) return unsubscribe } catch (error) { - console.log('Error occurred in watchScores:', error); + console.error('Error occurred in watchScores:', error); throw error } } diff --git a/src/api/server.cjs b/src/api/server.cjs @@ -632,7 +632,7 @@ server.on('request', async (req, res) => { }); function notFound(res, path) { - console.log('x Error: 404 not found'); + console.error('x Error: 404 not found'); res.statusCode = 404; res.end(JSON.stringify({ message: 'no tea cup 🍵 use fingaa 👇 haaiiyaa!', @@ -640,7 +640,7 @@ function notFound(res, path) { })); } function notAllowed(res, method) { - console.log('x Error: 403 method', method, 'not allowed'); + console.error('x Error: 403 method', method, 'not allowed'); res.statusCode = 403; res.end(JSON.stringify({ message: 'why english tea 🫖 cup to measure rice? Use fingaa 👇 haaiiyaa!', @@ -648,7 +648,7 @@ function notAllowed(res, method) { })); } function serverError(res, err) { - console.log('x Error:', err); + console.error('x Error:', err); res.statusCode = 500; res.end(JSON.stringify({ message: 'no colander, haaiiyaa, rice 🍚 wet you fucked up!', @@ -675,7 +675,7 @@ wss1.on('connection', function connection(sock) { db.watchScores(clients) } } catch (error) { - console.log('x Error: %s', error.message); + console.error('x Error: %s', error.message); } });