// SPDX-License-Identifier: Apache-2.0 package main import ( "encoding/json" "strings" cli "git.ipng.ch/ipng/golang-cli" ) // formatError returns a user-friendly error string. gRPC status errors are // unwrapped to show only the server's message (no "rpc error: code = ..." // boilerplate). In JSON mode it returns the message as a {"error": "..."} // document (so failures are machine-readable, matching the data/`{}` a success // prints); otherwise it wraps the message in red when color is enabled. // App.FormatError prints the result. func formatError(err error) string { msg := err.Error() // google.golang.org/grpc/status errors format as: // rpc error: code = desc = if i := strings.Index(msg, " desc = "); i >= 0 { msg = msg[i+len(" desc = "):] } if cli.IsJSON() { b, _ := json.Marshal(map[string]string{"error": msg}) return string(b) } if cli.ColorEnabled() { return cli.Red + msg + cli.Reset } return msg }