• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang api2go.NewHTTPError函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/manyminds/api2go.NewHTTPError函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHTTPError函数的具体用法?Golang NewHTTPError怎么用?Golang NewHTTPError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewHTTPError函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:jens-a-e,项目名称:api2go,代码行数:29,代码来源:crud_example.go


示例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
}
开发者ID:hnakamur,项目名称:api2go-gorm-gin-crud-example,代码行数:14,代码来源:resource_chocolate.go


示例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
}
开发者ID:gitter-badger,项目名称:soyfr,代码行数:15,代码来源:user.go


示例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)
}
开发者ID:jens-a-e,项目名称:api2go,代码行数:9,代码来源:crud_example.go


示例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)
}
开发者ID:wkrause13,项目名称:api2go,代码行数:9,代码来源:resource_user.go


示例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)
}
开发者ID:wkrause13,项目名称:api2go,代码行数:9,代码来源:resource_chocolate.go


示例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
}
开发者ID:wkrause13,项目名称:api2go,代码行数:9,代码来源:resource_chocolate.go


示例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)
}
开发者ID:hnakamur,项目名称:api2go-gorm-gin-crud-example,代码行数:9,代码来源:storage_user.go


示例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
}
开发者ID:hnakamur,项目名称:api2go-gorm-gin-crud-example,代码行数:18,代码来源:resource_user.go


示例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)
}
开发者ID:derelk,项目名称:api2go,代码行数:9,代码来源:storage_user.go


示例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
}
开发者ID:hnakamur,项目名称:api2go-gorm-gin-crud-example,代码行数:10,代码来源:resource_user.go


示例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
}
开发者ID:derelk,项目名称:api2go,代码行数:10,代码来源:resource_chocolate.go


示例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
}
开发者ID:wkrause13,项目名称:api2go,代码行数:11,代码来源:resource_user.go


示例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
}
开发者ID:derelk,项目名称:api2go,代码行数:11,代码来源:resource_chocolate.go


示例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
}
开发者ID:Gurpartap,项目名称:api2go,代码行数:12,代码来源:resource_user.go


示例16: getOneWithAssociations

func (s UserStorage) getOneWithAssociations(id int64) (model.User, error) {
	var user model.User
	s.db.First(&user, id)
	s.db.Model(&user).Related(&user.Chocolates, "Chocolates")
	if err := s.db.Error; err == gorm.ErrRecordNotFound {
		errMessage := fmt.Sprintf("User for id %s not found", id)
		return model.User{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusNotFound)
	} else if err != nil {
		return model.User{}, err
	}
	user.ChocolatesIDs = make([]string, len(user.Chocolates))
	for i, choc := range user.Chocolates {
		user.ChocolatesIDs[i] = choc.GetID()
	}
	return user, nil
}
开发者ID:hnakamur,项目名称:api2go-gorm-gin-crud-example,代码行数:16,代码来源:storage_user.go


示例17: 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) (api2go.Responder, error) {
	user, err := s.UserStorage.GetOne(ID)
	if err != nil {
		return &Response{}, api2go.NewHTTPError(err, err.Error(), http.StatusNotFound)
	}

	user.Chocolates = []model.Chocolate{}
	for _, chocolateID := range user.ChocolatesIDs {
		choc, err := s.ChocStorage.GetOne(chocolateID)
		if err != nil {
			return &Response{}, err
		}
		user.Chocolates = append(user.Chocolates, choc)
	}
	return &Response{Res: user}, nil
}
开发者ID:hnakamur,项目名称:api2go-gorm-gin-crud-example,代码行数:18,代码来源:resource_user.go


示例18: Update

//Update stores all changes on the user
func (s *userResource) Update(obj interface{}, r api2go.Request) error {
	user, ok := obj.(User)
	if !ok {
		return api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	// check references and get embedded objects, in real world, you would make database queries and check all your references
	user.Chocolates = []*Chocolate{}
	for _, chocID := range user.ChocolatesIDs {
		choc, err := s.chocStorage.GetOne(chocID)
		if err != nil {
			return err
		}

		user.Chocolates = append(user.Chocolates, choc)
	}

	s.users[user.GetID()] = user

	return nil
}
开发者ID:jens-a-e,项目名称:api2go,代码行数:22,代码来源:crud_example.go


示例19: FindAll

func (controller *Book) FindAll(request api2go.Request) (interface{}, error) {
	return nil, api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)
}
开发者ID:tanmayambre,项目名称:testapi2go,代码行数:3,代码来源:book.go


示例20: Update

func (controller *Author) Update(object interface{}, request api2go.Request) error {
	return api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)
}
开发者ID:tanmayambre,项目名称:testapi2go,代码行数:3,代码来源:author.go



注:本文中的github.com/manyminds/api2go.NewHTTPError函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang cmd.Cmd类代码示例发布时间:2022-05-23
下一篇:
Golang x.Instruction类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap