Skip to content

Commit

Permalink
Rewrite safeResult in terms of atmp
Browse files Browse the repository at this point in the history
  • Loading branch information
diogob committed Nov 7, 2023
1 parent 2682de7 commit 3b35ec4
Showing 1 changed file with 2 additions and 43 deletions.
45 changes: 2 additions & 43 deletions src/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,8 @@ function formatSchemaErrors(errors: ParserIssue[]): SchemaError[] {
* console.log(result.errors[0].message)
* }
*/
async function safeResult<T>(fn: () => T): Promise<Result<T>> {
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<T>(fn: () => T): Promise<Result<T>> {
return dfResultFromAtmp(atmp(fn))() as Promise<Result<T>>
}

function dfResultFromAtmp<T extends Attempt, R>(fn: T) {
Expand Down

0 comments on commit 3b35ec4

Please sign in to comment.