Skip to content

Why asymcmy MySQLError (and subclasses) don't save SQLSTATE? #138

@bluebike

Description

@bluebike

In this function (look down) MySQL errors messages are parsed.
But SQLSTATE is just ignored.
SQLState is 5 chars after # (if exists).

Sqlstate is sometimes easier to recognize some types of errors and error code.

asyncmy/asyncmy/errors.pyx

Lines 128 to 137 in 2497b7b

cpdef raise_mysql_exception(bytes data):
errno = H.unpack(data[1:3])[0]
if data[3] == ord("#"):
err_val = data[9:].decode("utf-8", "replace")
else:
err_val = data[3:].decode("utf-8", "replace")
error_class = error_map.get(errno)
if error_class is None:
error_class = InternalError if errno < 1000 else OperationalError
raise error_class(errno, err_val)

Method could changed to add sqlstate just by adding it parsing if and adding to error args.

cpdef raise_mysql_exception(bytes data):
    errno = H.unpack(data[1:3])[0]

    if data[3] == ord("#"):
        sql_state = data[4:9].decode("utf-8", "replace")
        err_val = data[9:].decode("utf-8", "replace")

    else:
        sql_state = None
        err_val = data[3:].decode("utf-8", "replace")

    error_class = error_map.get(errno)
    if error_class is None:
        error_class = InternalError if errno < 1000 else OperationalError
    raise error_class(errno, err_val, sql_state)

I guess only problem is that if some software assumes that asyncmy.errors.MySQLError-classes have only size 2 tuple args.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions