Open danielniccoli opened 2 months ago
I'm not a Python expert, but could it be that the ValueError
type information isn't being preserved, causing the except
block to be skipped?
I ask because looking at the documentation here, it shows an except
block but without a specific error type.
I'm specifically wondering if this might work:
# Orchestrator
@myApp.orchestration_trigger(context_name="context")
def hello_orchestrator(context):
result1 = yield context.call_activity("hello", "Seattle")
try:
result2 = yield context.call_activity("hello", "Tokyo")
except: # handle all error types
result2 = "An error was thrown!"
result3 = yield context.call_activity("hello", "London")
return [result1, result2, result3]
Yes, I can confirm that except Exception as e:
does catch the error and it is of type Exception
, instead of type ValueError
.
Here are the logs:
Executing task: .venv\Scripts\activate ; func host start
Found Python version 3.11.8 (py).
Azure Functions Core Tools
Core Tools Version: 4.0.5611 Commit hash: N/A +591b8aec842e333a87ea9e23ba390bb5effe0655 (64-bit)
Function Runtime Version: 4.31.1.22191
[2024-05-02T08:27:23.874Z] 0.01s - Debugger warning: It seems that frozen modules are being used, which may
[2024-05-02T08:27:23.875Z] 0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
[2024-05-02T08:27:23.876Z] 0.00s - to python to disable frozen modules.
[2024-05-02T08:27:23.878Z] 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
[2024-05-02T08:27:23.966Z] Worker process started and initialized.
Functions:
http_start: http://localhost:7071/api/orchestrators/{functionName}
hello: activityTrigger
hello_orchestrator: orchestrationTrigger
For detailed output, run func with --verbose flag.
[2024-05-02T08:27:28.941Z] Host lock lease acquired by instance ID '00000000000000000000000067BB950F'.
[2024-05-02T08:27:51.568Z] Executing 'Functions.http_start' (Reason='This function was programmatically called via the host APIs.', Id=39efaa79-e648-4acc-ad3f-992132aaa7e7)
[2024-05-02T08:27:51.767Z] Executed 'Functions.http_start' (Succeeded, Id=39efaa79-e648-4acc-ad3f-992132aaa7e7, Duration=216ms)
[2024-05-02T08:27:51.790Z] Executing 'Functions.hello_orchestrator' (Reason='(null)', Id=d27703e7-cb42-4fd6-bb1f-6f66dbfb2fa1)
[2024-05-02T08:27:51.827Z] Executed 'Functions.hello_orchestrator' (Succeeded, Id=d27703e7-cb42-4fd6-bb1f-6f66dbfb2fa1, Duration=41ms)
[2024-05-02T08:27:51.870Z] Executing 'Functions.hello' (Reason='(null)', Id=1b393e57-623d-4160-9f73-b6bf9a9175f2)
[2024-05-02T08:27:51.874Z] Executed 'Functions.hello' (Succeeded, Id=1b393e57-623d-4160-9f73-b6bf9a9175f2, Duration=5ms)
[2024-05-02T08:27:51.911Z] Executing 'Functions.hello_orchestrator' (Reason='(null)', Id=afbc774b-9033-4ec6-908f-f611d8822518)
[2024-05-02T08:27:51.922Z] Executed 'Functions.hello_orchestrator' (Succeeded, Id=afbc774b-9033-4ec6-908f-f611d8822518, Duration=11ms)
[2024-05-02T08:27:51.943Z] Executing 'Functions.hello' (Reason='(null)', Id=7a95903b-babb-4146-9d1c-13c17fb77265)
[2024-05-02T08:27:51.963Z] Executed 'Functions.hello' (Failed, Id=7a95903b-babb-4146-9d1c-13c17fb77265, Duration=18ms)
[2024-05-02T08:27:51.965Z] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result: Failure
Exception: ValueError: This error is thrown on purpose!
Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 505, in _handle__invocation_request
call_result = await self._loop.run_executor(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\niccodan\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 778, in _run_sync_func
return ExtensionManager.get_sync_invocation_wrapper(context,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
result = function(**args)
^^^^^^^^^^^^^^^^
File "C:\code\sample_df_error_handling\function_app.py", line 31, in hello
raise ValueError("This error is thrown on purpose!")
.
[2024-05-02T08:27:51.978Z] d371bfcf602a4b0fbe694770472d5d8e: Function 'hello (Activity)' failed with an error. Reason: System.Exception: ValueError: This error is thrown on purpose!
[2024-05-02T08:27:51.979Z] ---> Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure
Exception: ValueError: This error is thrown on purpose!
Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 505, in _handle__invocation_request
call_result = await self._loop.run_executor(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\niccodan\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 778, in _run_sync_func
return ExtensionManager.get_sync_invocation_wrapper(context,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
result = function(**args)
^^^^^^^^^^^^^^^^
File "C:\code\sample_df_error_handling\function_app.py", line 31, in hello
raise ValueError("This error is thrown on purpose!")
[2024-05-02T08:27:51.981Z] at Microsoft.Azure.WebJobs.Script.Description.WorkerFunctionInvoker.InvokeCore(Object[] parameters, FunctionInvocationContext context) in /_/src/WebJobs.Script/Description/Workers/WorkerFunctionInvoker.cs:line 101
[2024-05-02T08:27:51.982Z] at Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters) in /_/src/WebJobs.Script/Description/FunctionInvokerBase.cs:line 82
[2024-05-02T08:27:51.982Z] at Microsoft.Azure.WebJobs.Script.Description.FunctionGenerator.Coerce[T](Task`1 src) in /_/src/WebJobs.Script/Description/FunctionGenerator.cs:line 225
[2024-05-02T08:27:51.983Z] at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
[2024-05-02T08:27:51.984Z] at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeWithTimeoutAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 581
[2024-05-02T08:27:51.985Z] at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 527
[2024-05-02T08:27:51.986Z] at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 306
[2024-05-02T08:27:51.987Z] --- End of inner exception stack trace ---. IsReplay: False. State: Failed. RuntimeStatus: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.12.0. SequenceNumber: 12. TaskEventId: 1
[2024-05-02T08:27:52.027Z] Executing 'Functions.hello_orchestrator' (Reason='(null)', Id=b588f41c-46d2-4b3b-8270-0899046e1200)
[2024-05-02T08:27:52.043Z] Executed 'Functions.hello_orchestrator' (Succeeded, Id=b588f41c-46d2-4b3b-8270-0899046e1200, Duration=16ms)
[2024-05-02T08:27:52.062Z] Executing 'Functions.hello' (Reason='(null)', Id=3054a92a-16ce-44e4-9037-84075d7508f1)
[2024-05-02T08:27:52.066Z] Executed 'Functions.hello' (Succeeded, Id=3054a92a-16ce-44e4-9037-84075d7508f1, Duration=4ms)
[2024-05-02T08:27:52.086Z] Executing 'Functions.hello_orchestrator' (Reason='(null)', Id=47cd7f2f-a45d-4962-9673-db3ab6b23c8a)
[2024-05-02T08:27:52.096Z] Executed 'Functions.hello_orchestrator' (Succeeded, Id=47cd7f2f-a45d-4962-9673-db3ab6b23c8a, Duration=10ms)
The orchestration completes with the runtime status of Completed
(successfully).
{
"name": "hello_orchestrator",
"instanceId": "d371bfcf602a4b0fbe694770472d5d8e",
"runtimeStatus": "Completed",
"input": null,
"customStatus": null,
"output": [
"Hello Seattle",
"An error was thrown!",
"Hello London"
],
"createdTime": "2024-05-02T08:27:51Z",
"lastUpdatedTime": "2024-05-02T08:27:52Z"
}
However, the Activity still fails. The log shows System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result: Failure
.
In Python, it is idiomatic to return errors as exceptions and let them be handled by the calling function. This means that the Activity should not fail when an exception is raised, but rather when it is not handled by the calling Orchestrator, or a higher function.
The status does reflect that correctly with the runtime status of Completed
. but the logs paint a different picture and make it seem like there was an unhandled issue in the code - which is not true. Thus, the raised Exception inside the Activity should not be printed to the output at all.
Then of course, there's the issue that the ValueError is raised as an Exception for some reason.
π Describe the bug
I cannot catch an error thrown inside an activity function in the orchestrator.
π€ Expected behavior
The error should have been caught and handled by the
except
in the orchestrator function.β Steps to reproduce
Logs