feat: handle template build error
This commit is contained in:
@@ -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 };
|
||||
|
Reference in New Issue
Block a user