frollo
frollo

Reputation: 1374

Postgresql "invalid end sequence"

I've a function which adds users on my application. It does a few check, salts and hashes the password and then inserts the values. Now, when i run the function I get

ERROR: invalid end sequence

(to be honest, i get it in italian and had to do quite a job to find the corresponding english string). Now, the single parts work very well alone, but if I put everything together in a function I get the error so I'm pretty out of ideas. Any suggestion?

Upvotes: 4

Views: 6430

Answers (3)

yurenchen
yurenchen

Reputation: 2493

if not sure input data correct,
 can catch the decode() exception, and return err value other than throw exception.

custom function

decode with exception catch

-------- try decode, ret null if err
CREATE OR REPLACE FUNCTION decode_null(str text, fmt text) RETURNS text AS $$
BEGIN
  RETURN decode(str, fmt);
EXCEPTION
  WHEN OTHERS THEN
    RAISE NOTICE USING
       MESSAGE = format('--decode_null: SQLSTATE %s, MSG: %s', SQLSTATE, SQLERRM);
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;

test

select decode_null('x', 'base64');
--decode_null: SQLSTATE 22023, MSG: invalid end sequence

select decode_null('_', 'base64');
--decode_null: SQLSTATE 22023, MSG: invalid symbol

Upvotes: 0

Oberdan
Oberdan

Reputation: 321

try also

case 
  when char_length(sequence) in (6, 10, 14) then decode(sequence::text || '==', 'base64'::text)::character varying 
  when char_length(sequence) in (7, 11, 15) then decode(sequence::text || '=', 'base64'::text)::character varying 
  when char_length(sequence) in (8, 9, 12, 13) then decode(sequence::text, 'base64'::text)::character varying 
  else null
end AS sequence,

Upvotes: 2

Daniel Vérité
Daniel Vérité

Reputation: 61636

This error happens when trying to decode incorrectly encoded base64 contents. Example:

=> select decode('aa', 'base64');

ERROR: invalid end sequence

as opposed to:

=> select decode('aa==', 'base64');
 decode 
--------
 \x69
(1 row)

Upvotes: 5

Related Questions