| 1 |
import json |
| 2 |
from types import SimpleNamespace |
| 3 |
|
| 4 |
|
| 5 |
class VarlinkEncoder(json.JSONEncoder): |
| 6 |
"""The Encoder used to encode JSON""" |
| 7 |
|
| 8 |
def default(self, o): |
| 9 |
if isinstance(o, set): |
| 10 |
return dict.fromkeys(o, {}) |
| 11 |
if isinstance(o, SimpleNamespace): |
| 12 |
return o.__dict__ |
| 13 |
if isinstance(o, VarlinkError): |
| 14 |
return o.as_dict() |
| 15 |
return json.JSONEncoder.default(self, o) |
| 16 |
|
| 17 |
|
| 18 |
class VarlinkError(Exception): |
| 19 |
"""The base class for varlink error exceptions""" |
| 20 |
|
| 21 |
@classmethod |
| 22 |
def new(cls, message, namespaced=False): |
| 23 |
if message["error"] == "org.varlink.service.InterfaceNotFound": |
| 24 |
return InterfaceNotFound.new(message, namespaced) |
| 25 |
|
| 26 |
elif message["error"] == "org.varlink.service.InvalidParameter": |
| 27 |
return InvalidParameter.new(message, namespaced) |
| 28 |
|
| 29 |
elif message["error"] == "org.varlink.service.MethodNotImplemented": |
| 30 |
return MethodNotImplemented.new(message, namespaced) |
| 31 |
|
| 32 |
elif message["error"] == "org.varlink.service.MethodNotImplemented": |
| 33 |
return MethodNotImplemented.new(message, namespaced) |
| 34 |
|
| 35 |
elif message["error"] == "org.varlink.service.MethodNotFound": |
| 36 |
return MethodNotFound.new(message, namespaced) |
| 37 |
|
| 38 |
else: |
| 39 |
return cls(message, namespaced) |
| 40 |
|
| 41 |
def __init__(self, message, namespaced=False): |
| 42 |
if not namespaced and not isinstance(message, dict): |
| 43 |
raise TypeError |
| 44 |
# normalize to dictionary |
| 45 |
Exception.__init__(self, json.loads(json.dumps(message, cls=VarlinkEncoder))) |
| 46 |
|
| 47 |
def error(self): |
| 48 |
"""returns the exception varlink error name""" |
| 49 |
return self.args[0].get("error") |
| 50 |
|
| 51 |
def parameters(self, namespaced=False): |
| 52 |
"""returns the exception varlink error parameters""" |
| 53 |
if namespaced: |
| 54 |
return json.loads( |
| 55 |
json.dumps(self.args[0]["parameters"]), |
| 56 |
object_hook=lambda d: SimpleNamespace(**d), |
| 57 |
) |
| 58 |
else: |
| 59 |
return self.args[0].get("parameters") |
| 60 |
|
| 61 |
def as_dict(self): |
| 62 |
return self.args[0] |
| 63 |
|
| 64 |
|
| 65 |
class InterfaceNotFound(VarlinkError): |
| 66 |
"""The standardized varlink InterfaceNotFound error as a python exception""" |
| 67 |
|
| 68 |
@classmethod |
| 69 |
def new(cls, message, namespaced=False): |
| 70 |
return cls( |
| 71 |
namespaced and message["parameters"].interface or message["parameters"].get("interface", None) |
| 72 |
) |
| 73 |
|
| 74 |
def __init__(self, interface): |
| 75 |
VarlinkError.__init__( |
| 76 |
self, |
| 77 |
{ |
| 78 |
"error": "org.varlink.service.InterfaceNotFound", |
| 79 |
"parameters": {"interface": interface}, |
| 80 |
}, |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
class MethodNotFound(VarlinkError): |
| 85 |
"""The standardized varlink MethodNotFound error as a python exception""" |
| 86 |
|
| 87 |
@classmethod |
| 88 |
def new(cls, message, namespaced=False): |
| 89 |
return cls(namespaced and message["parameters"].method or message["parameters"].get("method", None)) |
| 90 |
|
| 91 |
def __init__(self, method): |
| 92 |
VarlinkError.__init__( |
| 93 |
self, |
| 94 |
{ |
| 95 |
"error": "org.varlink.service.MethodNotFound", |
| 96 |
"parameters": {"method": method}, |
| 97 |
}, |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
class MethodNotImplemented(VarlinkError): |
| 102 |
"""The standardized varlink MethodNotImplemented error as a python exception""" |
| 103 |
|
| 104 |
@classmethod |
| 105 |
def new(cls, message, namespaced=False): |
| 106 |
return cls(namespaced and message["parameters"].method or message["parameters"].get("method", None)) |
| 107 |
|
| 108 |
def __init__(self, method): |
| 109 |
VarlinkError.__init__( |
| 110 |
self, |
| 111 |
{ |
| 112 |
"error": "org.varlink.service.MethodNotImplemented", |
| 113 |
"parameters": {"method": method}, |
| 114 |
}, |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
class InvalidParameter(VarlinkError): |
| 119 |
"""The standardized varlink InvalidParameter error as a python exception""" |
| 120 |
|
| 121 |
@classmethod |
| 122 |
def new(cls, message, namespaced=False): |
| 123 |
return cls( |
| 124 |
namespaced and message["parameters"].parameter or message["parameters"].get("parameter", None) |
| 125 |
) |
| 126 |
|
| 127 |
def __init__(self, name): |
| 128 |
VarlinkError.__init__( |
| 129 |
self, |
| 130 |
{ |
| 131 |
"error": "org.varlink.service.InvalidParameter", |
| 132 |
"parameters": {"parameter": name}, |
| 133 |
}, |
| 134 |
) |