Starlark Procedure Error Reporting¶
Inside a Starlark procedure, a failed
mo.sql()call returns an error value that still prints and concatenates as the message, and additionally carries the MySQL error number and SQLSTATE. Read them withmo.errno()andmo.sqlstate()to branch on the error class instead of matching message text.
Description¶
When mo.sql() fails inside a Starlark procedure, its second return value is an error value. The value keeps its previous behavior — it prints and concatenates as the plain message string — and now also carries two codes:
mo.errno()returns the MySQL error number (for example1062for a duplicate entry).mo.sqlstate()returns the SQLSTATE (for example23000for the integrity-constraint class).
On success, mo.sql() returns None for the error, so the codes are not populated. A subsequent successful call clears the record, so mo.errno() never reports a stale class.
Syntax¶
mo.errno()
mo.sqlstate()
Both builtins are called with no arguments, from inside a Starlark procedure, after a mo.sql() call.
Arguments¶
Neither builtin accepts arguments; each returns the code recorded by the most recent failed mo.sql() call:
Function |
Return value |
|---|---|
|
The MySQL error number (for example |
|
The SQLSTATE (for example |
Examples¶
DROP DATABASE IF EXISTS starlark_err_demo;
CREATE DATABASE starlark_err_demo;
USE starlark_err_demo;
CREATE TABLE t (a INT PRIMARY KEY, b VARCHAR(10));
INSERT INTO t VALUES (1, 'one');
CREATE TABLE plog (id INT, note VARCHAR(300));
CREATE OR REPLACE PROCEDURE sp_err() LANGUAGE 'starlark'
$$
rs, err = mo.sql("insert into t values (1, 'dup')")
code = -1 if err == None else mo.errno()
state = "none" if err == None else mo.sqlstate()
mo.sql("insert into plog values (1, 'code={} sqlstate={}')".format(code, state))
$$;
CALL sp_err();
SELECT id, note FROM plog ORDER BY id;
DROP DATABASE starlark_err_demo;
Usage Notes¶
The error value is still a printable string;
len(err), slicing,startswith(...), and string formatting continue to work.Assigning the error value to an
OUTparameter still delivers the message string, as it did before the codes existed.On success the error is
None; derive codes only from the error value, not from a stale record.