Write errors to stderr, closes #1131

This commit is contained in:
Simon Willison 2020-12-04 21:21:11 -08:00
commit eae103a82b
4 changed files with 16 additions and 6 deletions

View file

@ -1,8 +1,8 @@
import asyncio import asyncio
import contextlib
from pathlib import Path from pathlib import Path
import janus import janus
import queue import queue
import sys
import threading import threading
import uuid import uuid
@ -104,7 +104,8 @@ class Database:
try: try:
result = task.fn(conn) result = task.fn(conn)
except Exception as e: except Exception as e:
print(e) sys.stderr.write("{}\n".format(e))
sys.stderr.flush()
result = e result = e
task.reply_queue.sync_q.put(result) task.reply_queue.sync_q.put(result)
@ -156,11 +157,12 @@ class Database:
if e.args == ("interrupted",): if e.args == ("interrupted",):
raise QueryInterrupted(e, sql, params) raise QueryInterrupted(e, sql, params)
if log_sql_errors: if log_sql_errors:
print( sys.stderr.write(
"ERROR: conn={}, sql = {}, params = {}: {}".format( "ERROR: conn={}, sql = {}, params = {}: {}\n".format(
conn, repr(sql), params, e conn, repr(sql), params, e
) )
) )
sys.stderr.flush()
raise raise
if truncate: if truncate:

View file

@ -20,7 +20,6 @@ def convert_specific_columns_to_json(rows, columns, json_cols):
try: try:
value = json.loads(value) value = json.loads(value)
except (TypeError, ValueError) as e: except (TypeError, ValueError) as e:
print(e)
pass pass
new_row.append(value) new_row.append(value)
new_rows.append(new_row) new_rows.append(new_row)

View file

@ -2,6 +2,7 @@ import asyncio
import csv import csv
import hashlib import hashlib
import re import re
import sys
import time import time
import urllib import urllib
@ -362,7 +363,8 @@ class DataView(BaseView):
new_row.append(cell) new_row.append(cell)
await writer.writerow(new_row) await writer.writerow(new_row)
except Exception as e: except Exception as e:
print("caught this", e) sys.stderr.write("Caught this error: {}\n".format(e))
sys.stderr.flush()
await r.write(str(e)) await r.write(str(e))
return return

View file

@ -214,3 +214,10 @@ def test_config_deprecated(ensure_eventloop):
assert result.exit_code == 0 assert result.exit_code == 0
assert not json.loads(result.output)["allow_download"] assert not json.loads(result.output)["allow_download"]
assert "will be deprecated in" in result.stderr assert "will be deprecated in" in result.stderr
def test_sql_errors_logged_to_stderr(ensure_eventloop):
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, ["--get", "/:memory:.json?sql=select+blah"])
assert result.exit_code == 1
assert "sql = 'select blah', params = {}: no such column: blah\n" in result.stderr