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

Encode output.print_error message to avoid UnicodeEncodeError when displaying errors #806

Merged
merged 2 commits into from Jan 27, 2023

Conversation

prilr
Copy link
Contributor

@prilr prilr commented Dec 9, 2022

When leapp executes in a Python 2 environment, an actor encountering an error can cause further issues if the message string provided to leapp.utils.output contains non-ASCII characters.

This line in output.py is the main problem:

sys.stdout.write("{red}{time} [{severity}]{reset} Actor: {actor}\nMessage: {message}\n".format(
        red=Color.red, reset=Color.reset, severity=model.severity.upper(),
        message=model.message, time=model.time, actor=model.actor))

As you can see, this line isn't Unicode in Python 2, which is why trying to format it with u-strings directly will end up causing an error like the one below:

Dec 05 08:19:44 localhost upgrade[437]: Traceback (most recent call last):
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/bin/leapp", line 9, in <module>
Dec 05 08:19:44 localhost upgrade[437]:     load_entry_point('leapp==0.14.0', 'console_scripts', 'leapp')()
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/cli/__init__.py", line 37, in main
Dec 05 08:19:44 localhost upgrade[437]:     cli.command.execute('leapp version {}'.format(VERSION))
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/utils/clicmd.py", line 106, in execute
Dec 05 08:19:44 localhost upgrade[437]:     args.func(args)
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/utils/clicmd.py", line 128, in called
Dec 05 08:19:44 localhost upgrade[437]:     self.target(args)
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/cli/commands/upgrade/breadcrumbs.py", line 94, in wrapper
Dec 05 08:19:44 localhost upgrade[437]:     return f(*args, breadcrumbs=breadcrumbs, **kwargs)
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/cli/commands/upgrade/__init__.py", line 101, in upgrade
Dec 05 08:19:44 localhost upgrade[437]:     report_errors(workflow.errors)
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/utils/output.py", line 100, in report_errors
Dec 05 08:19:44 localhost upgrade[437]:     print_error(error)
Dec 05 08:19:44 localhost upgrade[437]:   File "/usr/lib/python2.7/site-packages/leapp/utils/output.py", line 48, in print_error
Dec 05 08:19:44 localhost upgrade[437]:     message=model.message, time=model.time, actor=model.actor))
Dec 05 08:19:44 localhost upgrade[437]: UnicodeEncodeError: 'ascii' codec can't encode character u'\u2192' in position 389382: ordinal not in range(128)
Dec 05 08:19:44 localhost upgrade[433]: Container sysroot failed with error code 1.

This results in the actor error itself not being displayed, only the subsequent UnicodeEncodeError exception.

I'm not sure the way I went about correcting the problem in this PR is the optimal one.
If there's a different solution you'd rather see, please let me know.

@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Thank you for contributing to the Leapp project!

Please note that every PR needs to comply with the Leapp Guidelines and must pass all tests in order to be mergable.
If you want to request a review or rebuild a package in copr, you can use following commands as a comment:

  • review please to notify leapp developers of review request
  • /packit copr-build to submit a public copr build using packit

To launch regression testing public members of oamg organization can leave the following comment:

  • /rerun to schedule basic regression tests using this pr build and leapp-repository*master* as artifacts
  • /rerun 42 to schedule basic regression tests using this pr build and leapp-repository*PR42* as artifacts
  • /rerun-all to schedule all tests (including sst) using this pr build and leapp-repository*master* as artifacts
  • /rerun-all 42 to schedule all tests (including sst) using this pr build and leapp-repository*PR42* as artifacts

Please open ticket in case you experience technical problem with the CI. (RH internal only)

Note: In case there are problems with tests not being triggered automatically on new PR/commit or pending for a long time, please consider rerunning the CI by commenting leapp-ci build (might require several comments). If the problem persists, contact leapp-infra.

@leapp-bot
Copy link
Collaborator

This PR has been linked in issue tracker (OAMG-8043).

Copy link
Member

@fernflower fernflower left a comment

Choose a reason for hiding this comment

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

Code looks ok, but I am really missing a unit test that covers this particular case. Could you please add one?

