I think what you are looking for lies here : https://www.postgresql.org/docs/11/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING
So if you want to have details about an error you may treat your transactions as follows:
DECLARE
text_var1 text;
text_var2 text;
text_var3 text;
BEGIN
-- some processing which might cause an exception
...
EXCEPTION WHEN OTHERS THEN
GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,
text_var2 = PG_EXCEPTION_DETAIL,
text_var3 = PG_EXCEPTION_HINT;
END;
But if you still want to have the corresponding error designation based on the error code (https://www.postgresql.org/docs/current/errcodes-appendix.html), then you can build a table with all the infos in it (PG_ERROR_CODES(id varchar(64), description varchar(255))) and interrogate it with your function.