-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch all exceptions in wallet API #220
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
from os.path import isdir, join | ||
from typing import Optional | ||
|
||
from fastapi import APIRouter, HTTPException, Query, status | ||
from fastapi import APIRouter, Query | ||
|
||
from ...core.base import TokenV3 | ||
from ...core.helpers import sum_proofs | ||
|
@@ -32,7 +32,7 @@ async def load_mint(wallet: Wallet, mint: Optional[str] = None): | |
try: | ||
await wallet.load_mint() | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) | ||
raise Exception(str(e)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block can be replaced by simply |
||
return wallet | ||
|
||
|
||
|
@@ -42,9 +42,7 @@ async def load_mint(wallet: Wallet, mint: Optional[str] = None): | |
@router.on_event("startup") | ||
async def start_wallet(): | ||
if settings.tor and not TorProxy().check_platform(): | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, detail="tor not working" | ||
) | ||
raise Exception("tor not working.") | ||
await init_wallet(wallet) | ||
|
||
|
||
|
@@ -57,23 +55,16 @@ async def pay( | |
), | ||
): | ||
if not settings.lightning: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, detail="lightning not enabled." | ||
) | ||
raise Exception("lightning not enabled.") | ||
|
||
global wallet | ||
wallet = await load_mint(wallet, mint) | ||
|
||
await wallet.load_proofs() | ||
initial_balance = wallet.available_balance | ||
total_amount, fee_reserve_sat = await wallet.get_pay_amount_with_fees(invoice) | ||
assert total_amount > 0, HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="amount has to be larger than zero.", | ||
) | ||
if wallet.available_balance < total_amount: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, detail="balance is too low." | ||
) | ||
assert total_amount > 0, "amount has to be larger than zero." | ||
assert wallet.available_balance >= total_amount, "balance is too low." | ||
_, send_proofs = await wallet.split_to_send(wallet.proofs, total_amount) | ||
await wallet.pay_lightning(send_proofs, invoice) | ||
await wallet.load_proofs() | ||
|
@@ -152,16 +143,10 @@ async def send_command( | |
|
||
await wallet.load_proofs() | ||
if not nostr: | ||
try: | ||
balance, token = await send(wallet, amount, lock, legacy=False) | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) | ||
balance, token = await send(wallet, amount, lock, legacy=False) | ||
return {"balance": balance, "token": token} | ||
else: | ||
try: | ||
token, pubkey = await send_nostr(wallet, amount, nostr) | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) | ||
token, pubkey = await send_nostr(wallet, amount, nostr) | ||
return { | ||
"balance": wallet.available_balance, | ||
"token": token, | ||
|
@@ -178,25 +163,12 @@ async def receive_command( | |
): | ||
result = {"initial_balance": wallet.available_balance} | ||
if token: | ||
try: | ||
tokenObj: TokenV3 = await deserialize_token_from_string(token) | ||
|
||
try: | ||
await verify_mints(wallet, tokenObj) | ||
except Exception as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e) | ||
) | ||
|
||
balance = await receive(wallet, tokenObj, lock) | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) | ||
tokenObj: TokenV3 = await deserialize_token_from_string(token) | ||
await verify_mints(wallet, tokenObj) | ||
balance = await receive(wallet, tokenObj, lock) | ||
elif nostr: | ||
try: | ||
await receive_nostr(wallet) | ||
balance = wallet.available_balance | ||
except Exception as e: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) | ||
await receive_nostr(wallet) | ||
balance = wallet.available_balance | ||
elif all: | ||
reserved_proofs = await get_reserved_proofs(wallet.db) | ||
balance = None | ||
|
@@ -205,23 +177,10 @@ async def receive_command( | |
proofs = list(value) | ||
token = await wallet.serialize_proofs(proofs) | ||
tokenObj = await deserialize_token_from_string(token) | ||
try: | ||
await verify_mints(wallet, tokenObj) | ||
except Exception as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e) | ||
) | ||
try: | ||
balance = await receive(wallet, tokenObj, lock) | ||
except Exception as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, detail=str(e) | ||
) | ||
await verify_mints(wallet, tokenObj) | ||
balance = await receive(wallet, tokenObj, lock) | ||
else: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="enter token or use either flag --nostr or --all.", | ||
) | ||
raise Exception("enter token or use either flag --nostr or --all.") | ||
assert balance | ||
result.update({"balance": balance}) | ||
return result | ||
|
@@ -245,9 +204,8 @@ async def burn( | |
if not delete: | ||
wallet = await load_mint(wallet, mint) | ||
if not (all or token or force or delete) or (token and all): | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="enter a token or use --all to burn all pending tokens, --force to check all tokens" | ||
raise Exception( | ||
"enter a token or use --all to burn all pending tokens, --force to check all tokens" | ||
"or --delete with send ID to force-delete pending token from list if mint is unavailable.", | ||
) | ||
if all: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jokingly suggest using a custom Exception class
Cachuww
🤧There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cash'uwu