diff --git a/healthUtil.ts b/healthUtil.ts index 5481387..0c7fed1 100644 --- a/healthUtil.ts +++ b/healthUtil.ts @@ -1,12 +1,15 @@ 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; - if (errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 })) return errorValue; + if ( + // deno-lint-ignore no-cond-assign + errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 }) + ) return errorValue; return await effects.fetch(url) .then((_) => ok) @@ -19,19 +22,27 @@ export const checkWebUrl: ( }; }; -export const runHealthScript = ({command, args}: { command: string, args: string[] }) => async (effects: Effects, _duration: number): Promise> => { - const res = await effects.runCommand({ command, args }) - if ('result' in res){ - return { result: null } - } else { - return res - } -} +export const runHealthScript = + ({ command, args }: { command: string; args: string[] }) => + async ( + effects: Effects, + _duration: number, + ): Promise> => { + const res = await effects.runCommand({ command, args }); + if ("result" in res) { + return { result: null }; + } else { + return res; + } + }; // Ensure the starting duration is pass a minimum export const guardDurationAboveMinimum = ( input: { duration: number; minimumTime: number }, -) => - (input.duration <= input.minimumTime) - ? errorCode(60, "Starting") - : null; +) => (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..6313d11 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 }; + +export const isKnownError = (e: unknown): e is T.KnownError => + e instanceof Object && ("error" in e || "error-code" in e);