Logging with MYSQL Handlers - mysql

I'm hoping this isn't just duplicating this question.
I have read the documentation but I don't think I fully understand how to use this properly yet.
I would like to catch errors thrown in my stored procedures and log those errors in a table. This is mostly for experimental purposes. I am wondering is there a way to catch any error and then log the code and error to a table.
So far it looks like I have to declare a different handler for each error. Is this correct or is there a way to catch all errors and get the code and message.
For example in each stored procedure I'm declaring a couple of handlers
DECLARE EXIT HANDLER FOR 1062
BEGIN
SELECT 'Attempt to create a duplicate entry occurred';
END;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Unexpected Error Ocurred';
END;
Instead of
SELECT 'custom message';
I want to do
INSERT INTO errorLogs(code, message);
Is this possible without declaring a load of handlers and adding each code manually?
Really appreciate any help pointing me in the right direction.

It looks like this is not really possible until I can use an updated version of MYSQL with DIAGNOSTICS. I found this question which is basically the same as mine. Ah well.

Related

How to read exception details in MYSQL Diagnostic area

I am using Mysql 5.7.14. The get diagnostics statement showing error though i am using 5.7. Sample lines what i am using are something like below where i need to extract error information but unfortunately it couldn't. please help me.
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;#I HAVE TO ROOL BACK A TRASACTION WHEN EVER AN EXCEPTION OCCURS.
GET DIAGNOSTICS CONDITION 1 ERRORSTATE=RETURNED_SQLSTATE,
ERRORNUMBER=MYSQL_ERRNO, ERRORTEXT=MESSAGE_TEXT;
END;

Troubleshooting SQL Stored Procedure

I am seeing a weird issue while executing an SQL stored procedure. Below is the format of the stored proc:
CREATE PROC abc
(
#status int=0 output
)
AS
BEGIN TRY
--Does some transactions
END TRY
BEGIN CATCH
SET #status=-1
END CATCH
return #status
I understand I should have added/logged more details like Error Number, Error Message etc in my catch block but I didnt realize such an issue would occur. Currently, the stored procedure in production sometimes returns a -1 status. The strange part is all the transactions in the try block have been successfully committed. I dont understand then why is the procedure getting into the catch block. I tried to manually execute the procedure with same data it completes successfully and returns status=0. I added extra logging in test environment and tested more than 300 times, but was not able to reproduce the issue. In production I am not allowed to make any changes. Hence, I asked our DBA's to enable trace logging, but that didnt help us much in getting any exception details.
I would really appreciate if there is any way I can troubleshoot this issue. Also are there any options for the DBA's to monitor this procedure and capture the exception

Is there a way to get the line of MySQL code that caused a SQLEXCEPTION in my stored procedure?

I use this code in my stored procedure to get info on a SQLEXCEPTION:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS condition 1
#SQLState = RETURNED_SQLSTATE, #SQLMessage = MESSAGE_TEXT;
SELECT CONCAT('Database error occurred, state - ',#SQLState, '; error msg - ', #SQLMessage) INTO #errorString;
CALL Collectors_Errors
(#errorString,
'Collectors_UpdateGame',
barcodeApp,
usernameApp);
END;
The problem with this code is even though I know what the error is, it doesn't tell me at which line it occurred. And some of my sprocs are very long.
Is there a way to determine which line number, or what the line was, that caused the exception?
You can try the rdebug tool for debugging your stored procedures. I have not used it, but I find the rest of the tools in common_schema to be super helpful.
There may be an easier quick fix for finding exactly where this one error comes from, but in you are working with long stored procedures you may find rdebug to be helpful beyond this one error.

How do i output an SQL Error in MySQL

I've declared an handler which will handle SQLEXCEPTION, in the clean up code i had it do SELECT 'My Handle';, i ran a script that fails because of a primary key violation and it worked cause i got my output.
the problem with DECLARE ... HANDLE FOR SQLEXCEPTION is that when there is an error it will run though it's doesn't say what error triggered it, so i want to output the error
How do i output the SQL Error using a MySQL Query, i don't care if i can only output the error code/id i just need something to output giving me an indication on what the error is so i can fix the problem
EDIT: in case if it's not obvious, this code is in an SQL Procedure
There is nothing in the DECLARE HANDLER documentation on this functionality. You could handle the errors MySQL returns to your application and print or log them that way.
I actually haven't used any handles, but i think you can make your code into a stored procedure and then run the stored procedure!! You might find MySQL accepts what you are trying to do then.

How to catch any exception in triggers and store procedures for mysql?

I have been trying to catch mysql exception especially for triggers and store procedures.How can we catch the exception from mysql side?. I still not found any solution. your help would be appreciate.
Thanks
Hitesh
Because this comes up in the top of my search for MySQL error handling in triggers, I thought I'd share my solution for MySQL 5.5+
My original post: https://stackoverflow.com/a/26115231/1733365 Duplicated below...
Because this article comes up towards the top when I search for error handling in MySQL triggers, I thought I'd share some knowledge.
If there is an error, you can force MySQL to use a SIGNAL, but if you don't specify it as a class as SQLEXCEPTION, then nothing will happen, as not all SQLSTATEs are considered bad, and even then you'd have to make sure to RESIGNAL if you have any nested BEGIN/END blocks.
Alternatively, and probably simpler still, within your trigger, declare an exit handler and resignal the exception.
CREATE TRIGGER `my_table_AINS` AFTER INSERT ON `my_table` FOR EACH ROW
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
RESIGNAL;
DECLARE EXIT HANDLER FOR SQLWARNING
RESIGNAL;
DECLARE EXIT HANDLER FOR NOT FOUND
RESIGNAL;
-- Do the work of the trigger.
END
And if in your body there occurs an error, it will be thrown back up to the top and exit with an error. This can also be used in stored procedures and whatnot.
This works with anything version 5.5+.
Check out the syntax for DECLARE HANDLER
http://dev.mysql.com/doc/refman/5.1/en/declare-handler.html
Also, if you're trying to debug a SP, this might be helpful for you:
http://www.bluegecko.net/mysql/debugging-stored-procedures/