better error messages in console when funds are exhausted

This commit is contained in:
Jimmy Kiselak 2015-09-17 01:43:41 -04:00
parent 8177119671
commit 33b6b85e72
9 changed files with 59 additions and 32 deletions

View file

@ -72,6 +72,7 @@ class BlobRequester(object):
sent_request = True sent_request = True
else: else:
d_r.cancel(InsufficientFundsError()) d_r.cancel(InsufficientFundsError())
d_r.finished_deferred.addErrback(lambda _: True)
return defer.fail(InsufficientFundsError()) return defer.fail(InsufficientFundsError())
if sent_request is True: if sent_request is True:
if p_r is not None: if p_r is not None:

View file

@ -80,13 +80,14 @@ class ConnectionManager(object):
return defer.succeed(False) return defer.succeed(False)
def handle_error(err): def handle_error(err):
if err.check(InsufficientFundsError): err.trap(InsufficientFundsError)
self.downloader.insufficient_funds() self.downloader.insufficient_funds(err)
return False return False
else:
return err
def check_if_request_sent(request_sent, request_creator): def check_if_request_sent(request_sent, request_creator):
if peer not in self._peer_connections:
# This can happen if the connection is told to close
return False
if request_sent is False: if request_sent is False:
if request_creator in self._peer_connections[peer].request_creators: if request_creator in self._peer_connections[peer].request_creators:
self._peer_connections[peer].request_creators.remove(request_creator) self._peer_connections[peer].request_creators.remove(request_creator)

View file

@ -136,7 +136,10 @@ class StandaloneBlobDownloader(object):
def _blob_downloaded(self, blob): def _blob_downloaded(self, blob):
self.stop() self.stop()
if not self.finished_deferred.called:
self.finished_deferred.callback(blob) self.finished_deferred.callback(blob)
def insufficient_funds(self): def insufficient_funds(self, err):
return self.stop() self.stop()
if not self.finished_deferred.called:
self.finished_deferred.errback(err)

View file

@ -96,7 +96,7 @@ class CryptStreamDownloader(object):
d.addCallback(lambda _: set_finished_deferred()) d.addCallback(lambda _: set_finished_deferred())
return d return d
def stop(self): def stop(self, err=None):
def check_if_stop_succeeded(success): def check_if_stop_succeeded(success):
self.stopping = False self.stopping = False
@ -112,7 +112,7 @@ class CryptStreamDownloader(object):
assert self.download_manager is not None assert self.download_manager is not None
self.stopping = True self.stopping = True
d = self.download_manager.stop_downloading() d = self.download_manager.stop_downloading()
self._fire_completed_deferred() self._fire_completed_deferred(err)
d.addCallback(check_if_stop_succeeded) d.addCallback(check_if_stop_succeeded)
return d return d
@ -196,9 +196,12 @@ class CryptStreamDownloader(object):
self._get_primary_request_creators(download_manager), self._get_primary_request_creators(download_manager),
self._get_secondary_request_creators(download_manager)) self._get_secondary_request_creators(download_manager))
def _fire_completed_deferred(self): def _fire_completed_deferred(self, err=None):
self.finished_deferred, d = None, self.finished_deferred self.finished_deferred, d = None, self.finished_deferred
if d is not None: if d is not None:
if err is not None:
d.errback(err)
else:
d.callback(self._get_finished_deferred_callback_value()) d.callback(self._get_finished_deferred_callback_value())
def _get_finished_deferred_callback_value(self): def _get_finished_deferred_callback_value(self):
@ -209,5 +212,5 @@ class CryptStreamDownloader(object):
self.completed = True self.completed = True
return self.stop() return self.stop()
def insufficient_funds(self): def insufficient_funds(self, err):
return self.stop() return self.stop(err=err)

View file

@ -521,7 +521,7 @@ class IStreamDownloader(Interface):
@rtype: Deferred which fires with anything @rtype: Deferred which fires with anything
""" """
def insufficient_funds(self): def insufficient_funds(self, err):
""" """
this function informs the stream downloader that funds are too low to finish downloading. this function informs the stream downloader that funds are too low to finish downloading.

View file

@ -40,9 +40,9 @@ class LBRYFileDownloader(CryptStreamDownloader):
else: else:
return defer.succeed(True) return defer.succeed(True)
def stop(self): def stop(self, err=None):
d = self._close_output() d = self._close_output()
d.addCallback(lambda _: CryptStreamDownloader.stop(self)) d.addCallback(lambda _: CryptStreamDownloader.stop(self, err=err))
return d return d
def _get_progress_manager(self, download_manager): def _get_progress_manager(self, download_manager):
@ -139,8 +139,8 @@ class LBRYFileSaver(LBRYFileDownloader):
d.addCallback(lambda _: set_file_name()) d.addCallback(lambda _: set_file_name())
return d return d
def stop(self): def stop(self, err=None):
d = LBRYFileDownloader.stop(self) d = LBRYFileDownloader.stop(self, err=err)
d.addCallback(lambda _: self._delete_from_info_manager()) d.addCallback(lambda _: self._delete_from_info_manager())
return d return d
@ -209,8 +209,8 @@ class LBRYFileOpener(LBRYFileDownloader):
self.process = None self.process = None
self.process_log = None self.process_log = None
def stop(self): def stop(self, err=None):
d = LBRYFileDownloader.stop(self) d = LBRYFileDownloader.stop(self, err=err)
d.addCallback(lambda _: self._delete_from_info_manager()) d.addCallback(lambda _: self._delete_from_info_manager())
return d return d

View file

@ -43,12 +43,12 @@ class ManagedLBRYFileDownloader(LBRYFileSaver):
d.addCallback(restore_status) d.addCallback(restore_status)
return d return d
def stop(self, change_status=True): def stop(self, err=None, change_status=True):
def set_saving_status_done(): def set_saving_status_done():
self.saving_status = False self.saving_status = False
d = LBRYFileDownloader.stop(self) # LBRYFileSaver deletes metadata when it's stopped. We don't want that here. d = LBRYFileDownloader.stop(self, err=err) # LBRYFileSaver deletes metadata when it's stopped. We don't want that here.
if change_status is True: if change_status is True:
self.saving_status = True self.saving_status = True
d.addCallback(lambda _: self._save_status()) d.addCallback(lambda _: self._save_status())

View file

