Handling PostgreSQL trigger exceptions in a DELETE request in Spring Boot

I’m experiencing an issue with my Spring Boot web application when trying to handle exceptions thrown by a PostgreSQL BEFORE DELETE trigger during a DELETE request.

Here is the situation: When a DELETE request is made, a PostgreSQL trigger checks whether the item can be deleted. If it cannot be deleted, the trigger raises an exception. This exception should be caught by a GlobalExceptionHandler in Spring to display an appropriate error message on the front-end.

When I test the DELETE request using Postman, it works correctly and I receive the custom error message from PostgreSQL.

However, when the same request is made from the front-end, I get the original error message “Error while committing the transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction” instead of the detailed error message.

Trigger:

CREATE OR REPLACE FUNCTION iot.f_bd_reset_mepo_references()
    RETURNS trigger
    LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
    haveRule integer := 0;
    haveEchart integer := 0;
    haveAlarm boolean := false;
    ruleName character varying (20);
    echartName character varying (20);

BEGIN
    SELECT COUNT(*), cond_is_active INTO haveRule, haveAlarm 
    FROM condition 
    WHERE cond_mepo_code = OLD.mepo_code AND client_code = OLD.client_code 
    GROUP BY cond_is_active;
    
    SELECT r.rule_code INTO ruleName
    FROM condition c
    JOIN event e ON c.cond_event_code = e.pk_event
    JOIN rule r ON e.pk_event = r.rule_event
    WHERE c.cond_mepo_code = OLD.mepo_code AND r.client_code = OLD.client_code; 

    SELECT COUNT(*), echart_code INTO haveEchart, echartName 
    FROM echartconfig 
    WHERE echart_pk_mepo = OLD.pk_measure_point AND client_code = OLD.client_code 
    GROUP BY echart_code;

    IF (haveAlarm = TRUE) THEN
        RAISE EXCEPTION 'Unable to delete measurement point because the alarm "%" is in progress.', ruleName;
    ELSIF (haveRule > 0) THEN
        RAISE EXCEPTION 'Unable to delete measurement point because it is referenced to the rule "%"', ruleName;
    ELSIF (haveEchart > 0) THEN
        RAISE EXCEPTION 'Unable to delete measurement point because it is referenced to the graph "%"', echartName;
    ELSE
        DELETE FROM alarm WHERE alarm_mepo_code = OLD.pk_measure_point AND client_code = OLD.client_code;
        DELETE FROM echartconfig WHERE echart_pk_mepo = OLD.pk_measure_point AND client_code = OLD.client_code;    
        DELETE FROM feedback WHERE sens_code = OLD.mepo_code AND client_code = OLD.client_code;    
    END IF;
    
    RETURN OLD;
END; 
$BODY$;

Global Exception Handler

import org.postgresql.util.PSQLException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(PSQLException.class)
    public ResponseEntity<?> handlePSQLException(PSQLException ex, WebRequest request) {
        String message = ex.getServerErrorMessage().getMessage();
        return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
    }

}
  1. Why does the custom error message from PostgreSQL not get propagated correctly to the front-end but works fine in Postman?
  2. Is there a better way to handle PostgreSQL trigger exceptions in Spring Boot to ensure that the error messages are correctly shown on the front-end?

5

Thanks to comments above from Andrei Lisa, I changed the approach and now it is working.

Trigger

CREATE OR REPLACE FUNCTION iot.f_bd_reset_mepo_references()
    RETURNS trigger
    LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
    haveRule integer := 0;
    haveEchart integer := 0;
    haveAlarm boolean := false;
BEGIN
    SELECT COUNT(*), cond_is_active INTO haveRule, haveAlarm
    FROM condition 
    WHERE cond_mepo_code = OLD.mepo_code AND client_code = OLD.client_code 
    GROUP BY cond_is_active;

    SELECT COUNT(*) INTO haveEchart
    FROM echartconfig
    WHERE echart_pk_mepo = OLD.pk_measure_point AND client_code = OLD.client_code
    GROUP BY echart_code;

    IF haveAlarm THEN
        RAISE EXCEPTION SQLSTATE 'P0001';
    ELSIF haveRule > 0 THEN
        RAISE EXCEPTION SQLSTATE 'P0002';
    ELSIF haveEchart > 0 THEN
        RAISE EXCEPTION SQLSTATE 'P0003';
    ELSE
        DELETE FROM alarm WHERE alarm_mepo_code = OLD.pk_measure_point AND client_code = OLD.client_code;
        DELETE FROM echartconfig WHERE echart_pk_mepo = OLD.pk_measure_point AND client_code = OLD.client_code;    
        DELETE FROM feedback WHERE sens_code = OLD.mepo_code AND client_code = OLD.client_code;
        RETURN OLD;
    END IF;
END;
$BODY$;

Global Exception Handler

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

import java.sql.SQLException;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLException.class)
    public ResponseEntity<?> handleSQLException(SQLException ex, WebRequest request) {
        String sqlState = ex.getSQLState();
        String customMessage = mapErrorMessage(sqlState);
        return new ResponseEntity<>(customMessage, HttpStatus.BAD_REQUEST);
    }

    private String mapErrorMessage(String sqlState) {
        switch (sqlState) {
            case "P0001":
                return "Unable to delete this measurement point because there is an alarm in progress.";
            case "P0002":
                return "Unable to delete this measurement point because it is referenced to a rule.";
            case "P0003":
                return "Unable to delete this measurement point because it is referenced to a graph.";
            case "P0004":
                return "Unable to delete this rule because there is an alarm in progress.";
            default:
                return "An unknown error occurred.";
        }
    }
    
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật