Module imandrax_api.twirp.exceptions
Functions
def InvalidArgument(*, argument, error)
-
Expand source code
def InvalidArgument(*, argument, error): return TwirpServerException( code=errors.Errors.InvalidArgument, message="{} {}".format(argument, error), meta={ "argument":argument } )
def RequiredArgument(*, argument)
-
Expand source code
def RequiredArgument(*, argument): return InvalidArgument( argument=argument, error="is required" )
def twirp_error_from_intermediary(status, reason, headers, body)
-
Expand source code
def twirp_error_from_intermediary(status, reason, headers, body): # see https://twitchtv.github.io/twirp/docs/errors.html#http-errors-from-intermediary-proxies meta = { 'http_error_from_intermediary': 'true', 'status_code': str(status), } if 300 <= status < 400: # twirp uses POST which should not redirect code = errors.Errors.Internal location = headers.get('location') message = 'unexpected HTTP status code %d "%s" received, Location="%s"' % ( status, reason, location, ) meta['location'] = location else: code = { 400: errors.Errors.Internal, # JSON response should have been returned 401: errors.Errors.Unauthenticated, 403: errors.Errors.PermissionDenied, 404: errors.Errors.BadRoute, 429: errors.Errors.ResourceExhausted, 502: errors.Errors.Unavailable, 503: errors.Errors.Unavailable, 504: errors.Errors.Unavailable, }.get(status, errors.Errors.Unknown) message = 'Error from intermediary with HTTP status code %d "%s"' % ( status, reason, ) meta['body'] = body return TwirpServerException(code=code, message=message, meta=meta)
Classes
class TwirpServerException (*, code, message, meta={})
-
Expand source code
class TwirpServerException(httplib.HTTPException): def __init__(self, *, code, message, meta={}): try: self._code = errors.Errors(code) except ValueError: self._code = errors.Errors.Unknown self._message = message self._meta = meta super(TwirpServerException, self).__init__(message) @property def code(self): if isinstance(self._code, errors.Errors): return self._code return errors.Errors.Unknown @property def message(self): return self._message @property def meta(self): return self._meta def to_dict(self): err = { "code": self._code.value, "msg": self._message, "meta": {} } for k, v in self._meta.items(): err["meta"][k] = str(v) return err def to_json_bytes(self): return json.dumps(self.to_dict()).encode('utf-8') @staticmethod def from_json(err_dict): return TwirpServerException( code=err_dict.get('code', errors.Errors.Unknown), message=err_dict.get('msg',''), meta=err_dict.get('meta',{}), )
Common base class for all non-exit exceptions.
Ancestors
- http.client.HTTPException
- builtins.Exception
- builtins.BaseException
Static methods
def from_json(err_dict)
-
Expand source code
@staticmethod def from_json(err_dict): return TwirpServerException( code=err_dict.get('code', errors.Errors.Unknown), message=err_dict.get('msg',''), meta=err_dict.get('meta',{}), )
Instance variables
prop code
-
Expand source code
@property def code(self): if isinstance(self._code, errors.Errors): return self._code return errors.Errors.Unknown
prop message
-
Expand source code
@property def message(self): return self._message
prop meta
-
Expand source code
@property def meta(self): return self._meta
Methods
def to_dict(self)
-
Expand source code
def to_dict(self): err = { "code": self._code.value, "msg": self._message, "meta": {} } for k, v in self._meta.items(): err["meta"][k] = str(v) return err
def to_json_bytes(self)
-
Expand source code
def to_json_bytes(self): return json.dumps(self.to_dict()).encode('utf-8')