Skip to content

Wrap functions with arguments, and add Rollbar to defer/recover functions#66

Merged
waltjones merged 4 commits into
masterfrom
wj-wrap-with-args
Oct 3, 2019
Merged

Wrap functions with arguments, and add Rollbar to defer/recover functions#66
waltjones merged 4 commits into
masterfrom
wj-wrap-with-args

Conversation

@waltjones

Copy link
Copy Markdown
Contributor

Wrap and WrapAndWait are convenient for reporting unhandled panics in wrapped functions, but until now could only wrap functions with no arguments and no return values. This PR allows any function signature to be used. It also exposes a new helper, LogPanic, that can be used to add Rollbar reporting to any defer/recover function.

Since wrapping a function in main() won't recover panics in goroutines, and since many programs do use goroutines liberally, this extra flexibility should help make it easier to report panics wherever needed.

The updated Wrap and WrapAndWait are fully compatible with previous/existing behavior, accepting func() just fine, and the existing behavior of returning the error is preserved.

While Wrap and WrapAndWait can now accept functions with any return signature, they do not return the function's return value. In cases where this is needed, the LogPanic helper can be used to add Rollbar to the function's defer/recover handler, instead of using any wrapper. Using LogPanic makes the resulting defer function simple and compact:

func ExampleFunction() {
  defer func() {
    err := recover()
    rollbar.LogPanic(err, true) // bool argument sets wait behavior
  }()

  // function body ...
}

An even more streamlined defer RollbarRecover() helper was considered, but recover() only works correctly when in a closure of the containing function, not when the defer function is a separate non-closure function.

@waltjones waltjones merged commit d2e3423 into master Oct 3, 2019
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.

1 participant