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

Golang errors.Newf函数代码示例

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

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



在下文中一共展示了Newf函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: JsonRequest

// JsonRequest JSON encodes and sends the object in reqData.ReqValue (if any) to the specified URL.
// Optional method arguments are passed using the RequestData object.
// Relevant RequestData fields:
// ReqHeaders: additional HTTP header values to add to the request.
// ExpectedStatus: the allowed HTTP response status values, else an error is returned.
// ReqValue: the data object to send.
// RespValue: the data object to decode the result into.
func (c *Client) JsonRequest(method, url, rfc1123Date string, request *RequestData, response *ResponseData) (err error) {
	err = nil
	var body []byte
	if request.Params != nil {
		url += "?" + request.Params.Encode()
	}
	if request.ReqValue != nil {
		body, err = json.Marshal(request.ReqValue)
		if err != nil {
			err = errors.Newf(err, "failed marshalling the request body")
			return
		}
	}
	headers, err := createHeaders(request.ReqHeaders, c.credentials, contentTypeJSON, rfc1123Date, c.apiVersion,
		isMantaRequest(url, c.credentials.UserAuthentication.User))
	if err != nil {
		return err
	}
	respBody, respHeader, err := c.sendRequest(
		method, url, bytes.NewReader(body), len(body), headers, response.ExpectedStatus, c.logger)
	if err != nil {
		return
	}
	defer respBody.Close()
	respData, err := ioutil.ReadAll(respBody)
	if err != nil {
		err = errors.Newf(err, "failed reading the response body")
		return
	}

	if len(respData) > 0 {
		if response.RespValue != nil {
			if _, ok := response.RespValue.(*[]byte); ok {
				response.RespValue = respData
				//err = decodeJSON(bytes.NewReader(respData), false, response.RespValue)
				//if err != nil {
				//	err = errors.Newf(err, "failed unmarshaling/decoding the response body: %s", respData)
				//}
			} else {
				err = json.Unmarshal(respData, response.RespValue)
				if err != nil {
					err = decodeJSON(bytes.NewReader(respData), true, response.RespValue)
					if err != nil {
						err = errors.Newf(err, "failed unmarshaling/decoding the response body: %s", respData)
					}
				}
			}
		}
	}

	if respHeader != nil {
		response.RespHeaders = respHeader
	}

	return
}
开发者ID:asteris-llc,项目名称:gocommon,代码行数:63,代码来源:client.go


示例2: TestErrorCause

func (s *ErrorsSuite) TestErrorCause(c *gc.C) {
	rootCause := errors.NewResourceNotFoundf(nil, "some value", "")
	// Construct a new error, based on a resource not found root cause.
	err := errors.Newf(rootCause, "an error occurred")
	c.Assert(err.Cause(), gc.Equals, rootCause)
	// Check the other error attributes.
	c.Assert(err.Error(), gc.Equals, "an error occurred\ncaused by: Resource Not Found: some value")
}
开发者ID:asteris-llc,项目名称:gocommon,代码行数:8,代码来源:errors_test.go


示例3: TestErrorIsType

func (s *ErrorsSuite) TestErrorIsType(c *gc.C) {
	rootCause := errors.NewBadRequestf(nil, "some value", "")
	// Construct a new error, based on a bad request root cause.
	err := errors.Newf(rootCause, "an error occurred")
	// Check that the error is not falsely identified as something it is not.
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, false)
	// Check that the error is correctly identified as a not found error.
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
}
开发者ID:asteris-llc,项目名称:gocommon,代码行数:9,代码来源:errors_test.go


示例4: sendRateLimitedRequest

func (c *Client) sendRateLimitedRequest(method, URL string, headers http.Header, reqData []byte,
	logger *loggo.Logger) (resp *http.Response, err error) {
	for i := 0; i < c.maxSendAttempts; i++ {
		var reqReader io.Reader
		if reqData != nil {
			reqReader = bytes.NewReader(reqData)
		}
		req, err := http.NewRequest(method, URL, reqReader)
		if err != nil {
			err = errors.Newf(err, "failed creating the request %s", URL)
			return nil, err
		}
		// Setting req.Close to true to avoid malformed HTTP version "nullHTTP/1.1" error
		// See http://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
		req.Close = true
		for header, values := range headers {
			for _, value := range values {
				req.Header.Add(header, value)
			}
		}
		req.ContentLength = int64(len(reqData))
		resp, err = c.Do(req)
		if err != nil {
			return nil, errors.Newf(err, "failed executing the request %s", URL)
		}
		if resp.StatusCode != http.StatusRequestEntityTooLarge || resp.Header.Get("Retry-After") == "" {
			return resp, nil
		}
		resp.Body.Close()
		retryAfter, err := strconv.ParseFloat(resp.Header.Get("Retry-After"), 64)
		if err != nil {
			return nil, errors.Newf(err, "Invalid Retry-After header %s", URL)
		}
		if retryAfter == 0 {
			return nil, errors.Newf(err, "Resource limit exeeded at URL %s", URL)
		}
		if logger != nil {
			logger.Warningf("Too many requests, retrying in %dms.", int(retryAfter*1000))
		}
		time.Sleep(time.Duration(retryAfter) * time.Second)
	}
	return nil, errors.Newf(err, "Maximum number of attempts (%d) reached sending request to %s", c.maxSendAttempts, URL)
}
开发者ID:asteris-llc,项目名称:gocommon,代码行数:43,代码来源:client.go


