Skip to content

Instantly share code, notes, and snippets.

@0x49D1
Last active August 2, 2019 08:03
Show Gist options
  • Save 0x49D1/bbc73d9d59aafd16ced01e145c3b34b0 to your computer and use it in GitHub Desktop.
Save 0x49D1/bbc73d9d59aafd16ced01e145c3b34b0 to your computer and use it in GitHub Desktop.
MySQL catch exception and log it into another table for example.
# CREATE PROCEDURE AND OTHER DECLARE STATEMENTS HERE
# ....
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
ROLLBACK;
SET @full_error = CONCAT('ERR:', @errno, '(', @sqlstate, '):', @text);
CALL sp_logaction(@full_error); # Some logging procedure
RESIGNAL;
END;
# PROCEDURE BODY WITH START TRANSACTION & COMMIT HERE
# .....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment