From 81771196717f59c3f42aab3b00bff2db53ca0451 Mon Sep 17 00:00:00 2001 From: Jimmy Kiselak Date: Wed, 16 Sep 2015 16:27:46 -0400 Subject: [PATCH] Fix how some errors are presented to the user If an invalid stream descriptor hash is entered, abort and give a useful error. If a name doesn't resolve, abort and give a useful error. If a name resolves to something invalid, abort and give a useful error. --- lbrynet/core/Error.py | 4 +++ lbrynet/core/client/BlobRequester.py | 6 ++++ .../core/client/StandaloneBlobDownloader.py | 6 ++++ lbrynet/lbrynet_console/ControlHandlers.py | 28 +++++++++++++++---- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lbrynet/core/Error.py b/lbrynet/core/Error.py index 89d22f13e..b2c4c3360 100644 --- a/lbrynet/core/Error.py +++ b/lbrynet/core/Error.py @@ -71,4 +71,8 @@ class NoSuchBlobError(Exception): class NoSuchStreamHashError(Exception): + pass + + +class InvalidBlobHashError(Exception): pass \ No newline at end of file diff --git a/lbrynet/core/client/BlobRequester.py b/lbrynet/core/client/BlobRequester.py index f0f56d82f..5dd97fc7c 100644 --- a/lbrynet/core/client/BlobRequester.py +++ b/lbrynet/core/client/BlobRequester.py @@ -141,6 +141,12 @@ class BlobRequester(object): return [p for p in peers if not p in bad_peers] d.addCallback(choose_best_peers) + + def lookup_failed(err): + log.error("An error occurred looking up peers for a hash: %s", err.getTraceback()) + return [] + + d.addErrback(lookup_failed) return d def _should_send_request_to(self, peer): diff --git a/lbrynet/core/client/StandaloneBlobDownloader.py b/lbrynet/core/client/StandaloneBlobDownloader.py index 40612dc8f..6fb01888c 100644 --- a/lbrynet/core/client/StandaloneBlobDownloader.py +++ b/lbrynet/core/client/StandaloneBlobDownloader.py @@ -5,6 +5,9 @@ from lbrynet.core.BlobInfo import BlobInfo from lbrynet.core.client.BlobRequester import BlobRequester from lbrynet.core.client.ConnectionManager import ConnectionManager from lbrynet.core.client.DownloadManager import DownloadManager +from lbrynet.core.Error import InvalidBlobHashError +from lbrynet.core.utils import is_valid_blobhash +from twisted.python.failure import Failure from twisted.internet import defer @@ -102,6 +105,9 @@ class StandaloneBlobDownloader(object): self.finished_deferred = None def download(self): + if not is_valid_blobhash(self.blob_hash): + return defer.fail(Failure(InvalidBlobHashError(self.blob_hash))) + def cancel_download(d): self.stop() diff --git a/lbrynet/lbrynet_console/ControlHandlers.py b/lbrynet/lbrynet_console/ControlHandlers.py index 0ca23b0da..5276209e4 100644 --- a/lbrynet/lbrynet_console/ControlHandlers.py +++ b/lbrynet/lbrynet_console/ControlHandlers.py @@ -6,6 +6,7 @@ from lbrynet.lbryfilemanager.LBRYFileCreator import create_lbry_file from lbrynet.lbryfile.StreamDescriptor import get_sd_info from lbrynet.lbrynet_console.interfaces import IControlHandler, IControlHandlerFactory from lbrynet.core.StreamDescriptor import download_sd_blob +from lbrynet.core.Error import UnknownNameError, InvalidBlobHashError from twisted.internet import defer @@ -164,8 +165,9 @@ class GetWalletBalances(ControlHandler): d = self.wallet.get_balance() def format_balance(balance): - balance_string = "id: 1\n" - balance_string += "balance: " + str(balance) + "\n" + if balance == 0: + balance = 0 + balance_string = "balance: " + str(balance) + " LBC\n" return balance_string d.addCallback(format_balance) @@ -231,7 +233,7 @@ class LBRYFileStatusFactory(ControlHandlerFactory): class AddStream(ControlHandler): prompt_description = None line_prompt = None - cancel_prompt = "Trying to locate the stream descriptor. Type \"cancel\" to cancel." + cancel_prompt = "Trying to locate the stream's metadata. Type \"cancel\" to cancel..." canceled_message = "Canceled locating the stream descriptor" line_prompt2 = "Modify options? (y/n)" line_prompt3 = "Start download? (y/n)" @@ -339,8 +341,8 @@ class AddStream(ControlHandler): def _handle_load_failed(self, err): self.loading_failed = True log.error("An exception occurred attempting to load the stream descriptor: %s", err.getTraceback()) - return defer.succeed("Encountered a problem while loading the stream descriptor: %s\n" - "See console.log for further details.\n" + return defer.succeed("An unexpected error occurred attempting to load the stream's metadata.\n" + "See console.log for further details.\n\n" "Press enter to continue" % err.getErrorMessage()) def _choose_factory(self, info_and_factories): @@ -460,6 +462,12 @@ class AddStreamFromHash(AddStream): d.addCallback(self.sd_identifier.get_info_and_factories_for_sd_blob) return d + def _handle_load_failed(self, err): + err.trap(InvalidBlobHashError) + self.loading_failed = True + 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") + class AddStreamFromHashFactory(ControlHandlerFactory): control_handler_class = AddStreamFromHash @@ -485,6 +493,16 @@ class AddStreamFromLBRYcrdName(AddStreamFromHash): d.addCallback(get_name_from_info) return d + def _handle_load_failed(self, err): + err.trap(UnknownNameError, InvalidBlobHashError) + self.loading_failed = True + if err.check(UnknownNameError): + return defer.succeed("The name %s could not be found.\n\n" + "Press enter to continue" % err.getErrorMessage()) + else: + return defer.succeed("The metadata for this name is invalid. The stream cannot be downloaded.\n\n" + + "Press enter to continue") + class AddStreamFromLBRYcrdNameFactory(ControlHandlerFactory): control_handler_class = AddStreamFromLBRYcrdName