Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"try" semantics #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

"try" semantics #2

wants to merge 6 commits into from

Conversation

warpfork
Copy link

@warpfork warpfork commented May 7, 2015

Hello! Spacemonkey errors are awesome! Using them in my projects has resulted in a huge reduction in boilerplate and grouping things into hierarchies has made my errors and handling much cleaner!

This is a package that attempts to take boilerplate reduction a step further, and replace frequent three-liner if err := whatever(); err != nil { return err } error handling blocks and replace them with... well, zero liners, by using panics instead. To make this feel cozy, this try package makes semantics that feel like try/catch semantics in other languages, and makes it easy to use spacemonkey error type hierarchies to trigger different handling paths. The example test functions in the PR should be pretty good demonstrations of how the syntax works.

Panics aren't for everybody (or every situation), I know. This would represent a totally optional way to use spacemonkey errors, and so is isolated off in a sub-package. If a project is happy on the path of error returns, they'll have no reason to ever import the try package. And indeed, return-style errors are still often a good idea in many situations ('specially around concurrent work, where they're effectively required for channel/cross-goroutine reporting, even if the majority of the program is using panics and try within individual goroutines) -- so there's some package docs that attempt to highlight that, and it's purposefully easy to use the CatchAll system to flip panics back into return-style errors. Another school of thought has panics as being valid within packages, but impolite to expose across package boundaries (stdlib encoding libraries are known for doing this internally), and try should be totally easy to use like that, too.

I've been incubating this in usage in some projects for a while now (as you can see from the commit timestamps...) and it's been working pretty well, so I thought I'd share and see if anyone thinks this is an interesting enough pattern to upstream :D

warpfork and others added 4 commits March 1, 2015 03:43
This makes it possible to use panics for flow control without redundant error checking (or the chance to forget to check returned errors), and collaborates with the error type system to all quick and easy filtering of errors.
There's no good reason to, since you can never meaningfully interact with it other than through the already exposed chainable functions, but exporting it makes godoc do a significantly better job on the package.
…object, even when handling non-error types such as int and string.
)

func TestStackHandling(t *testing.T) {
// Crappy test. Try harder later.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a super terrible test. I could say I left it in so it could spark some discussion, but really, I just forgot it was here.

I'd love suggestions for how to get coverage or describe this behavior correctly... otherwise it should probably thrown away, because all it does is print stuff that has to be eyeballed, and that's just not a test.

Add OriginalError convenience accessor.  Rename data key var to make more sense.  Add package level documentation pointing out CatchAll's error wrapping behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants