本文整理汇总了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;未经允许,请勿转载。 |
请发表评论