In FirebirdSql, how to return exception message from procedure
Asked Answered
P

2

7

I want to return the error message from a procedure when an exception happens. In SQL Server you would select the Error_Number() and Error_Message(). How would I do it in FirebirdSql

SET TERM ^ ;

CREATE PROCEDURE sprocname
 ( id int ) 
RETURNS 
 ( gcode int, errmsg varchar(250) )
AS 
BEGIN
  gcode = 0;
  errmsg = '';
  -- do procedure code here


  WHEN ANY DO
  BEGIN 
    gcode = gdscode; -- ??
    errmsg = ??;
  END
  SUSPEND;
END^

SET TERM ; ^
Penny answered 12/12, 2013 at 21:59 Comment(2)
You could simply not catch the exception, it will bubble to the caller.Mylor
I want to format the message and error code. Its for an intranet web appPenny
M
5

Unfortunately, you will need to do that at the client side, as it is currently not possible to obtain this in PSQL. There is a feature request in the Firebird tracker, which has been implemented for Firebird 4, which is expected to be released in 2019.

See Firebird 4 release notes, section System Function RDB$ERROR():

The function RDB$ERROR() takes a PSQL error context as input and returns the specific context of the active exception. Its scope is confined to the context of the exception-handling block in PSQL. Outside the exception handling block, RDB$ERROR always contains NULL.

The type of the return value depends on the context.

Syntax Rules

RDB$ERROR ( context )
context ::= { GDSCODE | SQLCODE | SQLSTATE | EXCEPTION | MESSAGE }

[..]

Example

BEGIN
  ...
WHEN ANY DO
  EXECUTE PROCEDURE P_LOG_EXCEPTION(RDB$ERROR(MESSAGE));
END
Mylor answered 16/12, 2013 at 21:29 Comment(1)
Well, I sure wish your's wasn't the correct answer, but since it is, for now, I'm going to give you credit for itPenny
T
0
CREATE PROCEDURE ADD_COUNTRY (
    ACountryName COUNTRYNAME,
    ACurrency VARCHAR(10) )
AS
BEGIN
  INSERT INTO country (country,
                       currency)
  VALUES (:ACountryName,
          :ACurrency);
  WHEN ANY DO
  BEGIN
        -- write an error in log
    IN AUTONOMOUS TRANSACTION DO
      INSERT INTO ERROR_LOG (PSQL_MODULE,
                             GDS_CODE,
                             SQL_CODE,
                             SQL_STATE)
      VALUES ('ADD_COUNTRY',
              GDSCODE,
              SQLCODE,
              SQLSTATE);
    -- Re-throw exception
    EXCEPTION;
  END
END

http://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-psql-handleexceptions.html

Turpin answered 28/10, 2016 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.