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

Add example test for ErrorHandler #16

Merged
merged 1 commit into from
Nov 12, 2023
Merged

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Nov 12, 2023

I was writing docs on how to handle errors and panics in Go and realized
that we didn't have an example demonstrating the use of ErrorHandler
that I could copy/link to. Here, add one in.

// Either function can also set the job to be immediately cancelled, which
// we take advantage of here to make sure it's not retried in the example.
// Can also be `return nil`.
return &river.ErrorHandlerResult{SetCancelled: true}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See note here, but sort of had to set cancelled because otherwise there's a chance the job could retry before the example finishes up.

I guess I could also set max attempts to 1. Not sure which one is better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm torn. It's useful for this example to also demonstrate SetCancelled because there's nowhere else we'd do that. However I worry a bit about people copy-pasta'ing this without realizing what they're doing, particularly in HandlePanic where there's no comment about it.

Maybe the answer is to do both? If we set MaxAttempts to 1 for the entire worker, then we could easily show off both the SetCancelled: true usage as well as the more common usage where you probably don't want to cancel any job that panics or errors.

If we go that route, I'd say it makes the most sense to put the SetCancelled in HandlePanic so that HandleError can demonstrate the most common usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1 all that. Changed to max attempts of 1, and made HandleError return nil for copy/pasta convenience.


riverClient, err := river.NewClient(riverpgxv5.New(dbPool), &river.Config{
ErrorHandler: &CustomErrorHandler{},
Logger: slog.New(&slogutil.SlogMessageOnlyHandler{Level: 9}), // Suppress logging so example output is cleaner (9 > slog.LevelError).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit unconventional, but there's a fair bit of error output on error/panic from jobExecutor. Setting level to > slog.LevelError suppresses it and makes the test output much cleaner, but in case of something bad happening might also suppress other things I suppose. Not sure which is the lesser of two evils, but this is the one I went with.

Copy link
Contributor

Choose a reason for hiding this comment

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

The verbose error/panic output might be something we need to take a look at. I don't think there was much intentional design there, more that it just evolved that way as we pieced together functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. I don't think we're too far off though. If I was running this thing I'd expect to that there was reasonable error logging by default so problems aren't black holed without me having to chase down more configuration.

@brandur brandur force-pushed the brandur-error-handler-context branch from 34d4dcc to e2a00fc Compare November 12, 2023 21:55
Base automatically changed from brandur-error-handler-context to master November 12, 2023 21:57
@brandur brandur force-pushed the brandur-error-handler-example branch from c0b13bb to 46048c6 Compare November 12, 2023 21:57

riverClient, err := river.NewClient(riverpgxv5.New(dbPool), &river.Config{
ErrorHandler: &CustomErrorHandler{},
Logger: slog.New(&slogutil.SlogMessageOnlyHandler{Level: 9}), // Suppress logging so example output is cleaner (9 > slog.LevelError).
Copy link
Contributor

Choose a reason for hiding this comment

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

The verbose error/panic output might be something we need to take a look at. I don't think there was much intentional design there, more that it just evolved that way as we pieced together functionality.

// Either function can also set the job to be immediately cancelled, which
// we take advantage of here to make sure it's not retried in the example.
// Can also be `return nil`.
return &river.ErrorHandlerResult{SetCancelled: true}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm torn. It's useful for this example to also demonstrate SetCancelled because there's nowhere else we'd do that. However I worry a bit about people copy-pasta'ing this without realizing what they're doing, particularly in HandlePanic where there's no comment about it.

Maybe the answer is to do both? If we set MaxAttempts to 1 for the entire worker, then we could easily show off both the SetCancelled: true usage as well as the more common usage where you probably don't want to cancel any job that panics or errors.

If we go that route, I'd say it makes the most sense to put the SetCancelled in HandlePanic so that HandleError can demonstrate the most common usage.

@brandur brandur force-pushed the brandur-error-handler-example branch from 46048c6 to ecc13c7 Compare November 12, 2023 22:28
I was writing docs on how to handle errors and panics in Go and realized
that we didn't have an example demonstrating the use of `ErrorHandler`
that I could copy/link to. Here, add one in.
@brandur brandur force-pushed the brandur-error-handler-example branch from ecc13c7 to deeb6ac Compare November 12, 2023 22:29
@brandur
Copy link
Contributor Author

brandur commented Nov 12, 2023

Thanks!

@brandur brandur merged commit 285a510 into master Nov 12, 2023
5 checks passed
@brandur brandur deleted the brandur-error-handler-example branch November 12, 2023 22:33
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.

None yet

2 participants