@fernflower
Copy link
Member

@prilr Added a really naive unit test, checked it failed on py2.7 if applied on master.

If you are ok with the changes, please squash previous 5 commits into one and let's merge it :)

@fernflower
Copy link
Member

/rerun

@github-actions
Copy link

Copr build succeeded: https://copr.fedorainfracloud.org/coprs/build/5309507

@github-actions
Copy link

Testing Farm request for RHEL-8.6-rhui/5290656;5309507 regression testing has been created.
Once finished, results should be available here.
Full pipeline log.

@github-actions
Copy link

Testing Farm request for RHEL-7.9-rhui/5290656;5309507 regression testing has been created.
Once finished, results should be available here.
Full pipeline log.

@github-actions
Copy link

Testing Farm request for RHEL-8.6.0-Nightly/5290656;5309507 regression testing has been created.
Once finished, results should be available here.
Full pipeline log.

@github-actions
Copy link

Testing Farm request for RHEL-8.7.0-Nightly/5290656;5309507 regression testing has been created.
Once finished, results should be available here.
Full pipeline log.

@github-actions
Copy link

Testing Farm request for RHEL-7.9-ZStream/5290656;5309507 regression testing has been created.
Once finished, results should be available here.
Full pipeline log.

@github-actions
Copy link

Testing Farm request for RHEL-7.9-ZStream/5290656;5309507 regression testing has been created.
Once finished, results should be available here.
Full pipeline log.

prilr and others added 2 commits January 27, 2023 14:53
Add a test that fails without a patch for python2.7 but passes with
the patch applied.
@fernflower
Copy link
Member

So I went on and squashed the commits :) Thanks for the contribution!

@fernflower fernflower merged commit 7c551bc into oamg:master Jan 27, 2023
@pirat89 pirat89 added the changelog-checked The merger/reviewer checked the changelog draft document and updated it when relevant label Feb 21, 2023
pirat89 added a commit to pirat89/leapp that referenced this pull request Feb 21, 2023
## Packaging
-  Change permissions for /var/lib/leapp to 0700 (oamg#807)

## Framework
### Fixes
- Prevent unicode errors when printing error messages (oamg#806)
- Make checks for missing answers more strict (oamg#797)

### Enhancements
- Expose tracebacks from actors (oamg#802)

### Known issue
- introduced new known issue in framework...
@pirat89 pirat89 mentioned this pull request Feb 21, 2023
pirat89 added a commit to pirat89/leapp that referenced this pull request Feb 21, 2023
## Packaging
-  Change permissions for /var/lib/leapp to 0700 (oamg#807)

## Framework
### Fixes
- Prevent unicode errors when printing error messages (oamg#806)
- Make checks for missing answers more strict (oamg#797)

### Enhancements
- Expose tracebacks from actors (oamg#802)
pirat89 added a commit to pirat89/leapp that referenced this pull request Feb 21, 2023
## Packaging
-  Change permissions for /var/lib/leapp to 0700 (oamg#807)

## Framework
### Fixes
- Prevent unicode errors when printing error messages (oamg#806)
- Make checks for missing answers more strict (oamg#797)

### Enhancements
- Expose tracebacks from actors (oamg#802)
pirat89 added a commit to pirat89/leapp that referenced this pull request Feb 21, 2023
## Packaging
-  Change permissions for /var/lib/leapp to 0700 (oamg#807)

## Framework
### Fixes
- Make checks for missing answers more strict (oamg#797)
- Prevent unicode errors when printing error messages (oamg#806)

### Enhancements
- Expose tracebacks from actors (oamg#802)
pirat89 added a commit that referenced this pull request Feb 21, 2023
## Packaging
-  Change permissions for /var/lib/leapp to 0700 (#807)

## Framework
### Fixes
- Make checks for missing answers more strict (#797)
- Prevent unicode errors when printing error messages (#806)

### Enhancements
- Expose tracebacks from actors (#802)
@prilr prilr deleted the unicode-err branch May 11, 2023 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-checked The merger/reviewer checked the changelog draft document and updated it when relevant
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants