1

I'm trying to find out is it possible in ANY way to exit a trigger procedure (return/raise exception, whatever) with a JSON output. I want to do something like this:

CREATE FUNCTION users_do_before_insert() RETURNS TRIGGER AS
  $$
  BEGIN
    IF (NEW.username = '' OR NEW.full_name = '' OR NEW.email = '') THEN
      RAISE EXCEPTION json_build_object('error', 'All fields are mandatory.');
    END IF;

    RETURN NEW;
  END;
  $$
LANGUAGE 'plpgsql';

CREATE TRIGGER users_bi_trigger
  BEFORE INSERT ON users
  FOR EACH ROW
  EXECUTE PROCEDURE users_do_before_insert();

So, the expected output would be:

{"error" : "All fields are mandatory."}

I would appreciate any help with this. Thanks in advance!

1 Answer 1

2

RAISE statement requires format string or USING clause

RAISE EXCEPTION '%', json_build_object(..);
RAISE EXCEPTION USING MESSAGE=json_build_object(..);
Sign up to request clarification or add additional context in comments.

1 Comment

God bless ya! Thanks

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.