8000 Encode output.print_error message to avoid UnicodeEncodeError when displaying errors by prilr · Pull Request #806 · oamg/leapp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

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

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion leapp/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
from contextlib import contextmanager
import six

from leapp.exceptions import LeappRuntimeError
from leapp.models import ErrorModel
Expand Down Expand Up @@ -43,9 +44,14 @@ def pretty_block(text, target, end_text=None, color=Color.bold, width=60):

def print_error(error):
model = ErrorModel.create(json.loads(error['message']['data']))

error_message = model.message
if six.PY2:
error_message = model.message.encode('utf-8', 'xmlcharrefreplace')

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))
message=error_message, time=model.time, actor=model.actor))
if model.details:
print('Summary:')
details = json.loads(model.details)
Expand Down
12 changes: 12 additions & 0 deletions tests/scripts/test_utils_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-

from leapp.utils.output import print_error


error = {'message': {'data': ('{"severity": "warning", "time": "2023-12-12T00:00:42", "actor": "An actor",'
'"message": "Нечакана"}')}}


def test_print_error_no_unicodedecodeerror():
# Make sure no exception is raised
print_error(error)
0