Using the example of Mr. Kwasnui, here is my best approach to finding specific errors:
The idea is to use tvariable to catch a simple error message, then you can catch the SQL states that you think might happen to save custom messages in your variable:
DELIMITER $$ DROP PROCEDURE IF EXISTS prc_work $$ CREATE PROCEDURE prc_work () BEGIN DECLARE EXIT HANDLER FOR SQLSTATE '23000' BEGIN SET @prc_work_error = 'Repeated key'; ROLLBACK TO sp_prc_work; END; DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN SET @prc_work_error = 'Unknown error'; ROLLBACK TO sp_prc_work; END; START TRANSACTION; SAVEPOINT sp_prc_work; INSERT into test (id, name) VALUES (1, 'SomeText'); COMMIT; END $$ DELIMITER ;
Then you just make your normal call and execute the select statement on a variable of type:
call prc_work(); select @prc_work_error;
This will return either NULL if there is no error, or a message error in case of an error. If you need a constant error message, you can create a table to store it.
This is tedious and not very flexible, because for every status code you want to catch, the DECLARE EXIT HANDLER segment is required, it will not show detailed error messages either, but hey, it works.
source share