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
3232 Key ,
3333)
3434from pysonar_scanner .utils import remove_trailing_slash , OsStr , ArchStr
35- from pysonar_scanner .exceptions import SonarQubeApiException , InconsistentConfiguration
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"
@@ -185,6 +189,16 @@ def __init__(self, base_urls: BaseUrls, token: str):
185189 self .base_urls = base_urls
186190 self .auth = BearerAuth (token )
187191
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+
188202 def is_sonar_qube_cloud (self ) -> bool :
189203 return self .base_urls .is_sonar_qube_cloud
190204
@@ -197,7 +211,7 @@ def get_analysis_version(self) -> SQVersion:
197211 res .raise_for_status ()
198212 return SQVersion .from_str (res .text )
199213 except requests .RequestException as e :
200- raise SonarQubeApiException ( "Error while fetching the analysis version" ) from e
214+ self . __raise_exception ( e )
201215
202216 def get_analysis_engine (self ) -> EngineInfo :
203217 try :
@@ -210,7 +224,7 @@ def get_analysis_engine(self) -> EngineInfo:
210224 raise SonarQubeApiException ("Invalid response from the server" )
211225 return EngineInfo (filename = json ["filename" ], sha256 = json ["sha256" ])
212226 except requests .RequestException as e :
213- raise SonarQubeApiException ( "Error while fetching the analysis engine information" ) from e
227+ self . __raise_exception ( e )
214228
215229 def download_analysis_engine (self , handle : typing .BinaryIO ) -> None :
216230 """
@@ -226,7 +240,7 @@ def download_analysis_engine(self, handle: typing.BinaryIO) -> None:
226240 )
227241 self .__download_file (res , handle )
228242 except requests .RequestException as e :
229- raise SonarQubeApiException ( "Error while fetching the analysis engine" ) from e
243+ self . __raise_exception ( e )
230244
231245 def get_analysis_jres (self , os : OsStr , arch : ArchStr ) -> list [JRE ]:
232246 try :
@@ -241,7 +255,7 @@ def get_analysis_jres(self, os: OsStr, arch: ArchStr) -> list[JRE]:
241255 json_array = res .json ()
242256 return [JRE .from_dict (jre ) for jre in json_array ]
243257 except (requests .RequestException , KeyError ) as e :
244- raise SonarQubeApiException ( "Error while fetching the analysis version" ) from e
258+ self . __raise_exception ( e )
245259
246260 def download_analysis_jre (self , id : str , handle : typing .BinaryIO ) -> None :
247261 """
@@ -257,7 +271,7 @@ def download_analysis_jre(self, id: str, handle: typing.BinaryIO) -> None:
257271 )
258272 self .__download_file (res , handle )
259273 except requests .RequestException as e :
260- raise SonarQubeApiException ( "Error while fetching the JRE" ) from e
274+ self . __raise_exception ( e )
261275
262276 def __download_file (self , res : requests .Response , handle : typing .BinaryIO ) -> None :
263277 res .raise_for_status ()
0 commit comments