Removes generic handling of errors from commands.
The default behavior for commands is now to only catch exceptions that must be handled specifically. This includes outputting a context dependant message, cleaning up, etc. All other exceptions will be handled by the ui.
This commit is contained in:
parent
df8f0e6d6b
commit
ed2bbb4498
@ -104,11 +104,7 @@ def command(conf, args):
|
|||||||
ui.error('citekey already exist {}.'.format(citekey))
|
ui.error('citekey already exist {}.'.format(citekey))
|
||||||
ui.exit(1)
|
ui.exit(1)
|
||||||
|
|
||||||
try:
|
p = paper.Paper.from_bibentry(bibentry, citekey=citekey)
|
||||||
p = paper.Paper.from_bibentry(bibentry, citekey=citekey)
|
|
||||||
except Exception as e:
|
|
||||||
ui.error(e.args[0])
|
|
||||||
ui.exit(1)
|
|
||||||
|
|
||||||
# tags
|
# tags
|
||||||
|
|
||||||
@ -132,21 +128,16 @@ def command(conf, args):
|
|||||||
if move is None:
|
if move is None:
|
||||||
move = conf['main']['doc_add'] == 'move'
|
move = conf['main']['doc_add'] == 'move'
|
||||||
|
|
||||||
try:
|
rp.push_paper(p)
|
||||||
rp.push_paper(p)
|
ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p)))
|
||||||
ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p)))
|
if docfile is not None:
|
||||||
if docfile is not None:
|
rp.push_doc(p.citekey, docfile, copy=copy or args.move)
|
||||||
rp.push_doc(p.citekey, docfile, copy=copy or args.move)
|
if copy:
|
||||||
if copy:
|
if move:
|
||||||
if move:
|
content.remove_file(docfile)
|
||||||
content.remove_file(docfile)
|
|
||||||
|
|
||||||
if copy:
|
if copy:
|
||||||
if move:
|
if move:
|
||||||
ui.message('{} was moved to the pubs repository.'.format(docfile))
|
ui.message('{} was moved to the pubs repository.'.format(docfile))
|
||||||
else:
|
else:
|
||||||
ui.message('{} was copied to the pubs repository.'.format(docfile))
|
ui.message('{} was copied to the pubs repository.'.format(docfile))
|
||||||
|
|
||||||
except ValueError as v:
|
|
||||||
ui.error(v.message)
|
|
||||||
ui.exit(1)
|
|
||||||
|
@ -65,26 +65,19 @@ def command(conf, args):
|
|||||||
if not ui.input_yn(question=msg, default='n'):
|
if not ui.input_yn(question=msg, default='n'):
|
||||||
ui.exit(0)
|
ui.exit(0)
|
||||||
else:
|
else:
|
||||||
try:
|
rp.remove_doc(paper.citekey)
|
||||||
rp.remove_doc(paper.citekey)
|
|
||||||
except (ValueError, IOError) as v:
|
|
||||||
ui.error(v.message)
|
|
||||||
ui.exit(1)
|
|
||||||
|
|
||||||
try:
|
document = args.document[0]
|
||||||
document = args.document[0]
|
if args.link:
|
||||||
if args.link:
|
rp.push_doc(paper.citekey, document, copy=False)
|
||||||
rp.push_doc(paper.citekey, document, copy=False)
|
else:
|
||||||
else:
|
rp.push_doc(paper.citekey, document, copy=True)
|
||||||
rp.push_doc(paper.citekey, document, copy=True)
|
if not args.link and args.move:
|
||||||
if not args.link and args.move:
|
content.remove_file(document)
|
||||||
content.remove_file(document)
|
|
||||||
|
|
||||||
ui.message('{} added to {}'.format(color.dye_out(document, 'filepath'),
|
ui.message('{} added to {}'.format(
|
||||||
color.dye_out(paper.citekey, 'citekey')))
|
color.dye_out(document, 'filepath'),
|
||||||
except (ValueError, IOError) as v:
|
color.dye_out(paper.citekey, 'citekey')))
|
||||||
ui.error(v.message)
|
|
||||||
ui.exit(1)
|
|
||||||
|
|
||||||
elif args.action == 'remove':
|
elif args.action == 'remove':
|
||||||
|
|
||||||
@ -103,11 +96,7 @@ def command(conf, args):
|
|||||||
if not ui.input_yn(question=msg, default='n'):
|
if not ui.input_yn(question=msg, default='n'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
rp.remove_doc(paper.citekey)
|
||||||
rp.remove_doc(paper.citekey)
|
|
||||||
except (ValueError, IOError) as v:
|
|
||||||
ui.error(v.message)
|
|
||||||
ui.exit(1)
|
|
||||||
|
|
||||||
elif args.action == 'export':
|
elif args.action == 'export':
|
||||||
|
|
||||||
@ -133,7 +122,7 @@ def command(conf, args):
|
|||||||
dest_path = path + os.path.basename(real_doc_path)
|
dest_path = path + os.path.basename(real_doc_path)
|
||||||
content.copy_content(real_doc_path, dest_path)
|
content.copy_content(real_doc_path, dest_path)
|
||||||
except (ValueError, IOError) as e:
|
except (ValueError, IOError) as e:
|
||||||
ui.error(e.message)
|
ui.error(str(e))
|
||||||
|
|
||||||
elif args.action == 'open':
|
elif args.action == 'open':
|
||||||
with_command = args.cmd
|
with_command = args.cmd
|
||||||
|
@ -74,20 +74,15 @@ def command(conf, args):
|
|||||||
papers = many_from_path(bibpath)
|
papers = many_from_path(bibpath)
|
||||||
keys = args.keys or papers.keys()
|
keys = args.keys or papers.keys()
|
||||||
for k in keys:
|
for k in keys:
|
||||||
try:
|
p = papers[k]
|
||||||
p = papers[k]
|
if isinstance(p, Exception):
|
||||||
if isinstance(p, Exception):
|
ui.error(u'Could not load entry for citekey {}.'.format(k))
|
||||||
ui.error(u'Could not load entry for citekey {}.'.format(k))
|
else:
|
||||||
|
rp.push_paper(p)
|
||||||
|
ui.info(u'{} imported.'.format(color.dye_out(p.citekey, 'citekey')))
|
||||||
|
docfile = bibstruct.extract_docfile(p.bibdata)
|
||||||
|
if docfile is None:
|
||||||
|
ui.warning("No file for {}.".format(p.citekey))
|
||||||
else:
|
else:
|
||||||
rp.push_paper(p)
|
rp.push_doc(p.citekey, docfile, copy=copy)
|
||||||
ui.info(u'{} imported.'.format(color.dye_out(p.citekey, 'citekey')))
|
#FIXME should move the file if configured to do so.
|
||||||
docfile = bibstruct.extract_docfile(p.bibdata)
|
|
||||||
if docfile is None:
|
|
||||||
ui.warning("No file for {}.".format(p.citekey))
|
|
||||||
else:
|
|
||||||
rp.push_doc(p.citekey, docfile, copy=copy)
|
|
||||||
#FIXME should move the file if configured to do so.
|
|
||||||
except KeyError:
|
|
||||||
ui.error(u'No entry found for citekey {}.'.format(k))
|
|
||||||
except IOError as e:
|
|
||||||
ui.error(e.message)
|
|
||||||
|
@ -19,8 +19,5 @@ def command(conf, args):
|
|||||||
ui = get_ui()
|
ui = get_ui()
|
||||||
rp = repo.Repository(conf)
|
rp = repo.Repository(conf)
|
||||||
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
|
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
|
||||||
try:
|
notepath = rp.databroker.real_notepath(citekey)
|
||||||
notepath = rp.databroker.real_notepath(citekey)
|
content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False)
|
||||||
content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False)
|
|
||||||
except Exception as e:
|
|
||||||
ui.error(e.message)
|
|
||||||
|
@ -26,13 +26,17 @@ def command(conf, args):
|
|||||||
.format(', '.join([color.dye_out(c, 'citekey') for c in args.citekeys])))
|
.format(', '.join([color.dye_out(c, 'citekey') for c in args.citekeys])))
|
||||||
sure = ui.input_yn(question=are_you_sure, default='n')
|
sure = ui.input_yn(question=are_you_sure, default='n')
|
||||||
if force or sure:
|
if force or sure:
|
||||||
|
failed = False # Whether something failed
|
||||||
for c in keys:
|
for c in keys:
|
||||||
try:
|
try:
|
||||||
rp.remove_paper(c)
|
rp.remove_paper(c)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ui.error(e.message)
|
ui.error(e.message)
|
||||||
|
failed = True
|
||||||
ui.message('The publication(s) [{}] were removed'.format(
|
ui.message('The publication(s) [{}] were removed'.format(
|
||||||
', '.join([color.dye_out(c, 'citekey') for c in keys])))
|
', '.join([color.dye_out(c, 'citekey') for c in keys])))
|
||||||
|
if failed:
|
||||||
|
ui.exit() # Exit with nonzero error code
|
||||||
# FIXME: print should check that removal proceeded well.
|
# FIXME: print should check that removal proceeded well.
|
||||||
else:
|
else:
|
||||||
ui.message('The publication(s) [{}] were {} removed'.format(
|
ui.message('The publication(s) [{}] were {} removed'.format(
|
||||||
|
@ -21,9 +21,6 @@ def command(conf, args):
|
|||||||
rp = repo.Repository(conf)
|
rp = repo.Repository(conf)
|
||||||
|
|
||||||
# TODO: here should be a test whether the new citekey is valid
|
# TODO: here should be a test whether the new citekey is valid
|
||||||
try:
|
key = resolve_citekey(repo=rp, citekey=args.citekey, ui=ui, exit_on_fail=True)
|
||||||
key = resolve_citekey(repo=rp, citekey=args.citekey, ui=ui, exit_on_fail=True)
|
paper = rp.pull_paper(key)
|
||||||
paper = rp.pull_paper(key)
|
rp.rename_paper(paper, args.new_citekey)
|
||||||
rp.rename_paper(paper, args.new_citekey)
|
|
||||||
except Exception as e:
|
|
||||||
ui.error(e.message)
|
|
||||||
|
@ -24,14 +24,14 @@ class CiteKeyError(Exception):
|
|||||||
return self.message or self.default_msg.format(self.citekey)
|
return self.message or self.default_msg.format(self.citekey)
|
||||||
|
|
||||||
|
|
||||||
class CiteKeyCollision(Exception):
|
class CiteKeyCollision(CiteKeyError):
|
||||||
|
|
||||||
default_message = "Citekey already in use: {}."
|
default_message = "Citekey already in use: {}."
|
||||||
|
|
||||||
|
|
||||||
class CiteKeyNotFound(Exception):
|
class CiteKeyNotFound(CiteKeyError):
|
||||||
|
|
||||||
default_message = "Could not find citekey: {}."
|
default_message = "No entry found for citekey: {}."
|
||||||
|
|
||||||
|
|
||||||
class Repository(object):
|
class Repository(object):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user