Skip to content

Commit b9f32b2

Browse files
committed
Handle inconsistent error codes and names used in Python 2 #1412
1 parent 27797a4 commit b9f32b2

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

resources/lib/youtube_plugin/kodion/network/http_server.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313
import re
1414
import socket
1515
from collections import deque
16-
from errno import (
17-
ECONNABORTED,
18-
ECONNREFUSED,
19-
ECONNRESET,
20-
EPIPE,
21-
EPROTOTYPE,
22-
ESHUTDOWN,
23-
)
16+
from errno import errorcode
2417
from functools import partial
2518
from io import open
2619
from json import dumps as json_dumps, loads as json_loads
@@ -128,12 +121,17 @@ class RequestHandler(BaseHTTPRequestHandler, object):
128121
}
129122

130123
SWALLOWED_ERRORS = {
131-
ECONNABORTED,
132-
ECONNREFUSED,
133-
ECONNRESET,
134-
EPIPE,
135-
EPROTOTYPE,
136-
ESHUTDOWN,
124+
'ECONNABORTED',
125+
'ECONNREFUSED',
126+
'ECONNRESET',
127+
'EPIPE',
128+
'EPROTOTYPE',
129+
'ESHUTDOWN',
130+
'WSAECONNABORTED',
131+
'WSAECONNREFUSED',
132+
'WSAECONNRESET',
133+
'WSAEPROTOTYPE',
134+
'WSAESHUTDOWN',
137135
}
138136

139137
def __init__(self, request, client_address, server):
@@ -176,11 +174,23 @@ def handle_one_request(self):
176174
try:
177175
super(RequestHandler, self).handle_one_request()
178176
return
179-
except (HTTPError, OSError) as exc:
177+
except Exception as exc:
180178
self.close_connection = True
181179
self.log.exception('Request failed')
182-
if (isinstance(exc, HTTPError)
183-
or getattr(exc, 'errno', None) in self.SWALLOWED_ERRORS):
180+
if isinstance(exc, HTTPError):
181+
return
182+
error = getattr(exc, 'errno', None)
183+
if error and errorcode.get(error) in self.SWALLOWED_ERRORS:
184+
return
185+
raise exc
186+
187+
def finish(self):
188+
try:
189+
super(RequestHandler, self).finish()
190+
except Exception as exc:
191+
self.log.exception('File object failed to close cleanly')
192+
error = getattr(exc, 'errno', None)
193+
if error and errorcode.get(error) in self.SWALLOWED_ERRORS:
184194
return
185195
raise exc
186196

0 commit comments

Comments
 (0)