From 5104cbb2855cf657aa285157f97fb38eaccb5d93 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 9 Dec 2024 13:50:33 +0100 Subject: [PATCH] chore: use HTTPStatus where possible (#2795) --- lnbits/core/views/node_api.py | 4 ++-- lnbits/lnurl.py | 7 ++++--- lnbits/nodes/cln.py | 12 +++++++++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lnbits/core/views/node_api.py b/lnbits/core/views/node_api.py index caff8f382..905ab508e 100644 --- a/lnbits/core/views/node_api.py +++ b/lnbits/core/views/node_api.py @@ -69,7 +69,7 @@ public_node_router = APIRouter( @node_router.get( "/ok", description="Check if node api can be enabled", - status_code=200, + status_code=HTTPStatus.OK, dependencies=[Depends(require_node)], ) async def api_get_ok(): @@ -197,5 +197,5 @@ async def api_get_1ml_stats(node: Node = Depends(require_node)) -> Optional[Node return r.json()["noderank"] except httpx.HTTPStatusError as exc: raise HTTPException( - status_code=404, detail="Node not found on 1ml.com" + status_code=HTTPStatus.NOT_FOUND, detail="Node not found on 1ml.com" ) from exc diff --git a/lnbits/lnurl.py b/lnbits/lnurl.py index 18ebdf9ec..20d6bcbf6 100644 --- a/lnbits/lnurl.py +++ b/lnbits/lnurl.py @@ -1,3 +1,4 @@ +from http import HTTPStatus from typing import Callable from fastapi import HTTPException, Request, Response @@ -31,21 +32,21 @@ class LnurlErrorResponseHandler(APIRoute): except (InvoiceError, PaymentError) as exc: logger.debug(f"Wallet Error: {exc}") response = JSONResponse( - status_code=200, + status_code=HTTPStatus.OK, content={"status": "ERROR", "reason": f"{exc.message}"}, ) return response except HTTPException as exc: logger.debug(f"HTTPException: {exc}") response = JSONResponse( - status_code=200, + status_code=HTTPStatus.OK, content={"status": "ERROR", "reason": f"{exc.detail}"}, ) return response except Exception as exc: logger.error("Unknown Error:", exc) response = JSONResponse( - status_code=200, + status_code=HTTPStatus.OK, content={ "status": "ERROR", "reason": f"UNKNOWN ERROR: {exc!s}", diff --git a/lnbits/nodes/cln.py b/lnbits/nodes/cln.py index 09ace6f56..72e4b8a45 100644 --- a/lnbits/nodes/cln.py +++ b/lnbits/nodes/cln.py @@ -47,9 +47,13 @@ def catch_rpc_errors(f): except RpcError as exc: msg = exc.error["message"] if exc.error["code"] == -32602: - raise HTTPException(status_code=400, detail=msg) from exc + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail=msg + ) from exc else: - raise HTTPException(status_code=500, detail=msg) from exc + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=msg + ) from exc return wrapper @@ -152,7 +156,9 @@ class CoreLightningNode(Node): force: bool = False, ): if not short_id: - raise HTTPException(status_code=400, detail="Short id required") + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail="Short id required" + ) try: await self.ln_rpc("close", short_id) except RpcError as exc: