diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-02-14 12:36:16 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-04-05 12:26:11 +0200 |
| commit | 79afcbe57ab0b83c75d44004e6c75c309540448e (patch) | |
| tree | 6202ec06ee6ff1d81fda9370e67763c432bd5952 /wrappers | |
| parent | d306d7b27ee1df60bf32f03b6160856e78d1b02c (diff) | |
Abstract error handling in python code
Signed-off-by: jwijenbergh <jeroenwijenbergh@protonmail.com>
Diffstat (limited to 'wrappers')
| -rw-r--r-- | wrappers/python/eduvpncommon/discovery.py | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/wrappers/python/eduvpncommon/discovery.py b/wrappers/python/eduvpncommon/discovery.py index d7fa73b..ae721c7 100644 --- a/wrappers/python/eduvpncommon/discovery.py +++ b/wrappers/python/eduvpncommon/discovery.py @@ -10,22 +10,49 @@ lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None lib.Verify.argtypes, lib.Verify.restype = [GoSlice, GoSlice, GoSlice, c_uint64], c_int64 lib.InsecureTestingSetExtraKey.argtypes, lib.InsecureTestingSetExtraKey.restype = [GoSlice], None -class RequestErrorCode(Enum): - ErrRequestFileError = 1 # The request for the file has failed. - ErrVerifySigError = 2 # The signature failed to verify. - Unknown = -1 # Other unknown error. - -def getOrganizationsList(): +def getOrganizationsList() -> str: dataError = lib.GetOrganizationsList() ptr = dataError.data - err = dataError.error + error = dataError.error body = None - if not err: + if not error: body = cast(ptr, c_char_p).value lib.FreeString(ptr) + if error: + raise RequestError(error) return body +class GoError(Exception): + message_dict: Enum + code: int + + def __init__(self, err: Enum, messages: dict): + assert err + try: + self.code = err + except ValueError: + self.code = -1 + self.message_dict = messages + + def __str__(self): + return self.message_dict[self.code] if self.code in self.message_dict else f"unknown error ({self.code})" + + +class RequestErrorCode(Enum): + ErrRequestFileError = 1 # The request for the file has failed. + ErrVerifySigError = 2 # The signature failed to verify. + Unknown = -1 # Other unknown error. + +class RequestError(GoError): + def __init__(self, err: int): + super().__init__(RequestErrorCode(err), + { + RequestErrorCode.ErrRequestFileError: "file request error", + RequestErrorCode.ErrVerifySigError: "signature verify error", + }) + + class VerifyErrorCode(Enum): ErrUnknownExpectedFileName = 1 # Unknown expected file name specified. The signature has not been verified. ErrInvalidSignature = 2 # Signature is invalid (for the expected file type). @@ -33,27 +60,15 @@ class VerifyErrorCode(Enum): ErrTooOld = 4 # Signature timestamp smaller than specified minimum signing time (rollback). Unknown = -1 # Other unknown error. - -class VerifyError(Exception): - code: VerifyErrorCode - code_int: int # Original error code also for VerifyErrorCode.Unknown - +class VerifyError(GoError): def __init__(self, err: int): - assert err - try: - self.code = VerifyErrorCode(err) - except ValueError: - self.code = VerifyErrorCode.Unknown - self.code_int = err - - def __str__(self): - return \ + super().__init__(VerifyErrorCode(err), { VerifyErrorCode.ErrUnknownExpectedFileName: "unknown expected file name", VerifyErrorCode.ErrInvalidSignature: "invalid signature", VerifyErrorCode.ErrInvalidSignatureUnknownKey: "invalid signature (unknown key)", VerifyErrorCode.ErrTooOld: "replay of previous signature (rollback)", - }[self.code] if self.code != VerifyErrorCode.Unknown else f"unknown verify error ({self.code_int})" + }) def verify(signature: bytes, signed_json: bytes, expected_file_name: str, min_sign_time: int) -> None: |
