Issue
I'm using Postgres with Golang via pgx
I've a trigger function something like the following:
CREATE OR REPLACE FUNCTION foo()
RETURNS TRIGGER AS
$$
BEGIN
IF (bar = 'baz') THEN
-- something
ELSE
RAISE EXCEPTION 'oops error';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
How do I check for oops error
in Go code?
The way I'm doing it now is:
errOops := errors.New("ERROR: oops error (SQLSTATE P0001)")
err := myDBFunc()
if errors.Is(err, errOops) {
}
But I wonder if there's a better way other than relying on the hardcoded message.
Solution
Should have read the Wiki: Error Handling in pgx
So I can do:
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "P0001" {
}
or something similar.
Answered By - Abhijit Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.