mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
add catch error with check for known error
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import { Effects, ResultType } from "./types.ts";
|
import { Effects, ResultType } from "./types.ts";
|
||||||
import { error, errorCode, ok } from "./util.ts";
|
import { error, errorCode, isKnownError, ok } from "./util.ts";
|
||||||
export const checkWebUrl: (
|
export const checkWebUrl: (
|
||||||
url: string,
|
url: string,
|
||||||
) => (effects: Effects, duration: number) => Promise<ResultType<null | void>> =
|
) => (effects: Effects, duration: number) => Promise<ResultType<null | void>> =
|
||||||
(url) => {
|
(url) => {
|
||||||
return async (effects, duration) => {
|
return async (effects, duration) => {
|
||||||
let errorValue;
|
let errorValue;
|
||||||
|
// deno-lint-ignore no-cond-assign
|
||||||
if (errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 })) return errorValue;
|
if (errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 })) return errorValue;
|
||||||
|
|
||||||
return await effects.fetch(url)
|
return await effects.fetch(url)
|
||||||
@@ -35,3 +36,9 @@ export const guardDurationAboveMinimum = (
|
|||||||
(input.duration <= input.minimumTime)
|
(input.duration <= input.minimumTime)
|
||||||
? errorCode(60, "Starting")
|
? errorCode(60, "Starting")
|
||||||
: null;
|
: 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" };
|
||||||
|
};
|
||||||
|
|||||||
3
util.ts
3
util.ts
@@ -21,3 +21,6 @@ export const errorCode = (code: number, error: string) => ({
|
|||||||
});
|
});
|
||||||
export const error = (error: string) => ({ error });
|
export const error = (error: string) => ({ error });
|
||||||
export const ok = { result: null };
|
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"]
|
||||||
Reference in New Issue
Block a user