本文整理汇总了Golang中github.com/manyminds/api2go.NewHTTPError函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHTTPError函数的具体用法?Golang NewHTTPError怎么用?Golang NewHTTPError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewHTTPError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Create
// Create method to satisfy `api2go.DataSource` interface
func (s *userResource) Create(obj interface{}, r api2go.Request) (string, error) {
user, ok := obj.(User)
if !ok {
return "", api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
if _, ok := s.users[user.GetID()]; ok {
return "", api2go.NewHTTPError(errors.New("User exists"), "User exists", http.StatusConflict)
}
s.idCount++
id := fmt.Sprintf("%d", s.idCount)
user.SetID(id)
// check references and get embedded objects
for _, chocID := range user.ChocolatesIDs {
choc, err := s.chocStorage.GetOne(chocID)
if err != nil {
return "", err
}
user.Chocolates = append(user.Chocolates, choc)
}
s.users[id] = user
return id, nil
}
示例2: Create
// Create a new choc
func (c ChocolateResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
choc, ok := obj.(model.Chocolate)
if !ok {
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
id := c.ChocStorage.Insert(choc)
err := choc.SetID(id)
if err != nil {
return &Response{}, api2go.NewHTTPError(errors.New("Non-integer ID given"), "Non-integer ID given", http.StatusInternalServerError)
}
return &Response{Res: choc, Code: http.StatusCreated}, nil
}
示例3: Create
//Create satisfies api2go create interface
func (s UserSource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
user, ok := obj.(User)
if !ok {
return &common.Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
err := s.connection.Collection("user").Save(&user)
if err != nil {
return &common.Response{}, api2go.NewHTTPError(err, err.Error(), http.StatusBadRequest)
}
return &common.Response{Res: user, Code: http.StatusCreated}, nil
}
示例4: FindOne
// FindOne to satisfy `api2go.DataSource` interface
// this method should return the user with the given ID, otherwise an error
func (s *userResource) FindOne(ID string, r api2go.Request) (interface{}, error) {
if user, ok := s.users[ID]; ok {
return user, nil
}
return nil, api2go.NewHTTPError(errors.New("Not Found"), "Not Found", http.StatusNotFound)
}
示例5: Update
//Update stores all changes on the user
func (s UserResource) Update(obj interface{}, r api2go.Request) error {
user, ok := obj.(model.User)
if !ok {
return api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
return s.UserStorage.Update(user)
}
示例6: Update
// Update a choc
func (c ChocolateResource) Update(obj interface{}, r api2go.Request) error {
choc, ok := obj.(model.Chocolate)
if !ok {
return api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
return c.ChocStorage.Update(choc)
}
示例7: Create
// Create a new choc
func (c ChocolateResource) Create(obj interface{}, r api2go.Request) (string, error) {
choc, ok := obj.(model.Chocolate)
if !ok {
return "", api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
return c.ChocStorage.Insert(choc), nil
}
示例8: GetOne
// GetOne user
func (s UserStorage) GetOne(id string) (model.User, error) {
intID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
errMessage := fmt.Sprintf("User id must be integer: %s", id)
return model.User{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusBadRequest)
}
return s.getOneWithAssociations(intID)
}
示例9: Create
// Create method to satisfy `api2go.DataSource` interface
func (s UserResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
user, ok := obj.(model.User)
if !ok {
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
id, err := s.UserStorage.Insert(user)
if err != nil {
return &Response{}, api2go.NewHTTPError(errors.New("Faild to create a user"), "Faild to create a user", http.StatusInternalServerError)
}
err = user.SetID(id)
if err != nil {
return &Response{}, api2go.NewHTTPError(errors.New("Non-integer ID given"), "Non-integer ID given", http.StatusInternalServerError)
}
return &Response{Res: user, Code: http.StatusCreated}, nil
}
示例10: GetOne
// GetOne user
func (s UserStorage) GetOne(id string) (model.User, error) {
user, ok := s.users[id]
if ok {
return *user, nil
}
errMessage := fmt.Sprintf("User for id %s not found", id)
return model.User{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusNotFound)
}
示例11: Update
//Update stores all changes on the user
func (s UserResource) Update(obj interface{}, r api2go.Request) (api2go.Responder, error) {
user, ok := obj.(model.User)
if !ok {
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
err := s.UserStorage.Update(user)
return &Response{Res: user, Code: http.StatusNoContent}, err
}
示例12: Update
// Update a choc
func (c ChocolateResource) Update(obj interface{}, r api2go.Request) (api2go.Responder, error) {
choc, ok := obj.(model.Chocolate)
if !ok {
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
err := c.ChocStorage.Update(choc)
return &Response{Res: choc, Code: http.StatusNoContent}, err
}
示例13: Create
// Create method to satisfy `api2go.DataSource` interface
func (s UserResource) Create(obj interface{}, r api2go.Request) (string, error) {
user, ok := obj.(model.User)
if !ok {
return "", api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
id := s.UserStorage.Insert(user)
return id, nil
}
示例14: Create
// Create a new choc
func (c ChocolateResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
choc, ok := obj.(model.Chocolate)
if !ok {
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
id := c.ChocStorage.Insert(choc)
choc.ID = id
return &Response{Res: choc, Code: http.StatusCreated}, nil
}
示例15: Create
// Create method to satisfy `api2go.DataSource` interface
func (s UserResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
user, ok := obj.(model.User)
if !ok {
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
}
id := s.UserStorage.Insert(user)
user.ID = id
return &Response{Res: user, Code: http.StatusCreated}, nil
}