diff --git a/healthUtil.ts b/healthUtil.ts index 5481387..559bcbd 100644 --- a/healthUtil.ts +++ b/healthUtil.ts @@ -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> = (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" }; +}; diff --git a/util.ts b/util.ts index 24f6ac0..fe979bd 100644 --- a/util.ts +++ b/util.ts @@ -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"] \ No newline at end of file