1 from __future__
import annotations
5 from ..utils
import YoutubeDLError
7 if typing
.TYPE_CHECKING
:
8 from .common
import RequestHandler
, Response
11 class RequestError(YoutubeDLError
):
14 msg
: str |
None = None,
15 cause
: Exception |
str |
None = None,
16 handler
: RequestHandler
= None,
18 self
.handler
= handler
25 class UnsupportedRequest(RequestError
):
26 """raised when a handler cannot handle a request"""
30 class NoSupportingHandlers(RequestError
):
31 """raised when no handlers can support a request for various reasons"""
33 def __init__(self
, unsupported_errors
: list[UnsupportedRequest
], unexpected_errors
: list[Exception]):
34 self
.unsupported_errors
= unsupported_errors
or []
35 self
.unexpected_errors
= unexpected_errors
or []
37 # Print a quick summary of the errors
39 for err
in unsupported_errors
:
40 err_handler_map
.setdefault(err
.msg
, []).append(err
.handler
.RH_NAME
)
42 reason_str
= ', '.join([f
'{msg} ({", ".join(handlers)})' for msg
, handlers
in err_handler_map
.items()])
44 reason_str
= ' + '.join(filter(None, [reason_str
, f
'{len(unexpected_errors)} unexpected error(s)']))
46 err_str
= 'Unable to handle request'
48 err_str
+= f
': {reason_str}'
50 super().__init
__(msg
=err_str
)
53 class TransportError(RequestError
):
54 """Network related errors"""
57 class HTTPError(RequestError
):
58 def __init__(self
, response
: Response
, redirect_loop
=False):
59 self
.response
= response
60 self
.status
= response
.status
61 self
.reason
= response
.reason
62 self
.redirect_loop
= redirect_loop
63 msg
= f
'HTTP Error {response.status}: {response.reason}'
65 msg
+= ' (redirect loop detected)'
67 super().__init
__(msg
=msg
)
73 return f
'<HTTPError {self.status}: {self.reason}>'
76 class IncompleteRead(TransportError
):
77 def __init__(self
, partial
: int, expected
: int |
None = None, **kwargs
):
78 self
.partial
= partial
79 self
.expected
= expected
80 msg
= f
'{partial} bytes read'
81 if expected
is not None:
82 msg
+= f
', {expected} more expected'
84 super().__init
__(msg
=msg
, **kwargs
)
87 return f
'<IncompleteRead: {self.msg}>'
90 class SSLError(TransportError
):
94 class CertificateVerifyError(SSLError
):
95 """Raised when certificate validated has failed"""
99 class ProxyError(TransportError
):
103 network_exceptions
= (HTTPError
, TransportError
)