Skip to content
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: 2 additions & 6 deletions cycode/cli/commands/auth/auth_command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import traceback

import click

from cycode.cli.commands.auth.auth_manager import AuthManager
Expand Down Expand Up @@ -54,16 +52,14 @@ def authorization_check(context: click.Context) -> None:
printer.print_result(passed_auth_check_res)
return
except (NetworkError, HttpUnauthorizedError):
if context.obj['verbose']:
click.secho(f'Error: {traceback.format_exc()}', fg='red')
ConsolePrinter(context).print_exception()

printer.print_result(failed_auth_check_res)
return


def _handle_exception(context: click.Context, e: Exception) -> None:
if context.obj['verbose']:
click.secho(f'Error: {traceback.format_exc()}', fg='red')
ConsolePrinter(context).print_exception()

errors: CliErrors = {
AuthProcessError: CliError(
Expand Down
4 changes: 1 addition & 3 deletions cycode/cli/exceptions/handle_report_sbom_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import traceback
from typing import Optional

import click
Expand All @@ -9,8 +8,7 @@


def handle_report_exception(context: click.Context, err: Exception) -> Optional[CliError]:
if context.obj['verbose']:
click.secho(f'Error: {traceback.format_exc()}', fg='red')
ConsolePrinter(context).print_exception()

errors: CliErrors = {
custom_exceptions.NetworkError: CliError(
Expand Down
4 changes: 1 addition & 3 deletions cycode/cli/exceptions/handle_scan_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import traceback
from typing import Optional

import click
Expand All @@ -14,8 +13,7 @@ def handle_scan_exception(
) -> Optional[CliError]:
context.obj['did_fail'] = True

if context.obj['verbose']:
click.secho(f'Error: {traceback.format_exc()}', fg='red')
ConsolePrinter(context).print_exception()

errors: CliErrors = {
custom_exceptions.NetworkError: CliError(
Expand Down
9 changes: 7 additions & 2 deletions cycode/cli/printers/console_printer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional
from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, Type

import click

Expand All @@ -15,7 +15,7 @@


class ConsolePrinter:
_AVAILABLE_PRINTERS: ClassVar[Dict[str, 'PrinterBase']] = {
_AVAILABLE_PRINTERS: ClassVar[Dict[str, Type['PrinterBase']]] = {
'text': TextPrinter,
'json': JsonPrinter,
'table': TablePrinter,
Expand Down Expand Up @@ -53,3 +53,8 @@ def print_result(self, result: CliResult) -> None:

def print_error(self, error: CliError) -> None:
self._printer_class(self.context).print_error(error)

def print_exception(self, e: Optional[BaseException] = None, force_print: bool = False) -> None:
"""Print traceback message in stderr if verbose mode is set."""
if force_print or self.context.obj.get('verbose', False):
self._printer_class(self.context).print_exception(e)
16 changes: 16 additions & 0 deletions cycode/cli/printers/printer_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Dict, List, Optional

Expand Down Expand Up @@ -30,3 +31,18 @@ def print_result(self, result: CliResult) -> None:
@abstractmethod
def print_error(self, error: CliError) -> None:
pass

def print_exception(self, e: Optional[BaseException] = None) -> None:
"""We are printing it in stderr so, we don't care about supporting JSON and TABLE outputs.

Note:
Called only when the verbose flag is set.
"""
if e is None:
# gets the most recent exception caught by an except clause
message = f'Error: {traceback.format_exc()}'
else:
traceback_message = ''.join(traceback.format_exception(e))
message = f'Error: {traceback_message}'

click.secho(message, err=True, fg=self.RED_COLOR_NAME)