1919#
2020import typing
2121from dataclasses import dataclass
22- from typing import Optional , TypedDict
22+ from typing import NoReturn , Optional , TypedDict
2323
2424import requests
2525import requests .auth
3131 SONAR_REGION ,
3232 Key ,
3333)
34- from pysonar_scanner .exceptions import MissingKeyException , SonarQubeApiException , InconsistentConfiguration
3534from pysonar_scanner .utils import remove_trailing_slash , OsStr , ArchStr
35+ from pysonar_scanner .exceptions import (
36+ SonarQubeApiException ,
37+ InconsistentConfiguration ,
38+ SonarQubeApiUnauthroizedException ,
39+ )
3640
3741GLOBAL_SONARCLOUD_URL = "https://sonarcloud.io"
3842US_SONARCLOUD_URL = "https://sonarqube.us"
@@ -98,18 +102,15 @@ class JRE:
98102
99103 @staticmethod
100104 def from_dict (dict : dict ) -> "JRE" :
101- try :
102- return JRE (
103- id = dict ["id" ],
104- filename = dict ["filename" ],
105- sha256 = dict ["sha256" ],
106- java_path = dict ["javaPath" ],
107- os = dict ["os" ],
108- arch = dict ["arch" ],
109- download_url = dict .get ("downloadUrl" , None ),
110- )
111- except KeyError as e :
112- raise MissingKeyException (f"Missing key in dictionary { dict } " ) from e
105+ return JRE (
106+ id = dict ["id" ],
107+ filename = dict ["filename" ],
108+ sha256 = dict ["sha256" ],
109+ java_path = dict ["javaPath" ],
110+ os = dict ["os" ],
111+ arch = dict ["arch" ],
112+ download_url = dict .get ("downloadUrl" , None ),
113+ )
113114
114115
115116ApiConfiguration = TypedDict (
@@ -188,6 +189,16 @@ def __init__(self, base_urls: BaseUrls, token: str):
188189 self .base_urls = base_urls
189190 self .auth = BearerAuth (token )
190191
192+ def __raise_exception (self , exception : Exception ) -> NoReturn :
193+ if (
194+ isinstance (exception , requests .RequestException )
195+ and exception .response is not None
196+ and exception .response .status_code == 401
197+ ):
198+ raise SonarQubeApiUnauthroizedException .create_default (self .base_urls .base_url ) from exception
199+ else :
200+ raise SonarQubeApiException ("Error while fetching the analysis version" ) from exception
201+
191202 def is_sonar_qube_cloud (self ) -> bool :
192203 return self .base_urls .is_sonar_qube_cloud
193204
@@ -200,7 +211,7 @@ def get_analysis_version(self) -> SQVersion:
200211 res .raise_for_status ()
201212 return SQVersion .from_str (res .text )
202213 except requests .RequestException as e :
203- raise SonarQubeApiException ( "Error while fetching the analysis version" ) from e
214+ self . __raise_exception ( e )
204215
205216 def get_analysis_engine (self ) -> EngineInfo :
206217 try :
@@ -213,7 +224,7 @@ def get_analysis_engine(self) -> EngineInfo:
213224 raise SonarQubeApiException ("Invalid response from the server" )
214225 return EngineInfo (filename = json ["filename" ], sha256 = json ["sha256" ])
215226 except requests .RequestException as e :
216- raise SonarQubeApiException ( "Error while fetching the analysis engine information" ) from e
227+ self . __raise_exception ( e )
217228
218229 def download_analysis_engine (self , handle : typing .BinaryIO ) -> None :
219230 """
@@ -229,7 +240,7 @@ def download_analysis_engine(self, handle: typing.BinaryIO) -> None:
229240 )
230241 self .__download_file (res , handle )
231242 except requests .RequestException as e :
232- raise SonarQubeApiException ( "Error while fetching the analysis engine" ) from e
243+ self . __raise_exception ( e )
233244
234245 def get_analysis_jres (self , os : OsStr , arch : ArchStr ) -> list [JRE ]:
235246 try :
@@ -243,8 +254,8 @@ def get_analysis_jres(self, os: OsStr, arch: ArchStr) -> list[JRE]:
243254 res .raise_for_status ()
244255 json_array = res .json ()
245256 return [JRE .from_dict (jre ) for jre in json_array ]
246- except (requests .RequestException , MissingKeyException ) as e :
247- raise SonarQubeApiException ( "Error while fetching the analysis version" ) from e
257+ except (requests .RequestException , KeyError ) as e :
258+ self . __raise_exception ( e )
248259
249260 def download_analysis_jre (self , id : str , handle : typing .BinaryIO ) -> None :
250261 """
@@ -260,7 +271,7 @@ def download_analysis_jre(self, id: str, handle: typing.BinaryIO) -> None:
260271 )
261272 self .__download_file (res , handle )
262273 except requests .RequestException as e :
263- raise SonarQubeApiException ( "Error while fetching the JRE" ) from e
274+ self . __raise_exception ( e )
264275
265276 def __download_file (self , res : requests .Response , handle : typing .BinaryIO ) -> None :
266277 res .raise_for_status ()
0 commit comments