22Exceptions.
33"""
44
5+ import json
56from typing import List
67from uuid import UUID
78from fastapi import status
@@ -61,7 +62,6 @@ def __init__(
6162 response : str | None = None ,
6263 exception : str | None = None ,
6364 ):
64- message = "HTTP request failed."
6565 details = {}
6666 details ["method" ] = method
6767 details ["url" ] = url
@@ -77,8 +77,17 @@ def __init__(
7777 details ["response" ] = response
7878 if exception is not None :
7979 details ["exception" ] = exception
80+ explain = ""
81+ if response is not None :
82+ try :
83+ response_json = json .loads (response ) # type: ignore
84+ if "message" in response_json :
85+ explain = f" - { response_json ['message' ]} "
86+ except Exception : # pylint: disable=broad-except
87+ pass
88+ message = f"HTTP request failed ({ method } { url } : { status_code } { explain } )."
8089 super ().__init__ (
81- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
90+ status_code = status_code or status .HTTP_500_INTERNAL_SERVER_ERROR ,
8291 message = message ,
8392 details = details ,
8493 )
@@ -129,11 +138,11 @@ class UnknownKeyIDError(DSKEException):
129138 Exception raised when an unknown key ID is provided.
130139 """
131140
132- def __init__ (self , key_id : str ):
141+ def __init__ (self , key_id : UUID ):
133142 super ().__init__ (
134143 status_code = status .HTTP_400_BAD_REQUEST ,
135144 message = "Unknown key ID" ,
136- details = {"key_ID" : key_id },
145+ details = {"key_ID" : str ( key_id ) },
137146 )
138147
139148
@@ -147,6 +156,7 @@ def __init__(
147156 key_id : UUID ,
148157 nr_successful_shares : int ,
149158 nr_required_shares : int ,
159+ status_code : int ,
150160 causes = List [str ],
151161 ):
152162 details = {
@@ -157,7 +167,7 @@ def __init__(
157167 if causes :
158168 details ["causes" ] = causes
159169 super ().__init__ (
160- status_code = status . HTTP_503_SERVICE_UNAVAILABLE ,
170+ status_code = status_code ,
161171 message = "Could not scatter enough shares for key." ,
162172 details = details ,
163173 )
@@ -173,6 +183,7 @@ def __init__(
173183 key_id : UUID ,
174184 nr_successful_shares : int ,
175185 nr_required_shares : int ,
186+ status_code : int ,
176187 causes = List [str ],
177188 ):
178189 details = {
@@ -183,7 +194,7 @@ def __init__(
183194 if causes :
184195 details ["causes" ] = causes
185196 super ().__init__ (
186- status_code = status . HTTP_503_SERVICE_UNAVAILABLE ,
197+ status_code = status_code ,
187198 message = "Could not gather enough shares for key." ,
188199 details = details ,
189200 )
0 commit comments