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

feat: add custom panic hook #168

Merged
merged 4 commits into from
Oct 7, 2021

Conversation

juniorbassani
Copy link
Contributor

Let me know what you think.

Closes #114

@@ -40,7 +101,11 @@ where
Fut: Future<Output = Result<T, E>>,
E: AsRef<dyn Error + 'static>
{
let client = TelemetryClient::new();
if is_enabled() {
let synth_command = serde_json::to_string(&args).unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't find a way to delay this serialization until inside the panic hook, unfortunately.

Copy link
Contributor

Choose a reason for hiding this comment

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

Short of adding #[derive(Clone)] to the Args and moving a clone of args into the closure, I don't see a way to do it either.

Copy link
Contributor

@brokad brokad left a comment

Choose a reason for hiding this comment

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

Looks great! I tested it with our PostHog instance and it all works 👌

I added just a couple thoughts.

let mut username = None;
let mut email = None;

println!("{}", "Synth encountered a fatal error. Because telemetry is enabled, an anonymous \
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it better if those are going to stderr instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. But only this first message should go to stderr, right? Otherwise, if the user, e.g., redirects stderr, they wouldn't see the other messages that wait for a response.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good point! Actually we should check synth is being run interactively before this otherwise this will deadlock.

This can be done with something like console::user_attended_stderr for an easy OOB solution.

I would still say it should all go to stderr, as this is, I think, where most people would expect diagnostic output to be sent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I haven't thought of that. I will follow your suggestions to fix the issue.

@@ -40,7 +101,11 @@ where
Fut: Future<Output = Result<T, E>>,
E: AsRef<dyn Error + 'static>
{
let client = TelemetryClient::new();
if is_enabled() {
let synth_command = serde_json::to_string(&args).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Short of adding #[derive(Clone)] to the Args and moving a clone of args into the closure, I don't see a way to do it either.

if is_enabled() {
let synth_command = serde_json::to_string(&args).unwrap();
// Register a custom panic hook that will send a bug report to posthog
panic::set_hook(Box::new(move |_| { report_panic(&synth_command, &TELEMETRY_CLIENT); }));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have the report_panic hook run after the "default" panic hook? That would still print the standard panic message which could be useful.

We could use panic::take_hook and move the old hook into the new one for that.

This replaces the default panic hook by one that will run the default
hook and send the panic occurrence to PostHog, with privacy sensitive
information erased (e.g., database names and user directories). The
hook also asks whether the user would like to send their name and e-mail
in case it is deemed necessary to enter in contact to obtain further
information.

The hook is only enabled when telemetry is enabled.
@juniorbassani juniorbassani changed the title Custom panic handler feat: add custom panic hook Oct 2, 2021
@juniorbassani juniorbassani requested a review from brokad October 2, 2021 13:48
@juniorbassani
Copy link
Contributor Author

I believe the issues pointed previously have now been fixed. I also refactored the code a little bit, because my previous changes left it messy. 😅

Copy link
Contributor

@brokad brokad left a comment

Choose a reason for hiding this comment

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

LGTM.

Thanks @juniorbassani for addressing everything so well and so quickly 😄

@brokad brokad merged commit c7af94a into shuttle-hq:master Oct 7, 2021
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.

Custom panic handler
2 participants