Go SDK issues

I've been playing around with using the Go SDK and have ran into an interesting issue while using it. For the most part the SDK has been very easy to use, but when I try to retrieve an object from a bucket, and that object doesn't exist, I have to inspect the string of the error message to determine that. It would be nice if there was a way to see if the object exists when a Get fails without having to inspect a string.

Here is an example to illustrate what I mean:

type User struct {
    Id string `json:"id"`
    Name string `json:"name"`
}
 
func UserHandler(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id := vars["id"]
 
    user := User{}
 
    err := userBucket.Get(id, &user)
    if err != nil {
 
        // Here I have to look at the contents of the Error string.
        // It would be nice if there was a way to figure out if the object
        // exists programatically without having to search through a string
        if strings.Contains(err.Error(), "KEY_ENOENT") {
            fmt.Printf("The entry %s does not exist\n", id)
            http.NotFound(w, r)
            return
        }
        fmt.Println(err)
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    encoder := json.NewEncoder(w)
    encoder.Encode(user)
}

Is there a nicer way of getting that kind of information back? Or is the way I'm doing it right now the only way currently?

0 Answers

No answers yet