Gitlab Community Edition Instance

Commit fa857f78 authored by Marcel Hellkamp's avatar Marcel Hellkamp
Browse files

Better error handling

parent 47e34cc2
......@@ -91,7 +91,7 @@ def main(*args): # noqa: C901
printer.v(json.dumps(e.json, indent=2), indent=2)
return e.status
except Exception as e:
printer.fatal("Uncaught exception ({}). Exiting...", e)
printer.fatal("Uncaught exception ({}: {}). Exiting...", type(e).__name__, e)
return 1
......
import os
from json import JSONDecodeError
import requests
from requests_toolbelt import MultipartEncoder
......@@ -59,8 +60,7 @@ class CDStar:
if rs.ok or (expect_status and rs.status_code in expect_status):
return rs
# TODO: handle hard errors (JSONDecodeError)
raise ApiError(rs.json())
raise ApiError(rs)
def rest(self, method, *path, expect_status=None, **options) -> dict:
""" Just like `raw()`, but expects the response to be JSON and returns
......@@ -311,8 +311,18 @@ class FileDownload:
class ApiError(Exception):
def __init__(self, json):
self.json = json
def __init__(self, rs):
if rs.ok:
raise AssertionError("Not an error response: " + rs)
if rs.headers.get("Content-Type") != "application/json":
raise AssertionError("Not a JSON response: " + rs)
self.rs = rs
try:
self.json = rs.json()
except JSONDecodeError as e:
raise AssertionError("Failed to decode server error response (invalid JSON) for: " + rs.request)
@property
def error(self):
......@@ -331,7 +341,9 @@ 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}): {0.message}'.format(self)
__str__ = __repr__
def pretty(self):
err = "API Error: {} ({})\n".format(self.error, self.status)
......
Supports Markdown
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