From 66e3a612d77ca9c000eddefe5c9ec64189fe27cc Mon Sep 17 00:00:00 2001 From: Odin Kroeger Date: Wed, 2 Jun 2021 23:12:00 +0200 Subject: [PATCH] fix: Nicer error messages. Addresses new issue in #4. But isn't beautiful enough just yet. --- pandoc-zotxt.lua | 22 ++++++++++++---------- test/scripts/debug-wrapper.lua | 5 ++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pandoc-zotxt.lua b/pandoc-zotxt.lua index aa80fdb..3595c0d 100644 --- a/pandoc-zotxt.lua +++ b/pandoc-zotxt.lua @@ -1115,10 +1115,12 @@ do -- error message (for easy citekeys) or an empty response -- (for Better BibTeX citation keys). local query_url = concat{base_url, key_ts[i], '=', id} - local mt, data = url_read(query_url) - if mt and mt ~= '' then - assert(match(mt, utf8_p), - 'Data retrieved from zotxt is not encoded in UTF-8.') + local ok, mt, data = pcall(url_read, query_url) + assert(ok, 'Could not get data from Zotero.') + assert(mt or match(mt, utf8_p), + 'zotxt response is not encoded in UTF-8.') + if mt ~= '' then + -- luacheck: ignore ok local ok, item = pcall(parse_f, data, mt) if ok then if i ~= 1 then @@ -1440,9 +1442,9 @@ function biblio_read (fname) if not decode then return nil, fname .. ': Cannot parse format.' end local str, err, errno = file_read(fname) if not str then return nil, err, errno end - local ok, items = pcall(decode, str) - if not ok then return nil, fname .. ': Parse error.' end - return items + local ok, ret = pcall(decode, str) + if not ok then return nil, fname .. ': ' .. tostring(ret) end + return ret end @@ -1473,9 +1475,9 @@ function biblio_write (fname, items) local encode = codec.encode if not encode then return nil, fname .. ': Cannot write format.' end if not items or #items == 0 then return suffix end - local ok, str = pcall(encode, items) - if not ok then return nil, fname .. ': Serialisation error.' end - local ok, err, errno = file_write(fname, str, EOL) + local ok, ret = pcall(encode, items) + if not ok then return nil, fname .. ': ' .. tostring(ret) end + local ok, err, errno = file_write(fname, ret, EOL) if not ok then return nil, err, errno end return suffix end diff --git a/test/scripts/debug-wrapper.lua b/test/scripts/debug-wrapper.lua index 23b9b8d..b01ce3f 100644 --- a/test/scripts/debug-wrapper.lua +++ b/test/scripts/debug-wrapper.lua @@ -108,9 +108,8 @@ function M.url_read (url) local hash = pandoc.utils.sha1(url):sub(1, 8) M.warnf('%s -> %s', url, hash) local path = M.path_join(CAN_DIR, hash) - local data, err, errno = M.file_read(path) - if not data then return nil, err, errno end - return 'text/plain; charset=utf-8', data + local data = M.file_read(path) + return 'text/plain; charset=utf-8', data or '' end return M \ No newline at end of file