Azure / Azure/appservice-settings
Improve error message in validateSettings to include JSON parse error details
- Dominant language
- JavaScript
- Stars
- 47
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Description
## Description
When `validateSettings` in `Utils.ts` receives an invalid JSON string, it catches the `JSON.parse` error and throws a generic message that discards the original error details:
```typescript
// src/Utils.ts L3-L15
static validateSettings(customSettings: string, maskInputs?: string) {
try {
var customParsedSettings = JSON.parse(customSettings);
// ...
}
catch (error) {
throw new Error('Given Settings object is not a valid JSON');
}
}
```
This makes it very difficult to debug what is actually wrong with the JSON input.
## Steps to reproduce
1. Create a GitHub secret `DB_CONNECTION_STRING` with a value containing double quotes, for example:
```
Server=tcp:myserver.database.windows.net,1433;Initial Catalog=mydb;Authentication="Active Directory Default";
```
2. Use the action with inline JSON interpolation:
```yaml
- uses: Azure/appservice-settings@v1
with:
app-name: "my-app"
app-settings-json: |
[
{"name": "MyConnection", "value": "${{ secrets.DB_CONNECTION_STRING }}"}
]
```
3. The interpolated JSON becomes invalid because the unescaped double quotes inside the secret break the JSON structure:
```json
[{"name": "MyConnection", "value": "Server=...;Authentication="Active Directory Default";"}]
```
4. The action fails with:
```
Error: Error: Given Settings object is not a valid JSON.
```
No additional context is provided about **where** or **why** the JSON is invalid, which makes debugging much harder than it needs to be.
## Expected behavior
The error message should include the original `JSON.parse` error detail, for example:
```
Error: Given Settings object is not a valid JSON. Parse error: Unexpected token A in JSON at position 198
```
## Suggested fix
```typescript
static validateSettings(customSettings: string, maskInputs?: string) {
try {
var customParsedSettings = JSON.parse(customSettings);
if (maskInputs !== undefined && maskInputs !== "false") {
Utils.maskValues(customParsedSettings);
}
return customParsedSettings;
}
catch (error) {
const detail = error instanceof Error ? error.message : String(error);
throw new Error(`Given Settings object is not a valid JSON. Parse error: ${detail}`);
}
}
```
This is a non-breaking change that only improves the developer experience when debugging invalid JSON inputs.
## Additional context
The user-side workaround is to use `toJSON()` to properly escape the secret value:
```yaml
app-settings-json: |
[
{"name": "MyConnection", "value": ${{ toJSON(secrets.DB_CONNECTION_STRING) }}}
]
```
However, without a meaningful error message, it is very hard to discover that unescaped quotes are the root cause — especially when the symptom appears to be related to spaces in the value.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.