question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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'))

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thepabloaguilarcommented, Jan 5, 2022

I was thinking about the implementation here, one thing we need to define is what to do with functions that has multiple parameters, like, if my function has 3 arguments like:

def my_func(a, b, *, c):
    pass

my_func(1, 2, c=3)

What should be inside the Result? A tuple where the first element is the positional args and then the second the keyword args?

0reactions
sobolevncommented, Jan 6, 2022

I think that functions with multiple arguments should not be allowed in this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Covariance and contravariance (computer science) - Wikipedia
covariant if it preserves the ordering of types (≤), which orders types from more specific to more generic: If A ≤ B ,...
Read more >
when other than exception, and raise application error
A when others is almost always a BUG unless it is immediately followed by a RAISE. The point of an exception block is...
Read more >
Reraise (same exception) after catching an exception in Ruby
It is often the case that the one responsible for handling errors is user of the object: the caller. What if we are...
Read more >
Logical Fallacies Handlist - Cn
Logical Fallacies Handlist: Fallacies are statements that might sound reasonable or superficially true but are actually flawed or dishonest.
Read more >
Covariance and Contravariance (C#) - Microsoft Learn
Covariance preserves assignment compatibility and contravariance reverses it. ... The following statement produces a run-time exception.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found