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 SonarQubeApiException , InconsistentConfiguration
34+ from pysonar_scanner .exceptions import (
35+ SonarQubeApiException ,
36+ InconsistentConfiguration ,
37+ SonarQubeApiUnauthroizedException ,
38+ )
3539from pysonar_scanner .utils import Arch , Os , remove_trailing_slash
3640
3741GLOBAL_SONARCLOUD_URL = "https://sonarcloud.io"
@@ -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 : Optional [Os ] = None , arch : Optional [Arch ] = None ) -> list [JRE ]:
232246 try :
@@ -244,7 +258,7 @@ def get_analysis_jres(self, os: Optional[Os] = None, arch: Optional[Arch] = None
244258 json_array = res .json ()
245259 return [JRE .from_dict (jre ) for jre in json_array ]
246260 except (requests .RequestException , KeyError ) as e :
247- raise SonarQubeApiException ( "Error while fetching the analysis version" ) from e
261+ self . __raise_exception ( e )
248262
249263 def download_analysis_jre (self , id : str , handle : typing .BinaryIO ) -> None :
250264 """
@@ -260,7 +274,7 @@ def download_analysis_jre(self, id: str, handle: typing.BinaryIO) -> None:
260274 )
261275 self .__download_file (res , handle )
262276 except requests .RequestException as e :
263- raise SonarQubeApiException ( "Error while fetching the JRE" ) from e
277+ self . __raise_exception ( e )
264278
265279 def __download_file (self , res : requests .Response , handle : typing .BinaryIO ) -> None :
266280 res .raise_for_status ()
0 commit comments