Skip to content

Commit 2f3cc23

Browse files
authored
fix: nil error SEGFAULT (#42)
* fix: nil error SEGFAULT Also, improves error message readability for unknown request * chore: Address Copilot feedback * Update notecard/validate.go Notecard API is case sensitive
1 parent 9a8a5a4 commit 2f3cc23

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

.vscode/launch.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"inputs": [
7+
{
8+
"id": "requestJson",
9+
"type": "promptString",
10+
"description": "Enter the JSON request string for the CLI",
11+
"default": "{\"req\":\"card.attn\",\"mode\":\"watchdog\",\"seconds\":30}"
12+
}
13+
],
14+
"configurations": [
15+
{
16+
"name": "Launch Notecard CLI",
17+
"type": "go",
18+
"request": "launch",
19+
"mode": "auto",
20+
"program": "${workspaceFolder}/notecard",
21+
"args": ["-req", "${input:requestJson}"],
22+
"env": {
23+
"BLUES": "[email protected]"
24+
}
25+
},
26+
{
27+
"name": "Launch Notehub CLI",
28+
"type": "go",
29+
"request": "launch",
30+
"mode": "auto",
31+
"program": "${workspaceFolder}/notehub",
32+
"args": ["-req", "${input:requestJson}"]
33+
}
34+
]
35+
}

notecard/validate.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func fetchAndCacheSchema(url string, verbose bool) (io.Reader, error) {
8181
}
8282

8383
func formatErrorMessage(reqType string, errUnformatted error) (err error) {
84+
// Check if the error is nil
85+
if errUnformatted == nil {
86+
return nil
87+
}
88+
89+
// Convert the error to a string
8490
errMsg := errUnformatted.Error()
8591

8692
// Define constants
@@ -230,15 +236,22 @@ func resolveSchemaError(reqMap map[string]interface{}, verbose bool) (err error)
230236
} else if reqTypeStr == "" {
231237
err = fmt.Errorf("no request type specified")
232238
} else {
233-
var reqSchema *jsonschema.Schema
234-
reqSchema, err = jsonschema.Compile(filepath.Join(cacheDir, reqTypeStr+".req.notecard.api.json"))
235-
if err == nil {
236-
err = reqSchema.Validate(reqMap)
237-
if !verbose {
238-
err = formatErrorMessage(reqTypeStr, err)
239+
// Validate against the specific request schema
240+
schemaPath := filepath.Join(cacheDir, reqTypeStr+".req.notecard.api.json")
241+
if _, err = os.Stat(schemaPath); os.IsNotExist(err) {
242+
err = fmt.Errorf("unknown request type: %s", reqTypeStr)
243+
} else if err == nil {
244+
var reqSchema *jsonschema.Schema
245+
reqSchema, err = jsonschema.Compile(schemaPath)
246+
if err == nil {
247+
err = reqSchema.Validate(reqMap)
248+
if err != nil && !verbose {
249+
err = formatErrorMessage(reqTypeStr, err)
250+
}
239251
}
240252
}
241253
}
254+
242255
return err
243256
}
244257

0 commit comments

Comments
 (0)