示例5: CancelJob

// This cancels a job from doing any further work.
// Cancellation is asynchronous and "best effort"; there is no guarantee the job will actually stop
// See API docs: http://apidocs.joyent.com/manta/api.html#CancelJob
func (c *Client) CancelJob(jobId string) error {
	req := request{
		method:         client.POST,
		url:            makeURL(apiJobs, jobId, apiJobsLive, apiJobsCancel),
		expectedStatus: http.StatusAccepted,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to cancel job %s", jobId)
	}
	return nil
}
开发者ID:partamonov,项目名称:terraform,代码行数:14,代码来源:manta.go


示例6: EndJobInputs

// This closes input for a job, and finalize the job.
// See API docs: http://apidocs.joyent.com/manta/api.html#EndJobInput
func (c *Client) EndJobInputs(jobId string) error {
	req := request{
		method:         client.POST,
		url:            makeURL(apiJobs, jobId, apiJobsLive, apiJobsIn, apiJobsEnd),
		expectedStatus: http.StatusAccepted,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to end inputs for job %s", jobId)
	}
	return nil
}
开发者ID:partamonov,项目名称:terraform,代码行数:13,代码来源:manta.go


示例7: AddJobInputs

// Submits inputs to an already created job.
// See API docs: http://apidocs.joyent.com/manta/api.html#AddJobInputs
func (c *Client) AddJobInputs(jobId string, jobInputs io.Reader) error {
	inputData, errI := ioutil.ReadAll(jobInputs)
	if errI != nil {
		return errors.Newf(errI, "failed to read inputs for job %s", jobId)
	}
	requestHeaders := make(http.Header)
	requestHeaders.Set("Accept", "*/*")
	requestHeaders.Set("Content-Type", "text/plain")
	req := request{
		method:         client.POST,
		url:            makeURL(apiJobs, jobId, apiJobsLive, apiJobsIn),
		reqValue:       string(inputData),
		reqHeader:      requestHeaders,
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to add inputs to job %s", jobId)
	}
	return nil
}
开发者ID:partamonov,项目名称:terraform,代码行数:22,代码来源:manta.go


示例8: DeleteMachineTag

// DeleteMachineTag deletes a single tag from the specified machine.
// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineTag
func (c *Client) DeleteMachineTag(machineID, tagKey string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiMachines, machineID, apiTags, tagKey),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete tag with key %s for machine with id %s", tagKey, machineID)
	}
	return nil
}
开发者ID:Zordrak,项目名称:terraform,代码行数:13,代码来源:machine_tags.go


示例9: DeleteImage

// DeleteImage (Beta) Delete the image specified by imageId. Must be image owner to do so.
// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteImage
func (c *Client) DeleteImage(imageID string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiImages, imageID),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete image with id: %s", imageID)
	}
	return nil
}
开发者ID:cehoffman,项目名称:gosdc,代码行数:13,代码来源:images.go


示例10: DeleteKey

// DeleteKey deletes the key identified by keyName.
// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteKey
func (c *Client) DeleteKey(keyName string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiKeys, keyName),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete key with name: %s", keyName)
	}
	return nil
}
开发者ID:RezaDKhan,项目名称:terraform,代码行数:13,代码来源:keys.go


示例11: DeleteFirewallRule

// DeleteFirewallRule removes the given firewall rule record from all the required account machines.
// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteFirewallRule
func (c *Client) DeleteFirewallRule(fwRuleID string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiFirewallRules, fwRuleID),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete firewall rule with id %s", fwRuleID)
	}
	return nil
}
开发者ID:RezaDKhan,项目名称:terraform,代码行数:13,代码来源:firewalls.go


示例12: DeleteMachineSnapshot

// DeleteMachineSnapshot deletes the specified snapshot.
// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineSnapshot
func (c *Client) DeleteMachineSnapshot(machineID, snapshotName string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiMachines, machineID, apiSnapshots, snapshotName),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete snapshot %s for machine with id %s", snapshotName, machineID)
	}
	return nil
}
开发者ID:Zordrak,项目名称:terraform,代码行数:13,代码来源:machine_snapshots.go


