Improve error handling, update README instructions to correct
This commit is contained in:
parent
1ed1913197
commit
26c3f8997b
3 changed files with 17 additions and 12 deletions
|
@ -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.
|
||||
|
|
|
@ -15,6 +15,7 @@ export class SodexoAPIError extends Error {
|
|||
export async function getMenu(restaurantId: RestaurantId): Promise<ServerBlob> {
|
||||
const url = new URL(String(restaurantId), API_URL);
|
||||
|
||||
try {
|
||||
const resp = await fetch(url.toString());
|
||||
if (resp.ok) {
|
||||
return await resp.json();
|
||||
|
@ -25,6 +26,10 @@ export async function getMenu(restaurantId: RestaurantId): Promise<ServerBlob> {
|
|||
restaurantId
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new SodexoAPIError(`Request error.`, restaurantId);
|
||||
}
|
||||
}
|
||||
|
||||
export type MenuResult = ServerBlob | SodexoAPIError;
|
||||
|
|
|
@ -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 }));
|
||||
|
|
Reference in a new issue