Gitlab Community Edition Instance

Commit 29a9ab91 authored by Marcel Hellkamp's avatar Marcel Hellkamp
Browse files

Improved error message display

parent 9998974c
......@@ -223,10 +223,19 @@ class ApiError(Exception):
return self.json.get('detail') or {}
def __repr__(self):
return '{0.error}({0.status, msg="{0.message}")'.format(self)
return '{0.error}({0.status}, msg="{0.message}")'.format(self)
def __str__(self):
return '{0.error}({0.status, msg="{0.message}")'.format(self)
return '{0.error}({0.status}, msg="{0.message}")'.format(self)
def pretty(self):
err = "API Error: {} ({})\n".format(self.error, self.status)
err += "Message: {}\n".format(self.message)
if self.detail:
err += "Details:\n"
for k, v in self.detail:
err += " {}: {!r}\n".format(k, v)
return err
def _fix_filename(name):
......@@ -234,7 +243,7 @@ def _fix_filename(name):
name = name.lstrip("/")
# Fail hard on relative filenames
if name != os.path.normpath(name):
raise ValueError( "Archive file name not in a normalized form: {} != {}".format(name, os.path.normpath(name)))
raise ValueError("Archive file name not in a normalized form: {} != {}".format(name, os.path.normpath(name)))
return name
......
import argparse
import configparser
import importlib
import json
import os
import re
import sys
from ..cdstar import ApiError
__ALL__ = ["main", "register_subcommand", "printer"]
#: Commands to load automatically. The module pycdstar.cli.NAME must have register() defined.
......@@ -36,43 +39,58 @@ class Printer:
set_verbosity = __init__
def _print(self, msg, *args, **kwargs):
hr = kwargs.pop("highlight", None)
indent = kwargs.pop("indent", 0)
if args:
msg = msg.format(*args)
if indent:
msg = ''.join(' '*indent + line for line in msg.splitlines(True))
if hr:
ncols = self._ncols()
msg = (hr*ncols) + '\n' + msg + ('' if msg.endswith('\n') else '\n') + (hr*ncols)
print(msg, file=self.file, **kwargs)
def _ncols(self):
import shutil
return shutil.get_terminal_size((40, 20))[0]
def __call__(self, msg, *args, **kwargs):
""" Print only if -q (--quiet) was NOT passed as a command-line parameter """
if self.verbosity >= 0:
print(msg.format(*args), file=self.file, **kwargs)
self._print(msg, *args, **kwargs)
def v(self, msg, *args, **kwargs):
""" Print only if -v was passed as a command-line parameter """
if self.verbosity >= 1:
print(msg.format(*args), file=self.file, **kwargs)
self._print(msg, *args, **kwargs)
def vv(self, msg, *args, **kwargs):
""" Print only if -vv was passed as a command-line parameter """
if self.verbosity >= 2:
print(msg.format(*args), file=self.file, **kwargs)
self._print(msg, *args, **kwargs)
def vvv(self, msg, *args, **kwargs):
""" Print only if -vvv was passed as a command-line parameter """
if self.verbosity >= 3:
print(msg.format(*args), file=self.file, **kwargs)
self._print(msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
""" Print an error message (if not quiet) and optionally (-vv or higher) a stacktrace."""
print("ERROR: " + msg.format(*args), file=self.file, **kwargs)
self._print("ERROR: " + msg, *args, **kwargs)
if self.verbosity >= 2:
import traceback
traceback.print_exc(file=self.file)
self._print(traceback.format_exc(), highlight="=")
def fatal(self, msg, *args, **kwargs):
""" Print an error message (even if quiet) and optionally (-vv or higher) a stacktrace."""
print("FATAL: " + msg.format(*args), file=self.file, **kwargs)
self._print("FATAL: " + msg, *args, **kwargs)
if self.verbosity >= 2:
import traceback
traceback.print_exc(file=self.file)
self._print(traceback.format_exc(), highlight="=")
else:
self("Stacktrace not shown. Add -vv to print a full stacktrace.")
#: This should be used to print optional messages to the user.
#: Messages are printed to stderr, so only use it for complementary information, not for the primary results.
printer = Printer(level=0, file=sys.stderr)
......@@ -111,6 +129,11 @@ def main(args=None):
except CliError as e:
printer.error(str(e))
return e.return_code
except ApiError as e:
printer.error(str(e))
printer.v("Full error response:")
printer.v(json.dumps(e.json, indent=2), indent=2)
return e.status
except Exception as e:
printer.fatal("Uncaught exception ({}). Exiting...", e)
return 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment