Skip to content

Commit

Permalink
always return errors as json if request being processed (#13)
Browse files Browse the repository at this point in the history
* if doing request, return response when not verbose

* all error returns with -req now result in json
  • Loading branch information
rayozzie authored Mar 1, 2023
1 parent bbda0b1 commit 813f9ab
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions notecard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"io/ioutil"
"strings"
"syscall"
Expand Down Expand Up @@ -164,14 +165,8 @@ func main() {
}
notecard.InitialDebugMode = actionVerbose
card, err = notecard.Open(lib.Config.Interface, lib.Config.Port, configVal)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(exitFail)

}

// Process non-config commands
err = nil
var rsp notecard.Request

// The timouts in the note-go library are set under the assumption that
Expand Down Expand Up @@ -576,6 +571,12 @@ func main() {
rsp, err = card.TransactionRequest(req)
}
}
if !actionVerbose {
if err == nil {
rspJSON, _ := note.JSONMarshal(rsp)
fmt.Printf("%s\n", rspJSON)
}
}
if err == nil && actionOutput != "" && rsp.Payload != nil {
err = ioutil.WriteFile(actionOutput, *rsp.Payload, 0644)
}
Expand Down Expand Up @@ -653,8 +654,12 @@ func main() {

// Process errors
if err != nil {
if actionRequest != "" && !actionVerbose {
fmt.Printf("{\"err\":%s}\n", strconv.Quote(err.Error()))
} else {
fmt.Printf("%s\n", err)
os.Exit(exitFail)
}
}

// Success
Expand Down

0 comments on commit 813f9ab

Please sign in to comment.