Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sql.mysql.errors

import sql.mysql.errors

sql lifts part of this module out to its own top level; each name below is shown with the path that reaches it. Anything still spelled sql.mysql.errors.* needs import sql.mysql.errors.

Turning MySQL’s error numbers into the shared error classes.

MySQL reports two things about a failure: its own error number, such as 1062 for a duplicate key, and a five character SQLSTATE. The number is the specific one and the SQLSTATE is the portable one, so mapping works on the number where a case is worth telling apart and falls back to the SQLSTATE class otherwise. An error number this table has never seen still lands somewhere sensible.

MariaDB shares both schemes and most of the numbers, and the few it adds sit in the same SQLSTATE classes, so nothing here needs to know which server answered.

Constants

SPECIFIC

sql.mysql.errors.SPECIFIC = {...}

Error numbers specific enough to name a class of their own.

CLASSES

sql.mysql.errors.CLASSES = {...}

SQLSTATE classes, for numbers the table above does not name.

Functions

class_for()

sql.mysql.class_for(code, sqlstate) -> class

The class that best describes an error the server reported.

Parameters

  • code (number|nil) — MySQL’s own error number.
  • sqlstate (string|nil)

Returns class

raise_for()

sql.mysql.raise_for(report: dict, query)

Raises the right class for a server error report.

Parameters

  • report (dict) — { code, sqlstate, message }, as protocol.parse_error() returns it.
  • query (string|nil) — The statement that was running.

Raises SqlError always.


2026, Richard Ore and Zuri contributors