pwwang / pymedoo

A lightweight database framework for python
MIT License
15 stars 4 forks source link

When error occurs, Print very long sql may obstruct debugging #13

Closed void285 closed 2 years ago

void285 commented 2 years ago

I just encountered with a mysql related error, and find error printing in base.py may obstruct debugging when the sql is very long, so I change it like this:

            if len(self.sql) <= 300:
                raise type(ex)(str(ex) + ":\n" + self.sql)
            else:
                raise type(ex)(str(ex) + ":\n" + self.sql[:300] + "\tsql length: " + str(len(self.sql)))

I tried to insert 100 records in one batch, the sql it very long, in this case about 5M, to solve the problem, I have to reduce the amount the records in a batch, this is a mysql related issue, maybe not due to pymedoo.

The error I got is as below, print the whole sql is terrible, so I modify base.py to print the slim version:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/mysql/connector/network.py", line 159, in send_plain
    self.sock.sendall(packet)
BrokenPipeError: [Errno 32] Broken pipe

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/medoo/base.py", line 139, in query
    self.cursor.execute(self.sql)
  File "/usr/lib/python3.9/site-packages/mysql/connector/cursor.py", line 564, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/usr/lib/python3.9/site-packages/mysql/connector/connection.py", line 990, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/usr/lib/python3.9/site-packages/mysql/connector/connection.py", line 620, in _send_cmd
    self._socket.send(
  File "/usr/lib/python3.9/site-packages/mysql/connector/network.py", line 161, in send_plain
    raise OperationalError(
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 32 Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/e/todb.py", line 531, in <module>
    populate_items(seq)
  File "/e/todb.py", line 449, in populate_items
    dbs.insert('table_%s' % (seq), *batch)
  File "/usr/lib/python3.9/site-packages/medoo/base.py", line 83, in insert
    return self.query(sql, kwargs.get("commit", True))
  File "/usr/lib/python3.9/site-packages/medoo/base.py", line 150, in query
    raise type(ex)(str(ex) + ":\n" + self.sql[:300] + "\tsql length: " + str(len(self.sql)))
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 32 Broken pipe:
INSERT INTO `items` VALUES ('12650774',2973,1637632343,'\r\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." sql length: 5062186
2055
Exception ignored in: <function Base.__del__ at 0x6fffff894ee0>
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/medoo/base.py", line 56, in __del__
  File "/usr/lib/python3.9/site-packages/medoo/base.py", line 46, in close
  File "/usr/lib/python3.9/site-packages/mysql/connector/connection.py", line 585, in close
  File "/usr/lib/python3.9/site-packages/mysql/connector/connection.py", line 1074, in cmd_quit
  File "/usr/lib/python3.9/site-packages/mysql/connector/network.py", line 161, in send_plain
  File "/usr/lib/python3.9/site-packages/mysql/connector/errors.py", line 186, in __init__
  File "/usr/lib/python3.9/site-packages/mysql/connector/locales/__init__.py", line 61, in get_client_error
ImportError: No localization support for language 'eng'
void285 commented 2 years ago

About the mysql related error, FYI https://stackoverflow.com/questions/1884859/lost-connection-to-mysql-server-during-query

The mysql docs have a whole page dedicated to this error: http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

of note are

You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section B.5.2.10, “Packet too large”.

You can get more information about the lost connections by starting mysqld with the --log-warnings=2 option. This logs some of the disconnected errors in the hostname.err file
pwwang commented 2 years ago

Sounds good to me. Mind submitting a PR?

pwwang commented 2 years ago

Included in https://github.com/pwwang/pymedoo/releases/tag/0.1.2