rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.98k stars 363 forks source link

Adding catch_panic anti-pattern #109

Open simonsan opened 3 years ago

simonsan commented 3 years ago

Due to inactivity of the author and maintainers not being able to edit the PR I'll move #22 to a new branch in this repository.

Future fixes to the PR will be made on this branch.

pickfire commented 3 years ago

Isn't web frameworks doing this? Is this really an anti-pattern?

MarcoIeni commented 3 years ago

Isn't web frameworks doing this? Is this really an anti-pattern?

In the "advantages" section it is mentioned

MarcoIeni commented 3 years ago

We can take a lot from the official documentation: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html

simonsan commented 3 years ago

@MarcoIeni Why is it expecting a + now for ul? :D

anti_patterns/catch_panic.md:76:1 MD004/ul-style Unordered list style [Expected: plus; Actual: dash]
anti_patterns/catch_panic.md:77:1 MD004/ul-style Unordered list style [Expected: plus; Actual: dash]
anti_patterns/catch_panic.md:78:1 MD004/ul-style Unordered list style [Expected: plus; Actual: dash]
anti_patterns/catch_panic.md:79:1 MD004/ul-style Unordered list style [Expected: plus; Actual: dash]
MarcoIeni commented 3 years ago

By default it expects the style to be consistent, see this

Since there is a + on line 66 it expects that all the lists follow that.

simonsan commented 3 years ago

By default it expects the style to be consistent, see this

Since there is a + on line 66 it expects that all the lists follow that.

ah ok, then it's nice!

just didn't see the plus and the error message was not so helpful as i'm used to from rustc :D

pickfire commented 3 years ago

We only see what's bad, is there something that could be done instead of this pattern, if there is maybe we should show it.

simonsan commented 3 years ago

We only see what's bad, is there something that could be done instead of this pattern, if there is maybe we should show it.

As this anti-pattern is focussed on "using a method that catches unexpected problems (implementation bugs) for handling an expected problem, such as a missing file.", Imho use good error handling is the answer to this? What would you say?

EDIT: So a link to the error handling chapter on the rust book could be sufficient? Not sure, but open for your ideas. :)

pickfire commented 3 years ago

I think that is an option but at least not so easy to pickup, but at least we have some suggestions. I am not aware of any straightforward switch.