Merge pull request #11 from Start9Labs/update/catch-error

add catch error with check for known error
This commit is contained in:
J M
2022-11-15 10:40:36 -07:00
committed by GitHub
2 changed files with 28 additions and 14 deletions

View File

@@ -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<ResultType<null | void>> =
(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<ResultType<null | void>> => {
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<ResultType<null | void>> => {
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");
};

View File

@@ -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);