Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

better error output when JSON is included in error

Dane Schneider7804d8614f98
diff --git a/app/cli/term/errors.go b/app/cli/term/errors.go
index c049155c..30eea9d6 100644
--- a/app/cli/term/errors.go
+++ b/app/cli/term/errors.go
@@ -1,6 +1,7 @@
package term
import (
+ "encoding/json"
"fmt"
"os"
"strings"
@@ -24,11 +25,6 @@ func SetConvertTrialFn(fn func()) {
convertTrial = fn
}
-func OutputNoOpenAIApiKeyMsgAndExit() {
- fmt.Fprintln(os.Stderr, color.New(color.Bold, ColorHiRed).Sprintln("\n🚨 OPENAI_API_KEY environment variable is not set.")+color.New().Sprintln("\nSet it with:\n\nexport OPENAI_API_KEY=your-api-key\n\nThen try again.\n\nπŸ‘‰ If you don't have an OpenAI account, sign up here β†’ https://platform.openai.com/signup\n\nπŸ”‘ Generate an api key here β†’ https://platform.openai.com/api-keys"))
- os.Exit(1)
-}
-
func OutputSimpleError(msg string, args ...interface{}) {
msg = fmt.Sprintf(msg, args...)
fmt.Fprintln(os.Stderr, color.New(ColorHiRed, color.Bold).Sprint("🚨 "+shared.Capitalize(msg)))
@@ -36,8 +32,12 @@ func OutputSimpleError(msg string, args ...interface{}) {
func OutputErrorAndExit(msg string, args ...interface{}) {
StopSpinner()
+
msg = fmt.Sprintf(msg, args...)
+ msg = strings.ReplaceAll(msg, "status code:", "status code")
+ msg = strings.ReplaceAll(msg, ", body:", ":")
+
displayMsg := ""
errorParts := strings.Split(msg, ": ")
@@ -46,12 +46,25 @@ func OutputErrorAndExit(msg string, args ...interface{}) {
if len(errorParts) > 1 {
var lastPart string
i := 0
- for _, part := range errorParts {
+ for idx, part := range errorParts {
// don't repeat the same error message
if _, ok := addedErrors[strings.ToLower(part)]; ok {
continue
}
+ tail := strings.Join(errorParts[idx:], ": ")
+ if maybeJSON(tail) {
+ prettyJSON := prettyJSON(tail)
+ indent := strings.Repeat(" ", i)
+
+ // prepend indent to **each** line in the pretty JSON
+ indentedJSON := strings.ReplaceAll(prettyJSON, "\n", "\n"+indent+" ")
+
+ // now write the block
+ displayMsg += "\n" + indent + "β†’ " + indentedJSON
+ break
+ }
+
if len(lastPart) < 10 && i > 0 {
lastPart = lastPart + ": " + part
displayMsg += ": " + part
@@ -201,3 +214,23 @@ func HandleApiError(apiError *shared.ApiError) {
StopSpinner()
OutputErrorAndExit(apiError.Msg)
}
+
+func maybeJSON(s string) bool {
+ s = strings.TrimSpace(s)
+ if strings.HasPrefix(s, "{") && strings.HasSuffix(s, "}") {
+ return true
+ }
+ if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
+ return true
+ }
+ return false
+}
+
+func prettyJSON(s string) string {
+ var v any
+ if err := json.Unmarshal([]byte(s), &v); err != nil {
+ return s // not JSON
+ }
+ out, _ := json.MarshalIndent(v, "", " ")
+ return string(out)
+}