Skip to content

Variant of safe that preserves the argument that lead to an exception #264

Closed
@stereobutter

Description

@stereobutter

I propose a function/decorator (maybe attempt would be a good name?) similar to safe with the twist of returning Failure(bad_input) from safe(f)(bad_input) instead of Failure(SomeException). An example where this would be useful:

(
    attempt(json.loads)('<hello world>')
    .alt(lambda s: ValueError(f'"{s}" is not a valid json')) 
)
# Failure(ValueError('"<hello world>" is not a valid json'))

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions