Skip to content

Commit c5b19b4

Browse files
committed
we have these constants, why not make use of them?
1 parent afbef5e commit c5b19b4

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

pymysql/connections.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from . import _auth
1414

1515
from .charset import charset_by_name, charset_by_id
16-
from .constants import CLIENT, COMMAND, CR, FIELD_TYPE, SERVER_STATUS
16+
from .constants import CLIENT, COMMAND, CR, ER, FIELD_TYPE, SERVER_STATUS
1717
from . import converters
1818
from .cursors import Cursor
1919
from .optionfile import Parser
@@ -441,7 +441,9 @@ def get_autocommit(self):
441441
def _read_ok_packet(self):
442442
pkt = self._read_packet()
443443
if not pkt.is_ok_packet():
444-
raise err.OperationalError(2014, "Command Out of Sync")
444+
raise err.OperationalError(
445+
CR.CR_COMMANDS_OUT_OF_SYNC, "Command Out of Sync"
446+
)
445447
ok = OKPacketWrapper(pkt)
446448
self.server_status = ok.server_status
447449
return ok
@@ -654,7 +656,8 @@ def connect(self, sock=None):
654656

655657
if isinstance(e, (OSError, IOError, socket.error)):
656658
exc = err.OperationalError(
657-
2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e)
659+
CR.CR_CONN_HOST_ERROR,
660+
"Can't connect to MySQL server on %r (%s)" % (self.host, e),
658661
)
659662
# Keep original exception and traceback to investigate error.
660663
exc.original_exception = e
@@ -945,7 +948,7 @@ def _process_auth(self, plugin_name, auth_packet):
945948
except AttributeError:
946949
if plugin_name != b"dialog":
947950
raise err.OperationalError(
948-
2059,
951+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
949952
"Authentication plugin '%s'"
950953
" not loaded: - %r missing authenticate method"
951954
% (plugin_name, type(handler)),
@@ -983,21 +986,21 @@ def _process_auth(self, plugin_name, auth_packet):
983986
self.write_packet(resp + b"\0")
984987
except AttributeError:
985988
raise err.OperationalError(
986-
2059,
989+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
987990
"Authentication plugin '%s'"
988991
" not loaded: - %r missing prompt method"
989992
% (plugin_name, handler),
990993
)
991994
except TypeError:
992995
raise err.OperationalError(
993-
2061,
996+
CR.CR_AUTH_PLUGIN_ERR,
994997
"Authentication plugin '%s'"
995998
" %r didn't respond with string. Returned '%r' to prompt %r"
996999
% (plugin_name, handler, resp, prompt),
9971000
)
9981001
else:
9991002
raise err.OperationalError(
1000-
2059,
1003+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
10011004
"Authentication plugin '%s' not configured" % (plugin_name,),
10021005
)
10031006
pkt = self._read_packet()
@@ -1007,7 +1010,8 @@ def _process_auth(self, plugin_name, auth_packet):
10071010
return pkt
10081011
else:
10091012
raise err.OperationalError(
1010-
2059, "Authentication plugin '%s' not configured" % plugin_name
1013+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
1014+
"Authentication plugin '%s' not configured" % plugin_name,
10111015
)
10121016

10131017
self.write_packet(data)
@@ -1024,7 +1028,7 @@ def _get_auth_plugin_handler(self, plugin_name):
10241028
handler = plugin_class(self)
10251029
except TypeError:
10261030
raise err.OperationalError(
1027-
2059,
1031+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
10281032
"Authentication plugin '%s'"
10291033
" not loaded: - %r cannot be constructed with connection object"
10301034
% (plugin_name, plugin_class),
@@ -1211,7 +1215,9 @@ def _read_load_local_packet(self, first_packet):
12111215
if (
12121216
not ok_packet.is_ok_packet()
12131217
): # pragma: no cover - upstream induced protocol error
1214-
raise err.OperationalError(2014, "Commands Out of Sync")
1218+
raise err.OperationalError(
1219+
CR.CR_COMMANDS_OUT_OF_SYNC, "Commands Out of Sync"
1220+
)
12151221
self._read_ok_packet(ok_packet)
12161222

12171223
def _check_packet_is_eof(self, packet):
@@ -1357,7 +1363,9 @@ def send_data(self):
13571363
break
13581364
conn.write_packet(chunk)
13591365
except IOError:
1360-
raise err.OperationalError(1017, f"Can't find file '{self.filename}'")
1366+
raise err.OperationalError(
1367+
ER.FILE_NOT_FOUND, f"Can't find file '{self.filename}'"
1368+
)
13611369
finally:
13621370
# send the empty packet to signify we are done sending data
13631371
conn.write_packet(b"")

0 commit comments

Comments
 (0)