Skip to content
9 changes: 8 additions & 1 deletion src/azure-cli-core/azure/cli/core/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ def strip_command(command):
contents = [item for item in command.split(' ') if item]
return ' '.join(contents).strip()

return [strip_command(example.command) for example in help_file.examples]
examples = []
for example in help_file.examples:
examples.append({
'command': strip_command(example.command),
'description': example.name
})

return examples

def _register_help_loaders(self):
import azure.cli.core._help_loaders as help_loaders
Expand Down
49 changes: 26 additions & 23 deletions src/azure-cli-core/azure/cli/core/azclierror.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,58 @@ def __init__(self, error_msg, recommendation=None):
# error message
self.error_msg = error_msg

# set recommendations to fix the error if the message is not actionable,
# they will be printed to users after the error message, one recommendation per line
# manual recommendations provided based on developers' knowledge
self.recommendations = []
self.set_recommendation(recommendation)

# AI recommendations provided by Aladdin service, with tuple form: (recommendation, description)
self.aladdin_recommendations = []

# exception trace for the error
self.exception_trace = None
super().__init__(error_msg)

def set_recommendation(self, recommendation):
"""" Set manual recommendations for the error.
Command module or extension authors could call this method to provide recommendations,
the recommendations will be printed after the error message, one recommendation per line
"""
if isinstance(recommendation, str):
self.recommendations.append(recommendation)
elif isinstance(recommendation, list):
self.recommendations.extend(recommendation)

def set_aladdin_recommendation(self, recommendations):
""" Set aladdin recommendations for the error.
One item should be a tuple with the form: (recommendation, description)
"""
self.aladdin_recommendations.extend(recommendations)

def set_exception_trace(self, exception_trace):
self.exception_trace = exception_trace

def print_error(self):
from azure.cli.core.azlogging import CommandLoggerContext
from azure.cli.core.style import print_styled_text
with CommandLoggerContext(logger):
# print error type and error message
message = '{}: {}'.format(self.__class__.__name__, self.error_msg)
logger.error(message)
# print error message
logger.error(self.error_msg)

# print exception trace if there is
if self.exception_trace:
logger.exception(self.exception_trace)

# print recommendations to action
if self.recommendations:
for recommendation in self.recommendations:
print(recommendation, file=sys.stderr)

if self.aladdin_recommendations:
print('\nTRY THIS:', file=sys.stderr)
Copy link
Member

@jiasli jiasli Dec 16, 2020

Choose a reason for hiding this comment

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

We can use print_styled_text("\nTRY THIS:") or print_styled_text((Style.PRIMARY, "\nTRY THIS:")) once #16258 is merged.

for recommendation, description in self.aladdin_recommendations:
print_styled_text(recommendation, file=sys.stderr)
Copy link
Member

Choose a reason for hiding this comment

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

sys.stderr is now the default value for file.

print_styled_text(description, file=sys.stderr)

def send_telemetry(self):
telemetry.set_error_type(self.__class__.__name__)
# endregion
Expand Down Expand Up @@ -101,15 +121,6 @@ def send_telemetry(self):
super().send_telemetry()
telemetry.set_failure(self.error_msg)

def print_error(self):
from azure.cli.core.azlogging import CommandLoggerContext
with CommandLoggerContext(logger):
# print only error message (no error type)
logger.error(self.error_msg)
# print recommendations to action
if self.recommendations:
for recommendation in self.recommendations:
print(recommendation, file=sys.stderr)
# endregion


Expand Down Expand Up @@ -233,15 +244,7 @@ class UnclassifiedUserFault(UserFault):
Avoid using this class unless the error can not be classified into
the UserFault related specific error types.
"""
def print_error(self):
from azure.cli.core.azlogging import CommandLoggerContext
with CommandLoggerContext(logger):
# print only error message (no error type)
logger.error(self.error_msg)
# print recommendations to action
if self.recommendations:
for recommendation in self.recommendations:
print(recommendation, file=sys.stderr)
pass


# CLI internal error type
Expand Down
Loading