Hello fellow programmers,
I have a question regarding exception handling.
I have created a function for inserting values into a table with the columns email and username with a max character length of 16 for the email and 32 for the password.
Now I want to give out an error message like 'Password too long,16 Chars max', whenever the inserted email is longer than 16 Chars.
I tried it already with the 'Name_too_long' error code.
How could I do that wiht exceptions in Postgres 10.5 ?
Thanks in advance.
EDIT: CODE that is semi functional
CREATE OR REPLACE FUNCTION users_insert(_email character varying,_passwort character varying) RETURNS void
AS $BODY$
BEGIN
INSERT INTO users(email,passwort,lastlogin)
VALUES(_email,_passwort,CURRENT_TIMESTAMP);
EXCEPTION
WHEN string_data_right_truncation
THEN RAISE NOTICE 'ERROR: INSERT TOO LONG';
END;
$BODY$
LANGUAGE plpgsql ;
EDIT: Working Code, with 2 different error messages, for each error:
CREATE OR REPLACE FUNCTION public.users_insert(
_email character varying,
_passwort character varying)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
BEGIN
IF LENGTH(_passwort) > 16
THEN RAISE EXCEPTION USING errcode = 50001;
END IF;
IF LENGTH(_email) > 32
THEN RAISE EXCEPTION USING errcode = 22001;
END IF;
INSERT INTO users(email,passwort,lastlogin)
VALUES(_email,_passwort,CURRENT_TIMESTAMP);
EXCEPTION
WHEN SQLSTATE '50001' THEN
RAISE NOTICE 'Password too long, 16 Chars max';
WHEN SQLSTATE '22001' THEN
RAISE NOTICE 'Email too long, 32 Chars max';
END;
$BODY$;
ALTER FUNCTION public.users_insert(character varying, character varying)
OWNER TO postgres;
Use an IF condition to check for lengths of specific columns and raise/handle relevant exceptions.
CREATE OR REPLACE FUNCTION users_insert(_email character varying,
_passwort character varying )
RETURNS void
AS $BODY$
BEGIN
IF LENGTH(_passwort) > 16
THEN RAISE EXCEPTION USING errcode = 50001;
END IF;
INSERT INTO users(email,passwort,lastlogin)
VALUES(_email,_passwort,CURRENT_TIMESTAMP);
EXCEPTION
WHEN SQLSTATE '50001' THEN
RAISE NOTICE 'Password too long,16 Chars max';
WHEN OTHERS THEN
raise notice '% %', SQLERRM, SQLSTATE;
END;
$BODY$
LANGUAGE plpgsql ;
Testing
knayak$# PERFORM users_insert('username2251621819101010#mail.com','password123passwod');
knayak$#
knayak$# END $$;
NOTICE: Password too long,16 Chars max
DO
knayak=# DO $$
knayak$# BEGIN
knayak$#
knayak$# PERFORM users_insert('username2251621819101010#mail.com','password');
knayak$#
knayak$# END $$;
NOTICE: value too long for type character varying(30) 22001
DO
Related
Trying to save data into table using procedures and loop, but unable to solve this error.
CREATE OR REPLACE PROCEDURE quality_save(p_1 character varying, p_2 character varying, p_3 character varying DEFAULT NULL::character varying, INOUT response character varying DEFAULT '1'::character varying)
LANGUAGE plpgsql
AS $procedure$
declare
begin
if 1=1 then
raise notice '_Insert start';
insert into table_A
(
brand,
model,
year
)
values(
p_1,
p_2,
p_3
);
raise notice 'insert-end';
else
select 'p_1,p_2 cannot be null' into response;
end if;
exception
when sqlstate '23505' then
select 'Duplicate Record' into response;
--when others then
-- select '-1' into response;
end
$procedure$
********************************************************************************************
CREATE OR REPLACE PROCEDURE auto_save(INOUT response character varying DEFAULT '1'::character varying)
LANGUAGE plpgsql
AS $procedure$
declare
response varchar(100);
f record;
l record;
m record;
begin
for f in select p_1,p_2,p_3 from table_dump
loop
call public.quality_save(
p_1 =>f.p_1,
p_2 =>f.p_2,
p_3 =>f.p_3
)
;
select 1 into response;
end loop;
select 1 into response;
end;$procedure$
;
SQL Error [42601]: ERROR: procedure parameter "response" is an output parameter but corresponding argument is not writable
Where: PL/pgSQL function auto_save(character varying) line 182 at CALL
I tried rewriting second procedure as function but still giving the same error.
You didn't pass an argument for response. Call the procedure like this:
DECLARE
f record;
p_response character varying;
BEGIN
[...]
CALL public.quality_save(
p_1 =>f.p_1,
p_2 =>f.p_2,
p_3 =>f.p_3,
response => p_response
);
[...]
I'm trying to understand the exception handling in PL/pgSQL but ...
Case 1:
CREATE OR REPLACE PROCEDURE ins ()
AS $$ DECLARE
i INT;
BEGIN
insert into scott.emp (empno) values (9000);
commit;
i:=1/0;
EXCEPTION WHEN OTHERS THEN
--rollback;
END $$ LANGUAGE plpgsql;
call ins();
I would guess the line is inserted, but it is not
CASE 2:
CREATE OR REPLACE PROCEDURE ins ()
AS $$ DECLARE
i INT;
BEGIN
insert into scott.emp (empno) values (9000);
commit;
--i:=1/0;
EXCEPTION WHEN OTHERS THEN
--rollback;
END $$ LANGUAGE plpgsql;
call ins();
Now, there is no div by zero , but alos no line in the table
Case 3:
CREATE OR REPLACE PROCEDURE ins ()
AS $$ DECLARE
i INT;
BEGIN
insert into scott.emp (empno) values (9000);
commit;
--i:=1/0;
--EXCEPTION WHEN OTHERS THEN
--rollback;
END $$ LANGUAGE plpgsql;
call ins();
Now the line is inserted. It seems that the Problen is my Exception Block, but why?
Thanks in advance.
Marco
Silently throwing away your error message certainly does make debugging difficult.
CREATE OR REPLACE PROCEDURE ins ()
AS $$ DECLARE
i INT;
BEGIN
insert into scott.emp (empno) values (9000);
commit;
--i:=1/0;
EXCEPTION WHEN OTHERS THEN
raise notice E'%', SQLERRM;
END $$ LANGUAGE plpgsql;
Shows:
NOTICE: cannot commit while a subtransaction is active
So this due to the documented limitation:
A transaction cannot be ended inside a block with exception handlers.
I'm new to PostgreSQL. I have experience in oracle. In oracle , to find the exact error, I use code 'dbms_output.put_line(sqlerrm)' . Here I have a postgresql function returning an integer value
CREATE OR REPLACE FUNCTION public.fn_sqltest(
p_id integer)
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
Declare
n integer;
begin
select off_id into n from office
where per_id=p_id;
return n ;
exception when others then
return -1;
end;
$BODY$;
ALTER FUNCTION public.fn_sqltest(character varying)
OWNER TO postgres;
I call this function as below
DO $$
DECLARE
ae integer;
BEGIN
ae:=fn_sqltest(10);
RAISE NOTICE 'exception: % % ', sqlstate , sqlerrm ;
RAISE NOTICE 'Return value is: % ', ae;
END $$;
and I get the error
ERROR: column "sqlstate" does not exist
How can I show the exact error message like sqlerrm in oracle.
I updated the function and wrote a code in function exception section
CREATE OR REPLACE FUNCTION public.fn_sqltest(
p_id integer)
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
Declare
n integer;
text_var1 text;
text_var2 text;
text_var3 text;
begin
select off_id into n from office
where per_id=p_id;
return n ;
exception when others then
GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,
text_var2 = PG_EXCEPTION_DETAIL,
text_var3 = PG_EXCEPTION_HINT;
RAISE NOTICE 'Return value is: % % %',text_var1 , text_var2, text_var3;
return -1;
end;
$BODY$;
ALTER FUNCTION public.fn_sqltest(character varying)
OWNER TO postgres;
Another method is by changing the return type. I changed the return type to text and rewrite the exception section code as below
return sqlerrm;
Unlike PL/SQL, in PL/pgSQL SQLSTATE and SQLERRM are not defined outside an exception handler. See the documentation, section "Obtaining Information About An Error".
This also means that you can't get SQLSTATE and SQLERRM of a successful operation, unlike PL/SQL.
So, if you want to use these special variables outside an exception handler, you have to store them in variables.
I don't know how would you like to return them from the function. I can demonstrate this idea inside your function code:
CREATE OR REPLACE FUNCTION public.fn_sqltest(
p_id integer)
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
Declare
n integer;
v_sqlerrm text;
v_sqlstate text;
begin
begin
select off_id into n from office
where per_id=p_id;
return n ;
exception when others then
v_sqlerrm := sqlerrm;
v_sqlstate := sqlstate;
end;
RAISE NOTICE 'exception: % % ', v_sqlstate , v_sqlerrm ;
return -1;
end;
$BODY$;
I know this is old but just for the record: The easiest way to output the state and message of error is to raise them from within the exception clause. You don't need to declare extra variables.
CREATE OR REPLACE FUNCTION fn_sqltest(p_id integer)
RETURNS integer
AS $$
DECLARE
n integer;
BEGIN
SELECT off_id
INTO n
FROM office
WHERE per_id = p_id;
RETURN n;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'exception: % - %', SQLSTATE, SQLERRM;
RETURN -1;
END;
$$ LANGUAGE plpgsql;
I am trying to make the following function working:
CREATE OR REPLACE FUNCTION validate_count(devices TEXT[], campaign_id INTEGER) RETURNS void AS $$
DECLARE
devices_array TEXT[] := devices;
devices_count INTEGER := array_length(devices, 1);
row_id INTEGER := campaign_id;
BEGIN
FOR device IN unnest(devices_array) LOOP
IF my_count('my_table', device, row_id) != 1;
RAISE EXCEPTION 'invalid_count %', row_id
ENDIF
END LOOP;
END
$$ LANGUAGE plpgsql;
my_count is a working function which returns INTEGER.
The definition fails with the error:
ERROR: syntax error at or near "unnest"
LINE 7: FOR device IN unnest(devices_array) LOOP
Could you spot the issue? Thanks!
I am planning to call the function as follows:
select validate_count('{foo, bar}', 1)
Use a FOREACH loop:
CREATE OR REPLACE FUNCTION validate_count(devices TEXT[], campaign_id INTEGER)
RETURNS void
AS
$$
DECLARE
device text;
devices_count INTEGER := array_length(devices, 1);
BEGIN
FOREACH device IN ARRAY devices LOOP
IF my_count('my_table', device, campaign_id) <> 1 then
RAISE EXCEPTION 'invalid_count %', campaign_id;
END IF;
END LOOP;
END
$$
LANGUAGE plpgsql;
A few things need to be fixed to make this valid.
The FOR loop needs SELECT before the unnest, i.e.:
FOR device IN SELECT unnest(devices_array) LOOP
You need to declare device up top, for example:
DECLARE device RECORD;
The IF statement needs a THEN instead of a ;, i.e.:
IF my_count('my_table', device, row_id) != 1 THEN
The RAISE EXCEPTION statement needs a semicolon at the end of the line, i.e.:
RAISE EXCEPTION 'invalid_count %', row_id;
The END for the IF statement should be END IF;.
The END for the LOOP statement should be END LOOP;.
Here's the net result:
CREATE OR REPLACE FUNCTION validate_count(devices TEXT[], campaign_id INTEGER) RETURNS void AS $$
DECLARE
devices_array TEXT[] := devices;
devices_count INTEGER := array_length(devices, 1);
row_id INTEGER := campaign_id;
device RECORD;
BEGIN
FOR device IN SELECT unnest(devices_array) LOOP
IF my_count('my_table', device, row_id) != 1 THEN
RAISE EXCEPTION 'invalid_count %', row_id;
END IF;
END LOOP;
END
$$ LANGUAGE plpgsql;
How to raise error from PostgreSQL SQL statement if some condition is met?
I tried code below but got error.
CREATE OR REPLACE FUNCTION "exec"(text)
RETURNS text AS
$BODY$
BEGIN
EXECUTE $1;
RETURN $1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
-- ERROR: syntax error at or near "raise"
-- LINE 1: raise 'test'
SELECT exec('raise ''test'' ') WHERE TRUE
In real application TRUE is replaced by some condition.
Update
I tried to extend answer to pass exception message parameters.
Tried code below but got syntax error.
How to pass message parameters ?
CREATE OR REPLACE FUNCTION exec(text, variadic )
RETURNS void LANGUAGE plpgsql AS
$BODY$
BEGIN
RAISE EXCEPTION $1, $2;
END;
$BODY$;
SELECT exec('Exception Param1=% Param2=%', 'param1', 2 );
You cannot call RAISE dynamically (with EXECUTE) in PL/pgSQL - that only works for SQL statements, and RAISE is a PL/pgSQL command.
Use this simple function instead:
CREATE OR REPLACE FUNCTION f_raise(text)
RETURNS void
LANGUAGE plpgsql AS
$func$
BEGIN
RAISE EXCEPTION '%', $1;
END
$func$;
Call:
SELECT f_raise('My message is empty!');
Related:
Generate an exception with a Context
Additional answer to comment
CREATE OR REPLACE FUNCTION f_raise1(VARIADIC text[])
RETURNS void
LANGUAGE plpgsql AS
$func$
BEGIN
RAISE EXCEPTION 'Reading % % %!', $1[1], $1[2], $1[3];
END
$func$;
Call:
SELECT f_raise1('the','manual','educates');
VARIADIC is not a data type, but an argument mode.
Elements have to be handled like any other array element.
To use multiple variables in a RAISE statement, put multiple % into the message text.
The above example will fail if no $3 is passed. You'd have to assemble a string from the variable number of input elements. Example:
CREATE OR REPLACE FUNCTION f_raise2(VARIADIC _arr text[])
RETURNS void
LANGUAGE plpgsql AS
$func$
DECLARE
_msg text := array_to_string(_arr, ' and '); -- simple string construction
BEGIN
RAISE EXCEPTION 'Reading %!', _msg;
END
$func$;
Call:
SELECT f_raise2('the','manual','educates');
I doubt you need a VARIADIC parameter for this at all. Read the manual here.
Instead, define all parameters, maybe add defaults:
CREATE OR REPLACE FUNCTION f_raise3(_param1 text = ''
, _param2 text = ''
, _param3 text = 'educates')
RETURNS void
LANGUAGE plpgsql AS
$func$
BEGIN
RAISE EXCEPTION 'Reading % % %!', $1, $2, $3;
END
$func$;
Call:
SELECT f_raise3('the','manual','educates');
Or:
SELECT f_raise3(); -- defaults kick in