add catch error with check for known error

This commit is contained in:
Lucy Cifferello
2022-11-15 12:32:08 -05:00
parent 8630000e8c
commit bfa30da1f6
2 changed files with 11 additions and 1 deletions

View File

@@ -1,11 +1,12 @@
import { Effects, ResultType } from "./types.ts";
import { error, errorCode, ok } from "./util.ts";
import { error, errorCode, isKnownError, ok } from "./util.ts";
export const checkWebUrl: (
url: string,
) => (effects: Effects, duration: number) => Promise<ResultType<null | void>> =
(url) => {
return async (effects, duration) => {
let errorValue;
// deno-lint-ignore no-cond-assign
if (errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 })) return errorValue;
return await effects.fetch(url)
@@ -35,3 +36,9 @@ export const guardDurationAboveMinimum = (
(input.duration <= input.minimumTime)
? errorCode(60, "Starting")
: null;
export const catchError = (effects: Effects) => (e: unknown) => {
if (isKnownError(e)) return e
effects.error(`Health check failed: ${e}`);
return { error: "Error while running health check" };
};

View File

@@ -21,3 +21,6 @@ export const errorCode = (code: number, error: string) => ({
});
export const error = (error: string) => ({ error });
export const ok = { result: null };
// deno-lint-ignore no-explicit-any
export const isKnownError = (e: any): e is T.KnownError => e.error || e["error-code"]