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.
main
Olivier Mangin 9 years ago
parent df8f0e6d6b
commit ed2bbb4498

@ -104,11 +104,7 @@ def command(conf, args):
ui.error('citekey already exist {}.'.format(citekey))
ui.exit(1)
try:
p = paper.Paper.from_bibentry(bibentry, citekey=citekey)
except Exception as e:
ui.error(e.args[0])
ui.exit(1)
p = paper.Paper.from_bibentry(bibentry, citekey=citekey)
# tags
@ -132,21 +128,16 @@ def command(conf, args):
if move is None:
move = conf['main']['doc_add'] == 'move'
try:
rp.push_paper(p)
ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p)))
if docfile is not None:
rp.push_doc(p.citekey, docfile, copy=copy or args.move)
if copy:
if move:
content.remove_file(docfile)
if copy:
if move:
ui.message('{} was moved to the pubs repository.'.format(docfile))
else:
ui.message('{} was copied to the pubs repository.'.format(docfile))
except ValueError as v:
ui.error(v.message)
ui.exit(1)
rp.push_paper(p)
ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p)))
if docfile is not None:
rp.push_doc(p.citekey, docfile, copy=copy or args.move)
if copy:
if move:
content.remove_file(docfile)
if copy:
if move:
ui.message('{} was moved to the pubs repository.'.format(docfile))
else:
ui.message('{} was copied to the pubs repository.'.format(docfile))

@ -65,26 +65,19 @@ def command(conf, args):
if not ui.input_yn(question=msg, default='n'):
ui.exit(0)
else:
try:
rp.remove_doc(paper.citekey)
except (ValueError, IOError) as v:
ui.error(v.message)
ui.exit(1)
rp.remove_doc(paper.citekey)
try:
document = args.document[0]
if args.link:
rp.push_doc(paper.citekey, document, copy=False)
else:
rp.push_doc(paper.citekey, document, copy=True)
if not args.link and args.move:
content.remove_file(document)
ui.message('{} added to {}'.format(color.dye_out(document, 'filepath'),
color.dye_out(paper.citekey, 'citekey')))
except (ValueError, IOError) as v:
ui.error(v.message)
ui.exit(1)
document = args.document[0]
if args.link:
rp.push_doc(paper.citekey, document, copy=False)
else:
rp.push_doc(paper.citekey, document, copy=True)
if not args.link and args.move:
content.remove_file(document)
ui.message('{} added to {}'.format(
color.dye_out(document, 'filepath'),
color.dye_out(paper.citekey, 'citekey')))
elif args.action == 'remove':
@ -103,11 +96,7 @@ def command(conf, args):
if not ui.input_yn(question=msg, default='n'):
continue
try:
rp.remove_doc(paper.citekey)
except (ValueError, IOError) as v:
ui.error(v.message)
ui.exit(1)
rp.remove_doc(paper.citekey)
elif args.action == 'export':
@ -133,7 +122,7 @@ def command(conf, args):
dest_path = path + os.path.basename(real_doc_path)
content.copy_content(real_doc_path, dest_path)
except (ValueError, IOError) as e:
ui.error(e.message)
ui.error(str(e))
elif args.action == 'open':
with_command = args.cmd

@ -74,20 +74,15 @@ def command(conf, args):
papers = many_from_path(bibpath)
keys = args.keys or papers.keys()
for k in keys:
try:
p = papers[k]
if isinstance(p, Exception):
ui.error(u'Could not load entry for citekey {}.'.format(k))
p = papers[k]
if isinstance(p, Exception):
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:
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:
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)
rp.push_doc(p.citekey, docfile, copy=copy)
#FIXME should move the file if configured to do so.

@ -19,8 +19,5 @@ def command(conf, args):
ui = get_ui()
rp = repo.Repository(conf)
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
try:
notepath = rp.databroker.real_notepath(citekey)
content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False)
except Exception as e:
ui.error(e.message)
notepath = rp.databroker.real_notepath(citekey)
content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False)

@ -26,13 +26,17 @@ def command(conf, args):
.format(', '.join([color.dye_out(c, 'citekey') for c in args.citekeys])))
sure = ui.input_yn(question=are_you_sure, default='n')
if force or sure:
failed = False # Whether something failed
for c in keys:
try:
rp.remove_paper(c)
except Exception as e:
ui.error(e.message)
failed = True
ui.message('The publication(s) [{}] were removed'.format(
', '.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.
else:
ui.message('The publication(s) [{}] were {} removed'.format(

@ -21,9 +21,6 @@ def command(conf, args):
rp = repo.Repository(conf)
# 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)
paper = rp.pull_paper(key)
rp.rename_paper(paper, args.new_citekey)
except Exception as e:
ui.error(e.message)
key = resolve_citekey(repo=rp, citekey=args.citekey, ui=ui, exit_on_fail=True)
paper = rp.pull_paper(key)
rp.rename_paper(paper, args.new_citekey)

@ -24,14 +24,14 @@ class CiteKeyError(Exception):
return self.message or self.default_msg.format(self.citekey)
class CiteKeyCollision(Exception):
class CiteKeyCollision(CiteKeyError):
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):

Loading…
Cancel
Save