Skip to content

Commit 6665287

Browse files
committed
Fix Bad HTTP error code reading #4 and also improve error handling
1 parent 7085404 commit 6665287

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

mystbin/client.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async def get(self, paste_id: str) -> PasteData:
119119
paste_id_match = MB_URL_RE.match(paste_id)
120120

121121
if not paste_id_match:
122-
raise BadPasteID("This is an invalid Mystb.in paste ID.")
122+
raise BadPasteID("This is an invalid Mystb.in paste ID.", None)
123123

124124
paste_id = paste_id_match.group("ID")
125125

@@ -129,8 +129,8 @@ async def get(self, paste_id: str) -> PasteData:
129129
assert self.session is not None
130130

131131
async with self.session.get(f"{API_BASE_URL}/{paste_id}", timeout=aiohttp.ClientTimeout(CLIENT_TIMEOUT)) as response:
132-
if 200 <= response.status < 300:
133-
raise BadPasteID("This is an invalid Mystb.in paste ID.")
132+
if not 200 <= response.status < 300:
133+
raise BadPasteID("This is an invalid Mystb.in paste ID.", response)
134134
paste_data = await response.json()
135135

136136
return PasteData(paste_id, paste_data)
@@ -166,13 +166,13 @@ def get(self, paste_id: str) -> PasteData:
166166
paste_id_match = MB_URL_RE.match(paste_id)
167167

168168
if not paste_id_match:
169-
raise BadPasteID("This is an invalid Mystb.in paste ID.")
169+
raise BadPasteID("This is an invalid Mystb.in paste ID.", None)
170170

171171
paste_id = paste_id_match.group("ID")
172172

173173
with self.session.get(f"{API_BASE_URL}/{paste_id}", timeout=CLIENT_TIMEOUT) as response:
174174
if 200 <= response.status_code < 300:
175-
raise BadPasteID("This is an invalid Mystb.in paste ID.")
175+
raise BadPasteID("This is an invalid Mystb.in paste ID.", response)
176176

177177
paste_data = response.json()
178178
return PasteData(paste_id, paste_data)

mystbin/errors.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,34 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25+
from __future__ import annotations
26+
27+
2528
__all__ = (
2629
"BadPasteID",
2730
"MystbinException",
2831
"APIError",
2932
)
3033

34+
from typing import Optional, Union
35+
36+
import aiohttp
3137

32-
class BadPasteID(ValueError):
33-
"""Bad Paste ID."""
38+
39+
try:
40+
import requests
41+
except ModuleNotFoundError:
42+
pass
3443

3544

3645
class MystbinException(Exception):
3746
"""Error when interacting with Mystbin."""
47+
def __init__(self, /, message: str, response: Optional[Union[aiohttp.ClientResponse, requests.Response]]) -> None:
48+
self.message: str = message
49+
self.response: Optional[Union[aiohttp.ClientResponse, requests.Response]] = response
50+
51+
class BadPasteID(MystbinException):
52+
"""Bad Paste ID."""
3853

3954

4055
class APIError(MystbinException):

0 commit comments

Comments
 (0)