From 3b35ec4e38bfc792b74a8bc0bba9be2a9f833b5b Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Tue, 7 Nov 2023 18:08:05 -0500 Subject: [PATCH] Rewrite safeResult in terms of atmp --- src/constructor.ts | 45 ++------------------------------------------- 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/src/constructor.ts b/src/constructor.ts index d2efd75a..a54d9068 100644 --- a/src/constructor.ts +++ b/src/constructor.ts @@ -41,49 +41,8 @@ function formatSchemaErrors(errors: ParserIssue[]): SchemaError[] { * console.log(result.errors[0].message) * } */ -async function safeResult(fn: () => T): Promise> { - try { - return { - success: true, - data: await fn(), - errors: [], - inputErrors: [], - environmentErrors: [], - } - } catch (error) { - if (error instanceof InputError) { - return { - success: false, - errors: [], - environmentErrors: [], - inputErrors: [schemaError(error.message, error.path)], - } - } - if (error instanceof EnvironmentError) { - return { - success: false, - errors: [], - environmentErrors: [schemaError(error.message, error.path)], - inputErrors: [], - } - } - if (error instanceof InputErrors) { - return { - success: false, - errors: [], - environmentErrors: [], - inputErrors: error.errors.map((e) => schemaError(e.message, e.path)), - } - } - if (error instanceof ResultError) return error.result - - return { - success: false, - errors: [toErrorWithMessage(error)], - inputErrors: [], - environmentErrors: [], - } - } +function safeResult(fn: () => T): Promise> { + return dfResultFromAtmp(atmp(fn))() as Promise> } function dfResultFromAtmp(fn: T) {