diff --git a/README.md b/README.md index ee37d06..d2fbb45 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,6 @@ View live at https://nicd.gitlab.io/sodexo-menu ## To build Install TypeScript so that you have `tsc` available. It's suggested to use the Node.js -version specified in `.tool-versions`. Then run `tsc tsconfig.json`. Serve with your +version specified in `.tool-versions`. Then run `tsc --build tsconfig.json`. Serve with your favourite HTTP server such as `python -m http.server 53593`. Accessing from `file://` URL will not work. diff --git a/src/sodexo-api.ts b/src/sodexo-api.ts index 1c31349..69a9bbe 100644 --- a/src/sodexo-api.ts +++ b/src/sodexo-api.ts @@ -15,15 +15,20 @@ export class SodexoAPIError extends Error { export async function getMenu(restaurantId: RestaurantId): Promise { const url = new URL(String(restaurantId), API_URL); - const resp = await fetch(url.toString()); - if (resp.ok) { - return await resp.json(); - } else { - console.error(resp); - throw new SodexoAPIError( - `Got invalid response: ${resp.status} ${resp.statusText}.`, - restaurantId - ); + try { + const resp = await fetch(url.toString()); + if (resp.ok) { + return await resp.json(); + } else { + console.error(resp); + throw new SodexoAPIError( + `Got invalid response: ${resp.status} ${resp.statusText}.`, + restaurantId + ); + } + } catch (e) { + console.error(e); + throw new SodexoAPIError(`Request error.`, restaurantId); } } diff --git a/src/view.ts b/src/view.ts index fd4d208..6ec4afb 100644 --- a/src/view.ts +++ b/src/view.ts @@ -1,7 +1,7 @@ import { MenuData, Course } from './types.js'; import { el } from './dom.js'; import { SodexoAPIError } from './sodexo-api.js'; -import { RESTAURANTS } from './config.js'; +import { RestaurantId } from './config.js'; function renderCourse(course: Course): HTMLLIElement { const li = el('li'); @@ -50,7 +50,7 @@ export function renderMenu(menu: MenuData): HTMLElement[] { export function renderError(data: SodexoAPIError): HTMLElement { const errorDiv = el('div', { classes: ['error'] }); errorDiv.appendChild( - el('h2', { text: `Unable to fetch menu for "${RESTAURANTS[data.restaurantId]}":` }) + el('h2', { text: `Unable to fetch menu for "${RestaurantId[data.restaurantId]}":` }) ); errorDiv.appendChild(el('p', { text: data.message })); errorDiv.appendChild(el('p', { classes: ['stack'], text: data.stack }));