Skip to content

Commit

Permalink
Fixed bug and improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
w-henderson committed Nov 30, 2020
1 parent 5098dac commit e4c67d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ proc loadDepartures(crs: string): Station =

if len(stops) > 1:
for i in 0..len(callingPointsObject)-1:
stops[i] = CallingPoint(
name: callingPointsObject[i].attr("Name"),
crs: callingPointsObject[i].attr("crs"),
scheduledDeparture: parseTime(callingPointsObject[i].attr("ttdep")),
expectedDeparture: parseTime(callingPointsObject[i].attr("etdep"))
)
try:
stops[i] = CallingPoint(
name: callingPointsObject[i].attr("Name"),
crs: callingPointsObject[i].attr("crs"),
scheduledDeparture: parseTime(callingPointsObject[i].attr("ttdep")),
expectedDeparture: parseTime(callingPointsObject[i].attr("etdep"))
)
except:
stops[i] = CallingPoint(
name: callingPointsObject[i].attr("Name"),
crs: callingPointsObject[i].attr("crs"),
scheduledDeparture: parseTime(callingPointsObject[i].attr("ttdep")),
expectedDeparture: parseTime(callingPointsObject[i].attr("ttdep"))
)
try:
stops[len(stops) - 1] = CallingPoint(
name: serviceXML.findAll("Destination1")[0].attr("name"),
Expand Down
2 changes: 1 addition & 1 deletion src/nimtrains.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ if paramCount() == 1:
try:
let services = loadDepartures(tryToParseName(paramStr(1)))
renderStation(services)
except:
except StationNotFound:
coloredWrite("[ERROR]: Can't find the specified station! If it's a full name instead of a CRS code, make sure it's in quote marks.\n", fgRed)

# If given the wrong number of arguments throw an error
Expand Down
4 changes: 3 additions & 1 deletion src/stations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3400,11 +3400,13 @@ var stationsRaw = """{
import json
import strutils

type StationNotFound = object of ValueError

# Parse the JSON stations data
let stations: JsonNode = parseJson(stationsRaw)

proc nameToCrs(name: string): string =
for stat in stations.keys:
if stations[stat]["name"].getStr().toLower() == name.toLower():
return stat
raise newException(Exception, "Target doesn't exist.")
raise newException(StationNotFound, "Station not found.")

0 comments on commit e4c67d2

Please sign in to comment.