"""Errors utilities."""fromtypingimportOptional,Dict,UnionErrorCodeType=Union[int,str]defis_http_standard_error(error_code:ErrorCodeType):"""Returns true if code is in standard http error codes range."""result=Falseifisinstance(error_code,int)and(600>error_code>=100):result=Truereturnresult
DEFAULT_ERROR_MESSAGE:str="Something went wrong."error_mapping:Dict[ErrorCodeType,str]={ErrorCodes.AUTH1001:"Connection error. Make sure configuration ""(host and auth details) is correct.",ErrorCodes.HTTP_STD_ERROR:"Http bad request.",ErrorCodes.JSON1001:"Error occurred during decoding server json response.",}defformat_err_msg(code:ErrorCodeType,details:Optional[str]=None):"""Formats error message. Args: code: error code details: extra information about error Returns: formatted string """message=(error_mapping[ErrorCodes.HTTP_STD_ERROR]ifis_http_standard_error(code)elseerror_mapping.get(code,DEFAULT_ERROR_MESSAGE))result=f"\n| Message: {message}"ifcode:result+=f"\n| Code: {code}"ifdetails:result+=f"\n| Details: {details}"returnresult