|
1 | 1 | import base64 |
2 | 2 | import json |
3 | | - |
4 | 3 | import gnupg |
5 | 4 | import requests |
6 | 5 |
|
7 | 6 | from .cert_pinning import TLSPinningAdapter |
8 | 7 | from .srp import User as PmsrpUser |
9 | 8 | from .constants import DEFAULT_TIMEOUT, SRP_MODULUS_KEY, SRP_MODULUS_KEY_FINGERPRINT |
10 | | - |
11 | | - |
12 | | -class ProtonError(Exception): |
13 | | - def __init__(self, ret): |
14 | | - self.code = ret['Code'] |
15 | | - self.error = ret['Error'] |
16 | | - try: |
17 | | - self.headers = ret["Headers"] |
18 | | - except KeyError: |
19 | | - self.headers = "" |
20 | | - |
21 | | - super().__init__("[{}] {} {}".format( |
22 | | - self.code, self.error, self.headers |
23 | | - )) |
| 9 | +from .exceptions import ProtonError, TLSPinningError, NewConnectionError, UnknownConnectionError |
24 | 10 |
|
25 | 11 |
|
26 | 12 | class Session: |
@@ -106,12 +92,19 @@ def api_request( |
106 | 92 | if fct is None: |
107 | 93 | raise ValueError("Unknown method: {}".format(method)) |
108 | 94 |
|
109 | | - ret = fct( |
110 | | - self.__api_url + endpoint, |
111 | | - headers=additional_headers, |
112 | | - json=jsondata, |
113 | | - timeout=self.__timeout |
114 | | - ) |
| 95 | + try: |
| 96 | + ret = fct( |
| 97 | + self.__api_url + endpoint, |
| 98 | + headers=additional_headers, |
| 99 | + json=jsondata, |
| 100 | + timeout=self.__timeout |
| 101 | + ) |
| 102 | + except requests.exceptions.ConnectionError as e: |
| 103 | + raise NewConnectionError(e) |
| 104 | + except TLSPinningError as e: |
| 105 | + raise TLSPinningError(e) |
| 106 | + except (Exception, requests.exceptions.BaseHTTPError) as e: |
| 107 | + raise UnknownConnectionError(e) |
115 | 108 |
|
116 | 109 | try: |
117 | 110 | ret = ret.json() |
|
0 commit comments