2

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; 
2
  • You may be interested in the Security.SE quoestion Should I have a maximum password length? Commented Nov 19, 2018 at 8:13
  • @KaushikNayak I have edited the code to my post. My Problem now is, that it gives out an error, but I cant "choose" between two different errors. I am trying to get one for a too long password insert and one for an too Long email insert. I want to create for each error a distinct message. Commented Nov 19, 2018 at 8:24

1 Answer 1

4

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('[email protected]','password123passwod');
knayak$#
knayak$# END $$;
NOTICE:  Password too long,16 Chars max
DO
knayak=# DO $$
knayak$# BEGIN
knayak$#
knayak$# PERFORM users_insert('[email protected]','password');
knayak$#
knayak$# END $$;
NOTICE:  value too long for type character varying(30) 22001
DO
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this is the answer I was looking for, I failed to create the If condition myself, due to a minor syntax mistake, which I couldn't figure out!

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.