cs50/python-cs50

Improve error reporting

dmalan opened this issue · 2 comments

Currently, a syntax error (e.g., a missing paren) might yield this output from SQL.execute, with the DEBUG line in red:

DEBUG:cs50:INSERT INTO tbl (id, foo VALUES(1, NULL)
Traceback (most recent call last):
  File "foo.py", line 8, in <module>
    print(db.execute("INSERT INTO tbl (id, foo VALUES(1, NULL)"))
  File "/mnt/src/cs50/sql.py", line 289, in execute
    raise e
RuntimeError: (sqlite3.OperationalError) near "VALUES": syntax error
[SQL: INSERT INTO tbl (id, foo VALUES(1, NULL)]
(Background on this error at: http://sqlalche.me/e/e3q8)

Could be simplified to:

DEBUG:cs50:INSERT INTO tbl (id, foo VALUES(1, NULL)
Traceback (most recent call last):
  File "foo.py", line 8, in <module>
    print(db.execute("INSERT INTO tbl (id, foo VALUES(1, NULL)"))
  File "/mnt/src/cs50/sql.py", line 289, in execute
    raise e
RuntimeError: near "VALUES": syntax error

Meanwhile, an IntegrityError yields the below, with the DEBUG line in yellow, but doesn't report the actual issue:

DEBUG:cs50:INSERT INTO tbl (id, foo) VALUES(1, NULL)

This could be enhanced as follows, with the first DEBUG line in yellow, the second in white (since it's not a query):

DEBUG:cs50:INSERT INTO tbl (id, foo) VALUES(1, NULL)
DEBUG:cs50:NOT NULL constraint failed: tbl.foo

After conferring with @brianyu28, latest commit in #100 elevates IntegrityErrors to be RuntimeErrors as well, the presumption being that if a student's code is violating an integrity constraint, it's probably unintentional, and could be more cleanly solved with, e.g., INSERT OR IGNORE. Updated sample output below:

DEBUG:cs50:INSERT INTO tbl (id, foo) VALUES(1, NULL) # yellow
Traceback (most recent call last):
  File "foo.py", line 8, in <module>
    db.execute("INSERT INTO tbl (id, foo) VALUES(1, NULL)")
  File "/mnt/src/cs50/sql.py", line 283, in execute
    raise e
RuntimeError: NOT NULL constraint failed: tbl.foo

Will ship after pset8 is due (after extensions).