@ -6,7 +6,7 @@ from lbrynet.lbryfilemanager.LBRYFileCreator import create_lbry_file
from lbrynet.lbryfile.StreamDescriptor import get_sd_info from lbrynet.lbryfile.StreamDescriptor import get_sd_info
from lbrynet.lbrynet_console.interfaces import IControlHandler, IControlHandlerFactory from lbrynet.lbrynet_console.interfaces import IControlHandler, IControlHandlerFactory
from lbrynet.core.StreamDescriptor import download_sd_blob from lbrynet.core.StreamDescriptor import download_sd_blob
from lbrynet.core.Error import UnknownNameError, InvalidBlobHashError from lbrynet.core.Error import UnknownNameError, InvalidBlobHashError, InsufficientFundsError
from twisted.internet import defer from twisted.internet import defer
@ -234,7 +234,7 @@ class AddStream(ControlHandler):
prompt_description = None prompt_description = None
line_prompt = None line_prompt = None
cancel_prompt = "Trying to locate the stream's metadata. Type \"cancel\" to cancel..." cancel_prompt = "Trying to locate the stream's metadata. Type \"cancel\" to cancel..."
canceled_message = "Canceled locating the stream descriptor" canceled_message = "Canceled locating the stream's metadata."
line_prompt2 = "Modify options? (y/n)" line_prompt2 = "Modify options? (y/n)"
line_prompt3 = "Start download? (y/n)" line_prompt3 = "Start download? (y/n)"
@ -343,7 +343,7 @@ class AddStream(ControlHandler):
log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback()) log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback())
return defer.succeed("An unexpected error occurred attempting to load the stream's metadata.\n" return defer.succeed("An unexpected error occurred attempting to load the stream's metadata.\n"
"See console.log for further details.\n\n" "See console.log for further details.\n\n"
"Press enter to continue" % err.getErrorMessage()) "Press enter to continue")
def _choose_factory(self, info_and_factories): def _choose_factory(self, info_and_factories):
self.loading_info_and_factories_deferred = None self.loading_info_and_factories_deferred = None
@ -430,8 +430,16 @@ class AddStream(ControlHandler):
def _start_download(self): def _start_download(self):
d = self._make_downloader() d = self._make_downloader()
d.addCallback(lambda stream_downloader: stream_downloader.start()) d.addCallback(lambda stream_downloader: stream_downloader.start())
d.addErrback(self._handle_download_error)
return d return d
def _handle_download_error(self, err):
if err.check(InsufficientFundsError):
return "Download stopped due to insufficient funds."
else:
log.error("An unexpected error has caused the download to stop: %s" % err.getTraceback())
return "An unexpected error has caused the download to stop. See console.log for details."
def _make_downloader(self): def _make_downloader(self):
return self.factory.make_downloader(self.info_validator, self.options_chosen, return self.factory.make_downloader(self.info_validator, self.options_chosen,
self.payment_rate_manager) self.payment_rate_manager)
@ -463,10 +471,13 @@ class AddStreamFromHash(AddStream):
return d return d
def _handle_load_failed(self, err): def _handle_load_failed(self, err):
err.trap(InvalidBlobHashError)
self.loading_failed = True self.loading_failed = True
if err.check(InvalidBlobHashError):
return defer.succeed("The hash you entered is invalid. It must be 96 characters long and " return defer.succeed("The hash you entered is invalid. It must be 96 characters long and "
"contain only hex characters.\n\nPress enter to continue") "contain only hex characters.\n\nPress enter to continue")
if err.check(InsufficientFundsError):
return defer.succeed("Insufficient funds to download the metadata blob.\n\nPress enter to continue")
return AddStream._handle_load_failed(self, err)
class AddStreamFromHashFactory(ControlHandlerFactory): class AddStreamFromHashFactory(ControlHandlerFactory):
@ -494,14 +505,14 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash):
return d return d
def _handle_load_failed(self, err): def _handle_load_failed(self, err):
err.trap(UnknownNameError, InvalidBlobHashError)
self.loading_failed = True self.loading_failed = True
if err.check(UnknownNameError): if err.check(UnknownNameError):
return defer.succeed("The name %s could not be found.\n\n" return defer.succeed("The name %s could not be found.\n\n"
"Press enter to continue" % err.getErrorMessage()) "Press enter to continue" % err.getErrorMessage())
else: elif err.check(InvalidBlobHashError):
return defer.succeed("The metadata for this name is invalid. The stream cannot be downloaded.\n\n" + return defer.succeed("The metadata for this name is invalid. The stream cannot be downloaded.\n\n" +
"Press enter to continue") "Press enter to continue")
return AddStreamFromHash._handle_load_failed(self, err)
class AddStreamFromLBRYcrdNameFactory(ControlHandlerFactory): class AddStreamFromLBRYcrdNameFactory(ControlHandlerFactory):
@ -640,8 +651,16 @@ class ToggleLBRYFileRunning(ControlHandler):
def handle_line(self, line): def handle_line(self, line):
d = self.lbry_file_manager.toggle_lbry_file_running(self.lbry_file.stream_hash) d = self.lbry_file_manager.toggle_lbry_file_running(self.lbry_file.stream_hash)
d.addErrback(self._handle_download_error)
return True, d return True, d
def _handle_download_error(self, err):
if err.check(InsufficientFundsError):
return "Download stopped due to insufficient funds."
else:
log.error("An unexpected error occurred due to toggling an LBRY file running. %s", err.getTraceback())
return "An unexpected error occurred. See console.log for details."
class ToggleLBRYFileRunningFactory(LBRYFileChooserFactory): class ToggleLBRYFileRunningFactory(LBRYFileChooserFactory):
control_handler_class = ToggleLBRYFileRunning control_handler_class = ToggleLBRYFileRunning

View file

@ -109,5 +109,5 @@ class BlindRepeater(object):
def _update_status(self, stopped=True): def _update_status(self, stopped=True):
self.stopped = stopped self.stopped = stopped
def insufficient_funds(self): def insufficient_funds(self, err):
return self.stop() return self.stop()