Skip to content

sqlstate missing from previous minor release #1222

@Scrxtchy

Description

@Scrxtchy

# TODO: Append (sqlstate) in the error message. This will be come in next minor release.

Looks like the code here was actually mostly done up which I'm thankful for, I've been using a patch which mostly breaks the exception format but hands over the state and error message back to the software code.
It really looks like its just actually putting in the args to the error in a way that makes sense would have been the issue (projecting), but formally fixing this issue would be desirable to me rather than making a mess of the library code. This would have been an easy PR but I really can't think of a solution that makes sense either, thus my own work is pretty sloppy since I'm only writing it for myself

Its bad
def raise_mysql_exception(data):
    errno = struct.unpack("<h", data[1:3])[0]
    # https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_err_packet.html
    # Error packet has optional sqlstate that is 5 bytes and starts with '#'.
    errorclass = error_map.get(errno)
    if errorclass is None:
        errorclass = InternalError if errno < 1000 else OperationalError
    err = errorclass(errno)

    if data[3] == 0x23:  # '#'
        # sqlstate = data[4:9].decode()
        # TODO: Append (sqlstate) in the error message. This will be come in next minor release.
        err.sqlstate = data[4:9].decode("utf-8", "replace")
        err.errval = data[9:].decode("utf-8", "replace")
    else:
        err.errval = data[3:].decode("utf-8", "replace")
    raise err
-> breakpoint()
(Pdb) e
OperationalError(1644)
(Pdb) e.args
(1644,)
(Pdb) e.__dict__
{'sqlstate': '45000', 'errval': 'Record Delete Blocked'}
(Pdb) e.sqlstate
'45000'

Removing the error message from the args here isn't ideal, i'm aware 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions