From bfa30da1f636ed1df181f37694799148cda7932e Mon Sep 17 00:00:00 2001 From: Lucy Cifferello <12953208+elvece@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:32:08 -0500 Subject: [PATCH 1/4] add catch error with check for known error --- healthUtil.ts | 9 ++++++++- util.ts | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 From a3f47810ee6304df3a934d603fd8935a8e26cb47 Mon Sep 17 00:00:00 2001 From: Lucy C <12953208+elvece@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:36:59 -0500 Subject: [PATCH 2/4] Update util.ts Co-authored-by: J M <2364004+Blu-J@users.noreply.github.com> --- util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.ts b/util.ts index fe979bd..5ffc571 100644 --- a/util.ts +++ b/util.ts @@ -23,4 +23,4 @@ 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 +export const isKnownError = (e: unknown): e is T.KnownError => e instanceof Object && ('error' in e || "error-code" in e) \ No newline at end of file From 7e57d0fb87c7d9c2a061d671d193068213be44cd Mon Sep 17 00:00:00 2001 From: Lucy C <12953208+elvece@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:37:06 -0500 Subject: [PATCH 3/4] Update healthUtil.ts Co-authored-by: J M <2364004+Blu-J@users.noreply.github.com> --- healthUtil.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/healthUtil.ts b/healthUtil.ts index 559bcbd..e3cbeac 100644 --- a/healthUtil.ts +++ b/healthUtil.ts @@ -40,5 +40,5 @@ export const guardDurationAboveMinimum = ( 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" }; + return error( "Error while running health check" ); }; From c465664a07726aecd678f83b428a0f723136a08e Mon Sep 17 00:00:00 2001 From: Lucy Cifferello <12953208+elvece@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:39:04 -0500 Subject: [PATCH 4/4] formatting --- healthUtil.ts | 36 ++++++++++++++++++++---------------- util.ts | 4 ++-- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/healthUtil.ts b/healthUtil.ts index e3cbeac..0c7fed1 100644 --- a/healthUtil.ts +++ b/healthUtil.ts @@ -6,8 +6,10 @@ export const checkWebUrl: ( (url) => { return async (effects, duration) => { let errorValue; - // deno-lint-ignore no-cond-assign - 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) @@ -20,25 +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 + if (isKnownError(e)) return e; effects.error(`Health check failed: ${e}`); - return error( "Error while running health check" ); + return error("Error while running health check"); }; diff --git a/util.ts b/util.ts index 5ffc571..6313d11 100644 --- a/util.ts +++ b/util.ts @@ -22,5 +22,5 @@ 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: unknown): e is T.KnownError => e instanceof Object && ('error' in e || "error-code" in e) \ No newline at end of file +export const isKnownError = (e: unknown): e is T.KnownError => + e instanceof Object && ("error" in e || "error-code" in e);