title: PostgreSQL: Return a trigger error (raise exception) in JSON format
body: <p>I'm trying to find out is it possible in <em>ANY</em> way to exit a trigger procedure (return/raise exception, whatever) with a JSON output. I want to do something like this:</p>
  
  <pre class="lang-sql prettyprint-override"><code>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();
  </code></pre>
  
  <p>So, the expected output would be:</p>
  
  <pre><code>{"error" : "All fields are mandatory."}
  </code></pre>
  
  <p>I would appreciate <strong>any</strong> help with this. Thanks in advance!</p>