def _create_pyzeebe_error_from_grpc_error(grpc_error: grpc.aio.AioRpcError) -> PyZeebeError: if is_error_status(grpc_error, grpc.StatusCode.RESOURCE_EXHAUSTED): return ZeebeBackPressureError() elif is_error_status(grpc_error, grpc.StatusCode.UNAVAILABLE, grpc.StatusCode.CANCELLED): return ZeebeGatewayUnavailableError() elif is_error_status(grpc_error, grpc.StatusCode.INTERNAL): return ZeebeInternalError() return UnkownGrpcStatusCodeError(grpc_error)
async def throw_error(self, job_key: int, message: str, error_code: str = "") -> ThrowErrorResponse: try: return await self._gateway_stub.ThrowError( ThrowErrorRequest(jobKey=job_key, errorMessage=message, errorCode=error_code) ) except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.NOT_FOUND): raise JobNotFoundError(job_key=job_key) from grpc_error elif is_error_status(grpc_error, grpc.StatusCode.FAILED_PRECONDITION): raise JobAlreadyDeactivatedError(job_key=job_key) from grpc_error await self._handle_grpc_error(grpc_error)
async def fail_job(self, job_key: int, retries: int, message: str) -> FailJobResponse: try: return await self._gateway_stub.FailJob( FailJobRequest(jobKey=job_key, retries=retries, errorMessage=message) ) except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.NOT_FOUND): raise JobNotFoundError(job_key=job_key) from grpc_error elif is_error_status(grpc_error, grpc.StatusCode.FAILED_PRECONDITION): raise JobAlreadyDeactivatedError(job_key=job_key) from grpc_error await self._handle_grpc_error(grpc_error)
async def complete_job(self, job_key: int, variables: Dict) -> CompleteJobResponse: try: return await self._gateway_stub.CompleteJob( CompleteJobRequest(jobKey=job_key, variables=json.dumps(variables)) ) except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.NOT_FOUND): raise JobNotFoundError(job_key=job_key) from grpc_error elif is_error_status(grpc_error, grpc.StatusCode.FAILED_PRECONDITION): raise JobAlreadyDeactivatedError(job_key=job_key) from grpc_error await self._handle_grpc_error(grpc_error)
async def _create_process_errors( self, grpc_error: grpc.aio.AioRpcError, bpmn_process_id: str, version: int, variables: Dict ) -> None: if is_error_status(grpc_error, grpc.StatusCode.NOT_FOUND): raise ProcessDefinitionNotFoundError(bpmn_process_id=bpmn_process_id, version=version) from grpc_error elif is_error_status(grpc_error, grpc.StatusCode.INVALID_ARGUMENT): raise InvalidJSONError( f"Cannot start process: {bpmn_process_id} with version {version}. Variables: {variables}" ) from grpc_error elif is_error_status(grpc_error, grpc.StatusCode.FAILED_PRECONDITION): raise ProcessDefinitionHasNoStartEventError(bpmn_process_id=bpmn_process_id) from grpc_error elif is_error_status(grpc_error, grpc.StatusCode.DEADLINE_EXCEEDED): raise ProcessTimeoutError(bpmn_process_id) from grpc_error await self._handle_grpc_error(grpc_error)
async def activate_jobs( self, task_type: str, worker: str, timeout: int, max_jobs_to_activate: int, variables_to_fetch: List[str], request_timeout: int, ) -> AsyncGenerator[Job, None]: try: async for response in self._gateway_stub.ActivateJobs( ActivateJobsRequest( type=task_type, worker=worker, timeout=timeout, maxJobsToActivate=max_jobs_to_activate, fetchVariable=variables_to_fetch, requestTimeout=request_timeout, ) ): for raw_job in response.jobs: job = self._create_job_from_raw_job(raw_job) logger.debug("Got job: %s from zeebe", job) yield job except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.INVALID_ARGUMENT): raise ActivateJobsRequestInvalidError(task_type, worker, timeout, max_jobs_to_activate) from grpc_error await self._handle_grpc_error(grpc_error)
async def cancel_process_instance(self, process_instance_key: int) -> None: try: await self._gateway_stub.CancelProcessInstance( CancelProcessInstanceRequest(processInstanceKey=process_instance_key) ) except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.NOT_FOUND): raise ProcessInstanceNotFoundError(process_instance_key=process_instance_key) from grpc_error await self._handle_grpc_error(grpc_error)
async def deploy_process(self, *process_file_path: str) -> DeployProcessResponse: try: return await self._gateway_stub.DeployProcess( DeployProcessRequest( processes=[await result for result in map(_create_process_request, process_file_path)] ) ) except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.INVALID_ARGUMENT): raise ProcessInvalidError() from grpc_error await self._handle_grpc_error(grpc_error)
async def publish_message( self, name: str, correlation_key: str, time_to_live_in_milliseconds: int, variables: Dict, message_id: str = None, ) -> PublishMessageResponse: try: return await self._gateway_stub.PublishMessage( PublishMessageRequest( name=name, correlationKey=correlation_key, messageId=message_id, timeToLive=time_to_live_in_milliseconds, variables=json.dumps(variables), )) except grpc.aio.AioRpcError as grpc_error: if is_error_status(grpc_error, grpc.StatusCode.ALREADY_EXISTS): raise MessageAlreadyExistsError() from grpc_error await self._handle_grpc_error(grpc_error)
def test_with_matching_and_unmatching_code_returns_true(self): assert grpc_utils.is_error_status(self.error, self.matching_status_code, self.unmatching_status_code)
def test_with_no_matching_code_returns_false(self): assert not grpc_utils.is_error_status(self.error, self.unmatching_status_code)