當前位置: 首頁>>代碼示例>>Golang>>正文


Golang api2go.NewHTTPError函數代碼示例

本文整理匯總了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
}
開發者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


注:本文中的github.com/manyminds/api2go.NewHTTPError函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。