示例13: StartMachineFromSnapshot

// StartMachineFromSnapshot starts the machine from the specified snapshot.
// Machine must be in 'stopped' state.
// See API docs: http://apidocs.joyent.com/cloudapi/#StartMachineFromSnapshot
func (c *Client) StartMachineFromSnapshot(machineID, snapshotName string) error {
	req := request{
		method:         client.POST,
		url:            makeURL(apiMachines, machineID, apiSnapshots, snapshotName),
		expectedStatus: http.StatusAccepted,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to start machine with id %s from snapshot %s", machineID, snapshotName)
	}
	return nil
}
开发者ID:Zordrak,项目名称:terraform,代码行数:14,代码来源:machine_snapshots.go


示例14: StartMachine

// Starts a stopped machine.
// See API docs: http://apidocs.joyent.com/cloudapi/#StartMachine
func (c *Client) StartMachine(machineId string) error {
	req := request{
		method:         client.POST,
		url:            fmt.Sprintf("%s/%s?action=%s", apiMachines, machineId, actionStart),
		expectedStatus: http.StatusAccepted,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to start machine with id: %s", machineId)
	}
	return nil
}
开发者ID:BrianHicks,项目名称:gosdc,代码行数:13,代码来源:cloudapi.go


示例15: DeleteInstrumentation

// Destroys an instrumentation.
// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteInstrumentation
func (c *Client) DeleteInstrumentation(instrumentationId string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiAnalytics, apiInstrumentations, instrumentationId),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete instrumentation with id %s", instrumentationId)
	}
	return nil
}
开发者ID:BrianHicks,项目名称:gosdc,代码行数:13,代码来源:cloudapi.go


示例16: DeleteFabricNetwork

// DeleteFabricNetwork deletes an existing fabric network
// See API docs: https://apidocs.joyent.com/cloudapi/#DeleteFabricNetwork
func (c *Client) DeleteFabricNetwork(vlanID int16, networkID string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID)), apiFabricNetworks, networkID),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete fabric network %s on vlan %d", networkID, vlanID)
	}
	return nil
}
开发者ID:RezaDKhan,项目名称:terraform,代码行数:13,代码来源:fabrics.go


示例17: GetObject

// Retrieves the specified object from the specified location.
// See API docs: http://apidocs.joyent.com/manta/api.html#GetObject
func (c *Client) GetObject(path, objectName string) ([]byte, error) {
	var resp []byte
	requestHeaders := make(http.Header)
	requestHeaders.Set("Accept", "*/*")
	req := request{
		method:    client.GET,
		url:       makeURL(apiStorage, path, objectName),
		reqHeader: requestHeaders,
		resp:      &resp,
	}
	respData, err := c.sendRequest(req)
	if err != nil {
		return nil, errors.Newf(err, "failed to get object %s/%s", path, objectName)
	}
	res, ok := respData.RespValue.(*[]byte)
	if !ok {
		return nil, errors.Newf(err, "failed to assert downloaded data as type *[]byte for object %s/%s", path, objectName)
	}
	return *res, nil
}
开发者ID:partamonov,项目名称:terraform,代码行数:22,代码来源:manta.go


示例18: RenameMachine

// RenameMachine renames an existing machine.
// See API docs: http://apidocs.joyent.com/cloudapi/#RenameMachine
func (c *Client) RenameMachine(machineID, machineName string) error {
	req := request{
		method:         client.POST,
		url:            fmt.Sprintf("%s/%s?action=%s&name=%s", apiMachines, machineID, actionRename, machineName),
		expectedStatus: http.StatusAccepted,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to rename machine with id: %s", machineID)
	}
	return nil
}
开发者ID:freshvolk,项目名称:terraform,代码行数:13,代码来源:machines.go


示例19: DeleteObject

// Deletes the specified object from the specified location.
// See API docs: http://apidocs.joyent.com/manta/api.html#DeleteObject
func (c *Client) DeleteObject(path, objectName string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiStorage, path, objectName),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete object %s/%s", path, objectName)
	}
	return nil
}
开发者ID:partamonov,项目名称:terraform,代码行数:13,代码来源:manta.go


示例20: DeleteFabricVLAN

// DeleteFabricVLAN delets a given VLAN as specified by ID
// See API docs: https://apidocs.joyent.com/cloudapi/#DeleteFabricVLAN
func (c *Client) DeleteFabricVLAN(vlanID int16) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID))),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete fabric VLAN with id %d", vlanID)
	}
	return nil
}
开发者ID:RezaDKhan,项目名称:terraform,代码行数:13,代码来源:fabrics.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang cloudapi.Client类代码示例发布时间:2022-05-23
下一篇:
Golang client.NewClient函数代码示例发布时间: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