Azure / Azure/azure-functions-powershell-worker

Null Input Bindings: Need a Method to Trap or Handle them

Open
#282 2 comments 0 reactions 1 assignee Claimed by @AnatoliB View on GitHub
Dominant language
C#
Stars
215
Forks
61
Avg merge
21h 4m
Merged PRs (30d)
6

Description

Given the following function.json:
```json
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
],
"route": "HttpTrigger/{row}"
},
{
"type": "table",
"direction": "in",
"name": "TableIn",
"partitionKey": "HttpTrigger",
"tableName": "HttpTrigger",
"rowKey": "{row}"
}
]
}
```

Which finds a table row "test" if https://myfunction/api/HttpTrigger/test is invoked, this works fine. However, if the table row doesn't exist, there's no way to trap the error and handle it gracefully as the command processor fails to bind the parameter at the worker level, not the script level.

```
[7/16/2019 4:15:31 PM] ERROR: Cannot bind argument to parameter 'InputObject' because it is null.
[7/16/2019 4:15:31 PM] Result: ERROR: Cannot bind argument to parameter 'InputObject' because it is null.
Exception: Cannot bind argument to parameter 'InputObject' because it is null.
Stack: at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)
[7/16/2019 4:15:31 PM] at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
[7/16/2019 4:15:31 PM] at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets)
[7/16/2019 4:15:31 PM] at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn)
[7/16/2019 4:15:31 PM] at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn)
[7/16/2019 4:15:31 PM] at System.Management.Automation.CommandProcessor.Read().
```

### Notes
Interestingly, this only happens if you actually call $TableIn in the script, it doesn't error if it is specified in Param() but never called.
I tried `Get-Variable -ErrorAction Stop` and tried to try/catch it to no avail, the exception happens "below" the script at the worker level.
I also tried a combination of `trap` and `$ErrorActionPreference='stop'` with no luck.

### Potential Solutions

1. Gracefully return a null result in the event an input binding is null (may be confused with an "intentional null")
2. Catch the exception and instead set the variable to the exception and pass the exception so it can be handled in function logic. For instance $TableIn would have been set to a NullPointerException or something. Maybe re-throw the exception inside the script scope at that line if possible so a try/catch can snag it, otherwise logic will have to be
`if $TableIn -is [Exception] {"handle it"}`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.