2

i am using postgrest exception in function 'public.main_function'. in exception 'others', i also use sub functions to save my log data.

but my sub function 'public.something_went_wrong_log_creation' might be error sometimes.

how can i add exception (nested exception) in exception 'others' below?

    CREATE OR REPLACE FUNCTION public.main_function(request json)
  RETURNS integer AS
$BODY$

BEGIN
    -- statement 1
    -- statement 2
    -- statement 3

    RETURN 1;

EXCEPTION
    -- SOMETHING WENT WRONG
    WHEN others
    THEN
        -- LOG SOMETHING WENT WRONG
        PERFORM public.something_went_wrong_log_creation();

        RETURN 0;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE

    CREATE OR REPLACE FUNCTION public.something_went_wrong_log_creation()
  RETURNS integer AS
$BODY$

BEGIN
    -- statement 1
    -- statement 2
    -- statement 3

    RETURN 1;

EXCEPTION
        -- SOMETHING WENT WRONG
        WHEN others
        THEN
            RETURN 0;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE

1 Answer 1

4

You can use subblock and wrap your exception handling code in it.

CREATE OR REPLACE FUNCTION public.main_function(request json)
  RETURNS integer AS
$BODY$

BEGIN
    -- statement 1
    -- statement 2
    -- statement 3

    RETURN 1;

EXCEPTION
    -- SOMETHING WENT WRONG
    WHEN others
    THEN
        BEGIN
          -- LOG SOMETHING WENT WRONG
          PERFORM public.something_went_wrong_log_creation();

          RETURN 0;
        EXCEPTION
             WHEN others
             THEN
               RETURN -1;
        END;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.