pg-mem icon indicating copy to clipboard operation
pg-mem copied to clipboard

Fixed bug that prevents constraints violations to fire "query-failed" events

Open nachovigilante opened this issue 9 months ago • 0 comments

The bug is best described in #459.

When reading the code to find the bug's origin I found that event firing was not tested at all. So I added tests for all global events.

Then, I found where the bug was coming from: the method DbSchema.prepare() is the place in the codebase that raises global events. This seems right since when a query is executed, it eventually gets prepared. The problem is that if the query does not fail to get prepared but it does fail when executed, the error is not catched thus the "query-failed" event is not risen.

To fix this issue, I simply moved up the try/catch to the DbSchema.query() method:

query(text: QueryOrAst): QueryResult {
        try {
            return this.prepare(text)
                .bind()
                .executeAll();
        } catch (e) {
            this.db.raiseGlobal('query-failed', text);
            throw e;
        }
    }

With this change, when a query fails to be executed, the "query-failed" event fires too.

nachovigilante avatar Jun 27 '25 20:06 nachovigilante