Created
January 9, 2017 18:55
-
-
Save aalvesjr/04b072071806e875a2230485dd68334f to your computer and use it in GitHub Desktop.
Testing errors for API in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type Person struct { | |
Name string | |
Errors map[string][]string `json:"-"` | |
} | |
func main() { | |
p := Person{"", map[string][]string{}} | |
// simulating the p.Validate() where the Errors array should be filled | |
p.Errors["name"] = append(p.Errors["name"], "blank") | |
p.Errors["name"] = append(p.Errors["name"], "invalid") | |
j, _ := json.Marshal(p) | |
e, _ := json.Marshal(p.Errors) | |
fmt.Println("resource:", string(j), ", errors:", string(e)) | |
} | |
// => resource: {"Name":""} , errors: {"name":["blank","invalid"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment