Skip to content

Commit

Permalink
fix: more graciously handle missing journey legs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chicken committed Nov 15, 2024
1 parent c336c2b commit ee7069d
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 167 deletions.
19 changes: 11 additions & 8 deletions src/components/LegendModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ export const LegendModal = ({
Voit klikata paikkaa nähdäksesi sen varauksen asemalta toiselle. Voit myös rajata
asemaväliä liikuttamalla liukusäätimen päätepisteitä.
</p>
<LegendItem bgColor={"#45475a"} text={"Paikka ei saatavilla"} />
<LegendItem bgColor={"#9399b2"} text={"Paikan vaunu ei kulje määränpäähän saakka"} />
<LegendItem bgColor={"#f38ba8"} text={"Paikka varattu valitulle matkalle"} />

<LegendItem
borderColor={"#820909"}
text={"Erikoispaikka (ekstra, ravintolavaunu, hytti, eläin)"}
/>
<LegendItem bgColor={"#a6e3a1"} text={"Paikka vapaa"} />
{heatmapEnabled ? (
<LegendItem
bgGradient
Expand All @@ -75,11 +78,11 @@ export const LegendModal = ({
) : (
<LegendItem bgColor={"#f9e2af"} text={"Paikka osittain varattu valitulle matkalle"} />
)}
<LegendItem bgColor={"#a6e3a1"} text={"Paikka vapaa"} />
<LegendItem
borderColor={"#820909"}
text={"Erikoispaikka (ekstra, ravintolavaunu, hytti, eläin)"}
/>

<LegendItem bgColor={"#f38ba8"} text={"Paikka varattu valitulle matkalle"} />
<LegendItem bgColor={"#9399b2"} text={"Paikan vaunu ei kulje määränpäähän saakka"} />
<LegendItem bgColor={"#45475a"} text={"Välin haussa tapahtui ongelmia (aikajana)"} />
<LegendItem bgColor={"#45475a"} text={"Paikka ei saatavilla (kartta)"} />
<p>
Vaihda keltaisesta väliväristä edistyneempään lämpökarttaan:&nbsp;
<Switch
Expand Down
54 changes: 40 additions & 14 deletions src/lib/vr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import crypto from "crypto";
import { z } from "zod";
import { env } from "~/lib/env";
import { getJSON, postJSON } from "~/lib/http";
import { error } from "~/lib/logger";
import { formatShortFinnishTime } from "./dateUtilities";

const loginResponseSchema = z.object({
Expand Down Expand Up @@ -130,7 +131,11 @@ const wagonResponseSchema = z.object({
z.string(),
z.object({
number: z.number(),
placeType: z.string().nullable().optional().transform((v) => v ?? null),
placeType: z
.string()
.nullable()
.optional()
.transform((v) => v ?? null),
type: z.string(),
floorCount: z.number(),
order: z.number(),
Expand Down Expand Up @@ -255,23 +260,44 @@ export async function getTrainOnDate(date: string, trainNumber: string) {
const dep = train.timeTableRows[i * 2]!;
const arr = train.timeTableRows[i * 2 + 1]!;
const trainNumber = train.trainNumber.toString();
const wagons = await getWagonMapData(
dep.stationShortCode,
arr.stationShortCode,
new Date(dep.scheduledTime),
trainNumber,
auth.sessionId,
auth.token
);
return {
dep,
arr,
wagons,
};
try {
const wagons = await getWagonMapData(
dep.stationShortCode,
arr.stationShortCode,
new Date(dep.scheduledTime),
trainNumber,
auth.sessionId,
auth.token
);
return {
dep,
arr,
wagons,
};
} catch (e) {
void error(
{
date,
train: trainNumber,
message: "Wagon map data fetching failed",
},
e
).catch(console.error);
return {
dep,
arr,
wagons: null,
};
}
})
),
};

const nullWagons = newTrain.timeTableRows.reduce((a, tt) => a + (tt.wagons == null ? 1 : 0), 0);
if (nullWagons == newTrain.timeTableRows.length || nullWagons > 3) {
throw new Error("Too many null wagons!");
}

return newTrain;
}

Expand Down
Loading

0 comments on commit ee7069d

Please sign in to comment.