Skip to content

Commit 35909c0

Browse files
authored
Merge pull request #6732 from cylc/8.4.x-sync
🤖 Merge 8.4.x-sync into master
2 parents 32f9951 + 53a268d commit 35909c0

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

changes.d/6730.feat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add SQLite detailed error codes to error logs

cylc/flow/rundb.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,22 +505,46 @@ def execute_queued_items(self):
505505
# something went wrong
506506
# (includes DB file not found, transaction processing issue, db locked)
507507
except sqlite3.Error as e:
508+
# Detailed error codes are only available for python >= 3.11
509+
if hasattr(e, "sqlite_errorcode"):
510+
error_code = str(e.sqlite_errorcode)
511+
else:
512+
error_code = "Not available"
513+
514+
if hasattr(e, "sqlite_errorname"):
515+
error_name = e.sqlite_errorname
516+
else:
517+
error_name = "Not available"
518+
508519
if not self.is_public:
509520
# incase this isn't a filesystem issue, log the statements
510521
# which make up the transaction to assist debug
511522
LOG.error(
512-
'An error occurred when writing to the database,'
523+
'An error occurred when writing to the database %(file)s,'
513524
' this is probably a filesystem issue.'
514-
f' The attempted transaction was:\n{pformat(sql_queue)}'
525+
' The error was: %(error)s'
526+
' SQLite error code: %(error_code)s'
527+
' SQLite error name: %(error_name)s'
528+
' The attempted transaction was:\n %(transaction)s' % {
529+
"file": self.db_file_name,
530+
"error": str(e),
531+
"error_code": error_code,
532+
"error_name": error_name,
533+
"transaction": pformat(sql_queue)
534+
}
515535
)
516536
raise
517537
self.n_tries += 1
518538
LOG.warning(
519539
"%(file)s: write attempt (%(attempt)d)"
520-
" did not complete: %(error)s\n" % {
540+
" did not complete: %(error)s\n"
541+
" SQLite error code: %(error_code)s\n"
542+
" SQLite error name: %(error_name)s" % {
521543
"file": self.db_file_name,
522544
"attempt": self.n_tries,
523-
"error": str(e)
545+
"error": str(e),
546+
"error_code": error_code,
547+
"error_name": error_name
524548
}
525549
)
526550
if self.conn is not None:
@@ -565,15 +589,15 @@ def _execute_stmt(self, stmt, stmt_args_list):
565589
try:
566590
self.connect()
567591
self.conn.executemany(stmt, stmt_args_list)
568-
except sqlite3.Error:
592+
except sqlite3.Error as e:
569593
if not self.is_public:
570594
raise
571595
if cylc.flow.flags.verbosity > 1:
572596
traceback.print_exc()
573597
err_log = (
574598
"cannot execute database statement:\n"
575-
"file=%(file)s:\nstmt=%(stmt)s"
576-
) % {"file": self.db_file_name, "stmt": stmt}
599+
"file=%(file)s:\nstmt=%(stmt)s\nerror=%(error)s"
600+
) % {"file": self.db_file_name, "stmt": stmt, "error": str(e)}
577601
for i, stmt_args in enumerate(stmt_args_list):
578602
err_log += ("\nstmt_args[%(i)d]=%(stmt_args)s" % {
579603
"i": i, "stmt_args": stmt_args})

tests/unit/test_rundb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
import unittest
2727
from unittest import mock
28+
import sys
2829

2930
import pytest
3031

@@ -132,6 +133,13 @@ def test_operational_error(tmp_path, caplog):
132133
assert 'DELETE FROM task_jobs' in message
133134
assert 'INSERT OR REPLACE INTO task_jobs' in message
134135
assert 'UPDATE task_jobs' in message
136+
assert 'The error was: no such table: task_jobs' in message
137+
if sys.version_info.major == 3 and sys.version_info.minor >= 11:
138+
assert 'SQLite error code: 1' in message
139+
assert 'SQLite error name: SQLITE_ERROR' in message
140+
else:
141+
assert 'SQLite error code: Not available' in message
142+
assert 'SQLite error name: Not available' in message
135143

136144

137145
def test_table_creation(tmp_path: Path):

0 commit comments

Comments
 (0)