feat: handle template build error

This commit is contained in:
2024-12-02 13:45:49 +00:00
parent 8a8582e06e
commit dc97c2c498
9 changed files with 123 additions and 11 deletions

View File

@@ -1,5 +1,12 @@
import { promiseOrThrow } from "./lib/errors";
interface ApiErrorResponse {
code: string;
error: string;
}
const API_ERROR_BAD_TEMPLATE = "BAD_TEMPLATE";
enum ApiError {
NotFound = "NOT_FOUND",
BadRequest = "BAD_REQUEST",
@@ -16,10 +23,9 @@ async function fetchApi(
() => ApiError.Network,
);
if (res.status !== 200) {
console.log(res.status);
switch (res.status) {
case 401:
throw ApiError.BadRequest;
case 400:
throw await res.json();
case 404:
throw ApiError.NotFound;
default:
@@ -29,4 +35,15 @@ async function fetchApi(
return res;
}
export { ApiError, fetchApi };
function isApiErrorResponse(error: unknown): error is ApiErrorResponse {
return (
error !== null &&
error !== undefined &&
typeof error === "object" &&
"code" in error &&
"error" in error
);
}
export { API_ERROR_BAD_TEMPLATE, ApiError, fetchApi, isApiErrorResponse };
export type { ApiErrorResponse };