27 lines
669 B
TypeScript
27 lines
669 B
TypeScript
import { error } from '@sveltejs/kit';
|
|
import { APIError } from './api/request';
|
|
|
|
/// Maps an error from the backend to a SvelteKit error.
|
|
export function mapBackendError(e: unknown) {
|
|
if (e instanceof APIError) {
|
|
switch (e.response.status) {
|
|
case 404:
|
|
return error(404, 'Not Found');
|
|
default:
|
|
// TODO handle better
|
|
return error(500, e.response.statusText);
|
|
}
|
|
}
|
|
|
|
return error(502, 'Platform is down');
|
|
}
|
|
|
|
/// Ensures that a promise resolves to a value, or throws the appropriate error otherwise.
|
|
export async function must<T>(promise: Promise<T>): Promise<T> {
|
|
try {
|
|
return await promise;
|
|
} catch (err) {
|
|
throw mapBackendError(err);
|
|
}
|
|
}
|