Getting the exception message in when... section (there is gdscode context variable, but no other variable)

20 views
Skip to first unread message

Alexander Skara

unread,
Nov 3, 2025, 3:08:38 AM (4 days ago) Nov 3
to firebird-support
Hi!

I know how to read gdscode during the exception handling: Reference manual on context variable GDSCODE has this example https://www.firebirdsql.org/file/documentation/html/en/refdocs/fblangref30/firebird-30-language-reference.html#fblangref30-contextvars-gdscode

when gdscode grant_obj_notfound, gdscode grant_fld_notfound, gdscode grant_nopriv, gdscode grant_nopriv_on_base 
do 
  begin 
     execute procedure log_grant_error(gdscode); exit; 
   end

But can I somehow get message text of the exception. The SQL procedures and triggers of my database raises exceptions in the form:

msg = 'Some serious error in document id '||:id;
execute procedure my_exception(:msg);

where the declaration is:

create or alter procedure my_exception (
    MSG varchar(400))
as
BEGIN
  exception FREEEXCEPTION substring(:MSG from 1 for 74);
END

Of course, FREEEXCEPTION is already declared exception, available in rdb$exceptions table with some initial text. I have not tested so far whether I can read the actual message text with the code:

select rdb$message
  from rdb$exceptions
  where rdb$exception_name='FREEEXCEPTION'
  into :current_exception

but I guess: 'exception exception_name custom_text' is not changing rdb$exceptions record, it could lead to lot of deadlock conflicts.

And I have checked the Firebird 3.0 and Firebird 5.0 references manuals (context variable sections) and there were no context variables of the exception message either.

So - is it possible to get the message text in when... section?

Regards, Alex

Alexander Skara

unread,
Nov 3, 2025, 3:15:24 AM (4 days ago) Nov 3
to firebird-support
I am thinking, that there can by my private table my$message(transaction_id bigint, message varchar(100)) and the my_exception could insert message here based with current_transaction as transaction_id. And the when... any section can read that value... Well that can work, but that require modifying such core procedure my_exception for only one user case where such message text is needed in when any... section. Maybe there is less custom solution?

Dimitry Sibiryakov

unread,
Nov 3, 2025, 3:32:19 AM (4 days ago) Nov 3
to firebird...@googlegroups.com
Alexander Skara wrote 03.11.2025 9:08:
> And I have checked the Firebird 3.0 and Firebird 5.0 references manuals (context
> variable sections) and there were no context variables of the exception message
> either.

You had to check WHEN section.

https://firebirdsql.org/file/documentation/chunk/en/refdocs/fblangref50/fblangref50-scalarfuncs-other.html#fblangref50-scalarfuncs-rdberror

--
WBR, SD.

Dmitry Yemanov

unread,
Nov 3, 2025, 3:38:09 AM (4 days ago) Nov 3
to firebird...@googlegroups.com
In Firebird 4.0 and newer you may use RDB$ERROR(MESSAGE).


Dmitry

Alexander Skara

unread,
Nov 3, 2025, 4:15:32 AM (4 days ago) Nov 3
to firebird-support
Yes, thx. That would solve. But I am stuck with Firebird 2.1.

So - I am thinking about solution:

create or alter procedure my_exception (
    MSG varchar(400))
as
BEGIN
  rdb$set_contex('USER_TRANSACTION', 'LAST_EXCEPTION_MSG', :msg);

  exception FREEEXCEPTION substring(:MSG from 1 for 74);
END

and

when gdscode grant_obj_notfound, gdscode grant_fld_notfound, gdscode grant_nopriv, gdscode grant_nopriv_on_base 
do 
  begin 
     execute procedure log_grant_error(gdscode); exit; 
     insert into my_log(error_message, ...) values (rdb$get_context('USER_TRANSACTION', 'LAST_EXCEPTION_MSG'), ...);
   end

But is it safe? Can there any be deadlocks, any caveats?
Reply all
Reply to author
Forward
0 new messages