Skip to content

Commit

Permalink
refactor: Better describe object errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aseure committed May 7, 2018
1 parent 355486b commit cb025d7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions algoliasearch/types_object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package algoliasearch

import "fmt"
import (
"encoding/json"
"fmt"
)

type CreateObjectRes struct {
CreatedAt string `json:"createdAt"`
Expand All @@ -23,13 +26,21 @@ type Object Map
func (o Object) ObjectID() (objectID string, err error) {
i, ok := o["objectID"]
if !ok {
err = fmt.Errorf("Cannot extract `objectID` field from Object")
err = fmt.Errorf("Cannot extract `objectID` field from Object `%s`", o)
return
}

if objectID, ok = i.(string); !ok {
err = fmt.Errorf("Cannot cast `objectID` field to string type")
err = fmt.Errorf("Cannot cast `objectID` field to string type from Object `%s`", o)
}

return
}

func (o Object) String() string {
jsonContent, err := json.Marshal(&o)
if err == nil {
return string(jsonContent)
}
return fmt.Sprintf("%#v", o)
}

0 comments on commit cb025d7

Please sign in to comment.