Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log handler example in documentation fails to show correct extended result code name #342

Closed
2-5 opened this issue Jul 2, 2022 · 1 comment

Comments

@2-5
Copy link

2-5 commented Jul 2, 2022

The suggested logging handler shown here: https://rogerbinns.github.io/apsw/tips.html#diagnostics fails to show the extended result code name, because it incorrectly clears the lower bits (& ~ 255).

Test case with suggested fix (handler_fixed):

import apsw

def handler(errcode, message):
    errstr=apsw.mapping_result_codes[errcode & 255]
    extended=errcode & ~ 255
    print ("SQLITE_LOG: %s (%d) %s %s" % (message, errcode, errstr, apsw.mapping_extended_result_codes.get(extended, "")))

def handler_fixed(errcode, message):
    errstr=apsw.mapping_result_codes.get(errcode & 255, "<NO_RESULT_CODE>")
    errstr=apsw.mapping_extended_result_codes.get(errcode, errstr)
    print ("SQLITE_LOG: %s (%d) %s" % (message, errcode, errstr))

def test_log():
    apsw.log(apsw.SQLITE_IOERR_ACCESS, "bla")
    apsw.log(apsw.SQLITE_CONSTRAINT_DATATYPE, "bla")

print("=== ORIGINAL HANDLER ===")
apsw.config(apsw.SQLITE_CONFIG_LOG, handler)
test_log()
# PRINTS:
# SQLITE_LOG: bla (3338) SQLITE_IOERR
# SQLITE_LOG: bla (3091) SQLITE_CONSTRAINT

print()

print("=== FIXED HANDLER ===")
apsw.config(apsw.SQLITE_CONFIG_LOG, handler_fixed)
test_log()
# PRINTS:
# SQLITE_LOG: bla (3338) SQLITE_IOERR_ACCESS
# SQLITE_LOG: bla (3091) SQLITE_CONSTRAINT_DATATYPE
@rogerbinns
Copy link
Owner

Thanks for spotting that. It was written with the (incorrect) model that the bottom 8 bits error code was independent of the top 24 bits, and that they would be mixed. They aren't and the rest of the code and doc needs verification that this isn't happening anywhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants