Error handling

This commit is contained in:
Adam Blažek 2020-09-07 17:18:43 +02:00
parent efbcc70038
commit f7962f0e0f
1 changed files with 34 additions and 16 deletions

50
bk.nim
View File

@ -21,10 +21,24 @@ const
]
proc loadConfig(configFile = defaultConfigFile): Config =
defaultConfigFile.readFile.parseJson.to(Config)
try:
defaultConfigFile.readFile.parseJson.to(Config)
except IOError:
stderr.writeLine "Error: Can't read the config file. Are you signed in?"
quit QuitFailure
except JsonParsingError, JsonKindError:
stderr.writeLine "Error: Invalid config file. Make sure it's in JSON format."
quit QuitFailure
except KeyError:
stderr.writeLine "Error: The config file doesn't contain all necessary fields."
quit QuitFailure
proc saveConfig(configFile = defaultConfigFile, config: Config) =
configFile.writeFile((%*config).pretty)
try:
configFile.writeFile((%*config).pretty)
except IOError:
stderr.writeLine "Error: Can't write the config file."
quit QuitFailure
proc signin(
website: string,
@ -71,17 +85,21 @@ proc timetable(
line &= lesson.room.abbrev.align(4)
stdout.writeLine line
dispatchMulti(
[signin, help = {
"website": "the URL for Bakaláři (e.g. https://bakalari.myschool.cz)",
"username": "your username",
"password": "your password (will not be stored anywhere)",
"config-file": "where to store the credentials",
}],
[timetable, help = {
"config-file": "where the credentials are stored",
"date": "any date inside the week you want to display, in YYYYMMDD format (defaults to today)",
"one-day": "display only the specified day",
"permanent": "display the permanent timetable",
}],
)
try:
dispatchMulti(
[signin, help = {
"website": "the URL for Bakaláři (e.g. https://bakalari.myschool.cz)",
"username": "your username",
"password": "your password (will not be stored anywhere)",
"config-file": "where to store the credentials",
}],
[timetable, help = {
"config-file": "where the credentials are stored",
"date": "any date inside the week you want to display, in YYYYMMDD format (defaults to today)",
"one-day": "display only the specified day",
"permanent": "display the permanent timetable",
}],
)
except OSError:
stderr.writeLine "Error: Generic OS error. Check your internet connection."
quit QuitFailure