Merge remote-tracking branch 'origin/risto'
This commit is contained in:
45
main.go
45
main.go
@@ -230,7 +230,7 @@ func (c *KVSClient) handleGet(args []string) {
|
||||
fmt.Println(cyan("UUID: "), stored.UUID)
|
||||
fmt.Println(cyan("Timestamp:"), time.UnixMilli(stored.Timestamp).Format(time.RFC3339))
|
||||
fmt.Println(cyan("Data:"))
|
||||
|
||||
|
||||
var pretty bytes.Buffer
|
||||
json.Indent(&pretty, stored.Data, "", " ")
|
||||
fmt.Println(pretty.String())
|
||||
@@ -260,9 +260,8 @@ func (c *KVSClient) handlePut(args []string) {
|
||||
|
||||
// Parse JSON data
|
||||
var data json.RawMessage
|
||||
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
|
||||
fmt.Println(red("Invalid JSON:"), err)
|
||||
fmt.Println("Tip: Use proper JSON format with escaped quotes")
|
||||
if err := json.Unmarshal([]byte(args[1]), &data); err != nil {
|
||||
printJSONError(args[1], err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -388,7 +387,7 @@ func (c *KVSClient) handleMetaSet(args []string) {
|
||||
return
|
||||
}
|
||||
key := args[0]
|
||||
|
||||
|
||||
// Use a map to build the JSON payload for partial updates
|
||||
payload := make(map[string]interface{})
|
||||
|
||||
@@ -572,6 +571,39 @@ func (c *KVSClient) handleUserCreate(args []string) {
|
||||
|
||||
fmt.Println(green("User created successfully"))
|
||||
fmt.Println(cyan("UUID:"), response.UUID)
|
||||
|
||||
// printJSONError displays a user-friendly JSON parsing error with helpful hints
|
||||
func printJSONError(input string, err error) {
|
||||
fmt.Println(red("❌ Invalid JSON:"), err)
|
||||
fmt.Println()
|
||||
|
||||
// Show what was received
|
||||
if len(input) > 100 {
|
||||
fmt.Println(yellow("Received:"), input[:100]+"...")
|
||||
} else {
|
||||
fmt.Println(yellow("Received:"), input)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// Provide helpful hints based on common errors
|
||||
errMsg := err.Error()
|
||||
|
||||
if strings.Contains(errMsg, "looking for beginning of") ||
|
||||
strings.Contains(errMsg, "invalid character") {
|
||||
fmt.Println(cyan("💡 Tip:"), "JSON with spaces needs quotes:")
|
||||
fmt.Println(green(" ✓ Correct:"), "put key '{\"hello\":\"world\"}'")
|
||||
fmt.Println(green(" ✓ Correct:"), "put key '{\"a\":1,\"b\":2}'")
|
||||
fmt.Println(green(" ✓ Compact:"), "put key {\"a\":1,\"b\":2} (no spaces)")
|
||||
fmt.Println(red(" ✗ Wrong: "), "put key {\"a\": 1, \"b\": 2} (spaces without quotes)")
|
||||
} else if strings.Contains(errMsg, "unexpected end of JSON") {
|
||||
fmt.Println(cyan("💡 Tip:"), "JSON appears incomplete or cut off")
|
||||
fmt.Println(green(" Check:"), "Are all brackets/braces matched? {}, []")
|
||||
fmt.Println(green(" Check:"), "Did spaces split your JSON into multiple arguments?")
|
||||
} else {
|
||||
fmt.Println(cyan("💡 Tip:"), "Wrap JSON in quotes for complex values:")
|
||||
fmt.Println(green(" Single quotes:"), "put key '{\"your\":\"json\"}'")
|
||||
fmt.Println(green(" Double quotes:"), "put key \"{\\\"your\\\":\\\"json\\\"}\"")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *KVSClient) handleHelp(args []string) {
|
||||
@@ -761,6 +793,9 @@ func parseCommand(line string) []string {
|
||||
default:
|
||||
current.WriteRune(ch)
|
||||
}
|
||||
|
||||
// Add all other characters (including quotes inside unquoted args)
|
||||
current.WriteByte(ch)
|
||||
}
|
||||
|
||||
// Add the last part if it exists
|
||||
|
Reference in New Issue
Block a user