Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, thistry
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 andtry
within individual goroutines) -- so there's some package docs that attempt to highlight that, and it's purposefully easy to use theCatchAll
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 (stdlibencoding
libraries are known for doing this internally), andtry
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