raise unauthorized error when cannot connect to lbrycrd

This commit is contained in:
Lex Berezhny 2020-07-29 11:32:43 -04:00
parent a802d1f686
commit 3315175d1c
4 changed files with 21 additions and 3 deletions

View file

@ -15,7 +15,7 @@ import zmq.asyncio
from lbry.conf import Config
from lbry.event import EventController
from lbry.error import LbrycrdEventSubscriptionError
from lbry.error import LbrycrdEventSubscriptionError, LbrycrdUnauthorizedError
from .database import BlockchainDB
from .ledger import Ledger, RegTestLedger
@ -248,6 +248,8 @@ class Lbrycrd:
"params": params or []
}
async with self.session.post(self.rpc_url, json=message) as resp:
if resp.status == 401:
raise LbrycrdUnauthorizedError()
try:
result = await resp.json()
except aiohttp.ContentTypeError as e:

View file

@ -82,4 +82,5 @@ Code | Name | Message
702 | CurrencyConversion | {message}
703 | InvalidCurrency | Invalid currency: {currency} is not a supported currency.
**8xx** | Lbrycrd | **Lbrycrd**
801 | LbrycrdUnauthorized | Failed to authenticate with lbrycrd. Perhaps wrong username or password?
811 | LbrycrdEventSubscription | Lbrycrd is not publishing '{event}' events.

View file

@ -406,6 +406,12 @@ class LbrycrdError(BaseError):
"""
class LbrycrdUnauthorizedError(LbrycrdError):
def __init__(self):
super().__init__("Failed to authenticate with lbrycrd. Perhaps wrong username or password?")
class LbrycrdEventSubscriptionError(LbrycrdError):
def __init__(self, event):

View file

@ -11,7 +11,7 @@ from lbry import Config, Database, RegTestLedger, Transaction, Output, Input
from lbry.crypto.base58 import Base58
from lbry.schema.claim import Stream, Channel
from lbry.schema.support import Support
from lbry.error import LbrycrdEventSubscriptionError
from lbry.error import LbrycrdEventSubscriptionError, LbrycrdUnauthorizedError
from lbry.blockchain.lbrycrd import Lbrycrd
from lbry.blockchain.sync import BlockchainSync
from lbry.blockchain.dewies import dewies_to_lbc, lbc_to_dewies
@ -284,7 +284,16 @@ class SyncingBlockchainTestCase(BasicBlockchainTestCase):
self.assertEqual(accepted or [], await self.get_accepted())
class TestLbrycrdEvents(AsyncioTestCase):
class TestLbrycrdAPIs(AsyncioTestCase):
async def test_unauthorized(self):
chain = Lbrycrd.temp_regtest()
await chain.ensure()
await chain.start()
await chain.get_new_address()
chain.conf.set(lbrycrd_rpc_pass='wrong')
with self.assertRaises(LbrycrdUnauthorizedError):
await chain.get_new_address()
async def test_zmq(self):
chain = Lbrycrd.temp_regtest()