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

Handled unhandled exception to show bug report message #4565

Merged
merged 22 commits into from
Jan 17, 2023

Conversation

hawflau
Copy link
Contributor

@hawflau hawflau commented Jan 12, 2023

Which issue(s) does this change fix?

N/A

Why is this change necessary?

To show a message that guides customers to create a bug report in case of unhandled exceptions

How does it address the issue?

  • Handle the "unhandled exception" by printing the stack trace and the message with a github url
  • Include handling of click.BadOptionUsage, click.BadArgumentUsage, click.BadParameter and click.UsageError into the handling of UserException

Example:

> samdev build
.
.
.
'BuildProperties': {'Minify': False, 'Target': 'es2020', 'Sourcemap': True, 'External': ['./my-module-a.js', './my-module-b.js'], 'EntryPoints': ['app.js']}} architecture: x86_64 functions: HelloWorldFunction

Traceback:
  File "/Volumes/workplace/gh/aws-sam-cli/.venv39/lib/python3.9/site-packages/click/core.py", line 1055, in main
.
.
.
  File "/Volumes/workplace/gh/aws-sam-cli/samcli/lib/build/workflow_config.py", line 70, in get_selector
    raise UnsupportedBuilderException("'{}' does not have a supported builder".format(specified_workflow))

While trying to execute "samdev build", we encountered an unexpected error.
To create a bug report, follow the Github issue template below:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=samdev%20build%20-%20UnsupportedBuilderException

What side effects does this change have?

The actual exitcode of the execution in case of UnhandledException is still 1. This is to not break existing behavior. We only report 255 to Telemetry.

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Add input/output type hints to new functions/methods
  • Write design document if needed (Do I need to write a design document?)
  • Write/update unit tests
  • Write/update integration tests
  • Write/update functional tests if needed
  • make pr passes
  • make update-reproducible-reqs if dependencies were changed
  • Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

samcli/commands/exceptions.py Outdated Show resolved Hide resolved
@hawflau hawflau marked this pull request as ready for review January 12, 2023 16:37
@hawflau hawflau requested a review from a team as a code owner January 12, 2023 16:37
@hawflau hawflau requested review from qingchm and moelasmar January 12, 2023 16:37
Comment on lines 59 to 63
msg = (
f'An unexpected error was encountered while executing "{self._command}".\n'
"To create a bug report, follow the Github issue template below:\n"
f"{url}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this could make it easier to have duplicate issues (multiple issues asking about the same problem). Could we first give them a link to search issues with the same title that this issue would be auto-populated?
For example, for the example in your PR description it would be:

https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+samdev+build+-+UnsupportedBuilderException

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the messaging

@@ -148,16 +148,18 @@ def wrapped(*args, **kwargs):
# Execute the function and capture return value. This is returned back by the wrapper
# First argument of all commands should be the Context
return_value = func(*args, **kwargs)
except UserException as ex:
except (UserException, click.BadOptionUsage, click.BadArgumentUsage, click.BadParameter) as ex:
Copy link
Contributor

Choose a reason for hiding this comment

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

why did we add more exceptions to catch?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I understand now, but why did we add only these? Should we just catch all click exceptions? Or at least a few others that are here: https://click.palletsprojects.com/en/8.1.x/api/#exceptions

Copy link
Contributor Author

@hawflau hawflau Jan 12, 2023

Choose a reason for hiding this comment

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

Good catch. I missed click.UsageError.

The idea of grouping these click exceptions together is that these exceptions should be fixable by users. Any user-fixable-exception that is thrown within the call stacks wrapped by @track_command should be caught by this line. I don't want to catch click.ClickException in general because that could be over-catching. I'd rather an exception to be an Unhandled Exception by default.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha. What about NoSuchOption?

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 don't see any reason we should use NoSuchOption in our own code. So don't think we should include it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm misunderstanding but wouldn't NoSuchOption be raised by click when a user provides an option that doesn't exist? Or are you saying that wouldn't even reach this code anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't reach this code. We should have all @option declared before @track_command

exit_reason = type(ex).__name__
else:
# TODO (hawflau): review exitcode meaning. We now understand exitcode 1 as errors fixable by users.
exit_code = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did this change from ex.exit_code to 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

click.BadOptionUsage and other click exception caught here come with exitcode=2 by default. In our Telemetry, they were, however, classified as 255 (as we didn't catch any them). Today, we only have 1 and 255 exitcodes in our Telemetry. We should revisit the exitcode meaning someday. But for now, re-classifying them as exitcode 1 seems more correct to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

But why not use whatever actual exit code is thrown, instead of only either 1 or 255?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now I don't want to introduce new exitcode (e.g. 2) into Telemetry, until we have clearer understandings about how we want to use the exitcode. Today, click.BadOptionUsage and click.UsageError (maybe a couple other user-fixable exceptions as well) go into Telemetry with exitcode 255, which is certainly incorrect. I just want to fix this first.

samcli/commands/exceptions.py Outdated Show resolved Hide resolved
samcli/commands/exceptions.py Outdated Show resolved Hide resolved
samcli/commands/exceptions.py Outdated Show resolved Hide resolved
Copy link
Contributor

@torresxb1 torresxb1 left a comment

Choose a reason for hiding this comment

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

Left a few questions and suggestions

Copy link
Contributor

@jfuss jfuss left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for handling the feedback.

Copy link
Contributor

@sriram-mv sriram-mv left a comment

Choose a reason for hiding this comment

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

Thanks! do we have an idea of what kind of use cases would cause Unhandled Exceptions today?

samcli/commands/exceptions.py Outdated Show resolved Hide resolved
@hawflau
Copy link
Contributor Author

hawflau commented Jan 17, 2023

@sriram-mv

do we have an idea of what kind of use cases would cause Unhandled Exceptions today?

There are two cases from my observation:

  • Handled errors that are not raised as UserException or subclass of UserException
    • in somewhere we throw click exceptions (e.g. click.BadOptionUsage) but we did not handle it the same as UserException. This PR now includes certain click exceptions in the handling of UserException.
  • Bugs that we did not catch

@hawflau hawflau enabled auto-merge (squash) January 17, 2023 20:54
@hawflau hawflau merged commit ef89456 into aws:develop Jan 17, 2023
@hawflau hawflau deleted the 255-messaging branch January 18, 2023 03:41
mildaniel pushed a commit to mildaniel/aws-sam-cli that referenced this pull request Jan 26, 2023
* Handled unhandled exception to show bug report message

* Fix linting issues

* run black reformat

* Update message text and add unit test

* Run black reformat

* add test cases

* Update actual exitcode of UnhandledException to 1 to not break existing behavior

* remove unused import

* address feedback

* Update messaging

* Update Unit Tests GH Action to make sure Bug_report.md exists

* Update bug report

* Update Bug Report template

* fix typo

* Update wording

* update test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants