Astuces pour gérer les exceptions imbriquées

Transactions et gestion des erreurs dans PostgreSQL

Jason Myers

Principal Engineer

Simuler des points de sauvegarde avec des blocs imbriqués

DO $$
BEGIN
    -- Bloc 1
    BEGIN
        UPDATE inventory SET cost = 2.33 WHERE name = 'Linga';
        UPDATE inventory SET cost = 2.33 WHERE name = 'Petit-Beurre';
    EXCEPTION
    WHEN others THEN
       INSERT INTO errors (msg) VALUES ('Max cost is 10!');
       RAISE INFO 'Max cost is 10!';
    END; 
Transactions et gestion des erreurs dans PostgreSQL

Simulation du point de sauvegarde : Bloc 2

-- Bloc 2
    BEGIN
        UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
    EXCEPTION
    WHEN others THEN
       INSERT INTO errors (msg) VALUES ('Max cost is 10!');
       RAISE INFO 'Max cost is 10!';
    END; 
END;
$$ language 'plpgsql';
Transactions et gestion des erreurs dans PostgreSQL

Blocs imbriqués avec diagnostics empilés

DO $$
DECLARE
   exc_message text;
   exc_detail text;
   exc_context text;
BEGIN
    -- Bloc 1
    BEGIN
        UPDATE inventory SET cost = 2.33 WHERE name = 'Linga';
        UPDATE inventory SET cost = 2.33 WHERE name = 'Petit-Beurre';
    EXCEPTION
    WHEN others THEN
        GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                                exc_detail = PG_EXCEPTION_DETAIL,
                                exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
    -- Bloc 2
    BEGIN
        UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
    EXCEPTION
    WHEN others THEN
       GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                               exc_detail = PG_EXCEPTION_DETAIL,
                               exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
END$$;
Transactions et gestion des erreurs dans PostgreSQL

Blocs imbriqués avec diagnostics empilés

DO $$
DECLARE
   exc_message text;
   exc_detail text;
   exc_context text;
BEGIN
Transactions et gestion des erreurs dans PostgreSQL

Diagnostics empilés : bloc 2

    -- Bloc 1
    BEGIN
        UPDATE inventory SET cost = 2.33 WHERE name = 'Linga';
        UPDATE inventory SET cost = 2.33 WHERE name = 'Petit-Beurre';
    EXCEPTION
    WHEN others THEN
        GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                                exc_detail = PG_EXCEPTION_DETAIL,
                                exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
Transactions et gestion des erreurs dans PostgreSQL

Diagnostics empilés : bloc 2

-- Bloc 2
    BEGIN
        UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
    EXCEPTION
    WHEN others THEN
       GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                               exc_detail = PG_EXCEPTION_DETAIL,
                               exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
END$$;
Transactions et gestion des erreurs dans PostgreSQL

Résultats

INFO:  Message: new row for relation "inventory" violates check constraint 
"cost_check" | Details: Failing row contains (7, 35, Macaron). | Context SQL 
statement "UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron'"
PL/pgSQL function inline_code_block line 23 at SQL statement
DO
postgres=# \x on
Expanded display is on.
postgres=# select * from errors;
-[ RECORD 1 ]---------------------------------------------------------------------
error_id | 15
state    |
msg      | new row for relation "inventory" violates check constraint "cost_check"
detail   | Failing row contains (7, 35, Macaron).
context  | SQL statement "UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron'"+
         | PL/pgSQL function inline_code_block line 23 at SQL statement
Transactions et gestion des erreurs dans PostgreSQL

Gestion d'exceptions personnalisée vs diagnostics empilés

Personnalisé
  • Contexte d'erreur clair
  • Condition d'erreur prévue
  • Message d'erreur standard trop générique
Diagnostics empilés
  • Besoin de plus de contexte sur l'erreur
  • Nombreuses conditions d'erreur possibles
  • Débogage
  • Généraliser la gestion des exceptions
Transactions et gestion des erreurs dans PostgreSQL

Passons à la pratique !

Transactions et gestion des erreurs dans PostgreSQL

Preparing Video For Download...