Improve error handling, update README instructions to correct

This commit is contained in:
Mikko Ahlroth 2021-08-16 09:24:02 +03:00
parent 1ed1913197
commit 26c3f8997b
3 changed files with 17 additions and 12 deletions

View file

@ -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.

View file

@ -15,15 +15,20 @@ export class SodexoAPIError extends Error {
export async function getMenu(restaurantId: RestaurantId): Promise<ServerBlob> {
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);
}
}

View file

@ -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 }));