Sample Header Ad - 728x90

How to use user defined exception - PostgreSQL Function

1 vote
1 answer
6166 views
Can you please provide syntax for to use user defined exception in PostgreSQL function? Suppose I want to handle below user defined exception.
SQL Error : ERROR: password is too short.
There are multiple SQLSTATE error code but not able to find what is the SQLSTATE code for this error. I used above which is 22023 but not resolved. We have below code we are able to manage unique violation related exception but not able to manage for "password is too short". could you help me with the syntax? Code:
begin
EXECUTE 'ALTER USER ' || $1 || ' WITH PASSWORD '''|| $2||'''' ;
        EXCEPTION WHEN "Password is too short" 
        THEN RAISE DETAIL 'Please check your password';
        
INSERT INTO pwdhistory (usename,password,changed_on) values($1,md5($2),now());
        EXCEPTION WHEN unique_violation 
        THEN RAISE DETAIL 'Password already used earlier. Please try again with another password.';

end;
Asked by Adam Mulla (143 rep)
Aug 3, 2021, 02:23 PM
Last activity: Aug 4, 2021, 11:05 PM