def handle_result(callback): try: return callback.wait_for_completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError( message="Lost connection to IoTHub", cause=e) except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError( message="Could not connect to IoTHub", cause=e) except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError( message="Credentials invalid, could not connect", cause=e) except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError(message="Error in the IoTHub client", cause=e) except pipeline_exceptions.TlsExchangeAuthError as e: raise exceptions.ClientError( message="Error in the IoTHub client due to TLS exchanges.", cause=e) except pipeline_exceptions.ProtocolProxyError as e: raise exceptions.ClientError( message= "Error in the IoTHub client raised due to proxy connections.", cause=e) except Exception as e: raise exceptions.ClientError(message="Unexpected failure", cause=e)
async def handle_result(callback): try: return await callback.completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError(message="Lost connection to IoTHub", cause=e) except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError(message="Could not connect to IoTHub", cause=e) except pipeline_exceptions.NoConnectionError as e: raise exceptions.NoConnectionError(message="Client is not connected to IoTHub", cause=e) except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError(message="Credentials invalid, could not connect", cause=e) except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError(message="Error in the IoTHub client", cause=e) except pipeline_exceptions.TlsExchangeAuthError as e: raise exceptions.ClientError( message="Error in the IoTHub client due to TLS exchanges.", cause=e ) except pipeline_exceptions.ProtocolProxyError as e: raise exceptions.ClientError( message="Error in the IoTHub client raised due to proxy connections.", cause=e ) except pipeline_exceptions.PipelineNotRunning as e: raise exceptions.ClientError(message="Client has already been shut down", cause=e) except Exception as e: raise exceptions.ClientError(message="Unexpected failure", cause=e)
def handle_result(callback): try: return callback.wait_for_completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError( "Lost connection to IoTHub") from e except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError( "Could not connect to IoTHub") from e except pipeline_exceptions.NoConnectionError as e: raise exceptions.NoConnectionError( "Client is not connected to IoTHub") from e except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError( "Credentials invalid, could not connect") from e except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError("Error in the IoTHub client") from e except pipeline_exceptions.TlsExchangeAuthError as e: raise exceptions.ClientError( "Error in the IoTHub client due to TLS exchanges.") from e except pipeline_exceptions.ProtocolProxyError as e: raise exceptions.ClientError( "Error in the IoTHub client raised due to proxy connections." ) from e except pipeline_exceptions.PipelineNotRunning as e: raise exceptions.ClientError( "Client has already been shut down") from e except pipeline_exceptions.OperationCancelled as e: raise exceptions.OperationCancelled( "Operation was cancelled before completion") from e except pipeline_exceptions.OperationTimeout as e: raise exceptions.OperationTimeout( "Could not complete operation before timeout") from e except Exception as e: raise exceptions.ClientError("Unexpected failure") from e
async def handle_result(callback): try: return await callback.completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError(message="Lost connection to IoTHub", cause=e) except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError(message="Could not connect to IoTHub", cause=e) except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError(message="Credentials invalid, could not connect", cause=e) except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError(message="Error in the IoTHub client", cause=e) except Exception as e: raise exceptions.ClientError(message="Unexpected failure", cause=e)
def handle_result(callback): try: return callback.wait_for_completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError( message="Lost connection to the provisioning server", cause=e ) except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError( message="Could not connect to the provisioning server", cause=e ) except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError(message="Credentials invalid, could not connect", cause=e) except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError(message="Error in the provisioning client", cause=e) except Exception as e: raise exceptions.ClientError(message="Unexpected failure", cause=e)
def handle_result(callback): try: return callback.wait_for_completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError("Lost connection to the provisioning server") from e except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError( "Could not connect to the provisioning server" ) from e except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError("Credentials invalid, could not connect") from e except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError("Error in the provisioning client") from e except pipeline_exceptions.OperationTimeout as e: raise exceptions.OperationTimeout("Could not complete operation before timeout") from e except pipeline_exceptions.PipelineNotRunning as e: raise exceptions.ClientError("Client has already been shut down") from e except Exception as e: raise exceptions.ClientError("Unexpected failure") from e
def _check_receive_mode_is_api(self): """Call this function first in EVERY receive API""" with self._client_lock: if self._receive_type is RECEIVE_TYPE_NONE_SET: # Lock the client to ONLY use receive APIs (no handlers) self._receive_type = RECEIVE_TYPE_API elif self._receive_type is RECEIVE_TYPE_HANDLER: raise exceptions.ClientError( "Cannot use receive APIs - receive handler(s) have already been set" ) else: pass
def _check_receive_mode_is_handler(self): """Call this function first in EVERY handler setter""" with self._client_lock: if self._receive_type is RECEIVE_TYPE_NONE_SET: # Lock the client to ONLY use receive handlers (no APIs) self._receive_type = RECEIVE_TYPE_HANDLER # Set the inbox manager to use unified msg receives self._inbox_manager.use_unified_msg_mode = True elif self._receive_type is RECEIVE_TYPE_API: raise exceptions.ClientError( "Cannot set receive handlers - receive APIs have already been used" ) else: pass
async def handle_result(callback): try: return await callback.completion() except pipeline_exceptions.ConnectionDroppedError as e: raise exceptions.ConnectionDroppedError( message="Lost connection to IoTHub", cause=e) except pipeline_exceptions.ConnectionFailedError as e: raise exceptions.ConnectionFailedError( message="Could not connect to IoTHub", cause=e) except pipeline_exceptions.UnauthorizedError as e: raise exceptions.CredentialError( message="Credentials invalid, could not connect", cause=e) except pipeline_exceptions.ProtocolClientError as e: raise exceptions.ClientError(message="Error in the IoTHub client", cause=e) except pipeline_exceptions.OperationTimeout as e: raise exceptions.OperationTimeout( message="Could not complete operation before timeout", cause=e) except pipeline_exceptions.PipelineNotRunning as e: raise exceptions.ClientError( message="Client has already been shut down", cause=e) except Exception as e: raise exceptions.ClientError(message="Unexpected failure", cause=e)
def _replace_user_supplied_sastoken(self, sastoken_str): """ Replaces the pipeline's NonRenewableSasToken with a new one based on a provided sastoken string. Also does validation. This helper only updates the PipelineConfig - it does not reauthorize the connection. """ if not isinstance(self._mqtt_pipeline.pipeline_configuration.sastoken, st.NonRenewableSasToken): raise exceptions.ClientError( "Cannot update sastoken when client was not created with one") # Create new SasToken try: new_token_o = st.NonRenewableSasToken(sastoken_str) except st.SasTokenError as e: new_err = ValueError("Invalid SasToken provided") new_err.__cause__ = e raise new_err # Extract values from SasToken vals = _extract_sas_uri_values(new_token_o.resource_uri) # Validate new token if type(self).__name__ == "IoTHubDeviceClient" and vals["module_id"]: raise ValueError("Provided SasToken is for a module") if type(self ).__name__ == "IoTHubModuleClient" and not vals["module_id"]: raise ValueError("Provided SasToken is for a device") if self._mqtt_pipeline.pipeline_configuration.device_id != vals[ "device_id"]: raise ValueError( "Provided SasToken does not match existing device id") if self._mqtt_pipeline.pipeline_configuration.module_id != vals[ "module_id"]: raise ValueError( "Provided SasToken does not match existing module id") if self._mqtt_pipeline.pipeline_configuration.hostname != vals[ "hostname"]: raise ValueError( "Provided SasToken does not match existing hostname") if new_token_o.expiry_time < int(time.time()): raise ValueError("Provided SasToken has already expired") # Set token # NOTE: We only need to set this on MQTT because this is a reference to the same object # that is stored in HTTP. The HTTP pipeline is updated implicitly. self._mqtt_pipeline.pipeline_configuration.sastoken = new_token_o