当前位置: 首页>>代码示例>>Golang>>正文


Golang Error.HttpCode方法代码示例

本文整理汇总了Golang中github.com/hailocab/platform-layer/errors.Error.HttpCode方法的典型用法代码示例。如果您正苦于以下问题:Golang Error.HttpCode方法的具体用法?Golang Error.HttpCode怎么用?Golang Error.HttpCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/hailocab/platform-layer/errors.Error的用法示例。


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

示例1: commonLoggerMiddleware

// commonLogHandler will log to w using the Apache common log format
// http://httpd.apache.org/docs/2.2/logs.html#common
// If w is nil, nothing will be logged
func commonLoggerMiddleware(w io.Writer) Middleware {
	return func(ep *Endpoint, h Handler) Handler {
		// If no writer is passed to middleware just return the handler
		if w == nil {
			return h
		}

		return func(req *Request) (proto.Message, errors.Error) {
			var userId string
			if req.Auth() != nil && req.Auth().AuthUser() != nil {
				userId = req.Auth().AuthUser().Id
			}

			var err errors.Error
			var m proto.Message

			// In defer in case the handler panics
			defer func() {
				status := uint32(200)
				if err != nil {
					status = err.HttpCode()
				}

				size := 0
				if m != nil {
					log.Debug(m.String())
					size = len(m.String())
				}

				fmt.Fprintf(w, "%s - %s [%s] \"%s %s %s\" %d %d\n",
					req.From(),
					userId,
					time.Now().Format("02/Jan/2006:15:04:05 -0700"),
					"GET", // Treat them all as GET's at the moment
					req.Endpoint(),
					"HTTP/1.0", // Has to be HTTP or apachetop ignores it
					status,
					size,
				)
			}()

			// Execute the actual handler
			m, err = h(req)
			return m, err
		}
	}
}
开发者ID:smnalex,项目名称:platform-layer,代码行数:50,代码来源:middleware.go

示例2: publishError

// publishError publishes an event when a handler returns an error
func publishError(req *Request, e errors.Error) {
	if !PublishErrors {
		return
	}

	stacktrace := ""
	if e.MultiStack() != nil {
		stacktrace = e.MultiStack().String()
	}

	application := ""
	if req.Auth().IsAuth() && req.Auth().AuthUser() != nil {
		application = req.Auth().AuthUser().Application()
	}
	userId := ""
	if req.Auth().IsAuth() && req.Auth().AuthUser() != nil {
		userId = req.Auth().AuthUser().Id
	}

	msg := map[string]interface{}{
		"created":     time.Now(),
		"service":     Name,
		"version":     Version,
		"azName":      az,
		"hostname":    hostname,
		"instanceId":  InstanceID,
		"error":       e.Error(),
		"type":        e.Type(),
		"code":        e.Code(),
		"description": e.Description(),
		"httpCode":    e.HttpCode(),
		"context":     e.Context(),
		"userId":      userId,
		"application": application,
		"traceId":     req.TraceID(),
		"stacktrace":  stacktrace,
	}

	payload, err := json.Marshal(msg)
	if err != nil {
		log.Errorf("[Server] Failed to JSON encode error event: %v", err)
	}
	if err = nsq.Publish(errorTopic, payload); err != nil {
		log.Errorf("[Server] Failed to publish error event: %v", err)
	}
}
开发者ID:choirudin2210,项目名称:platform-layer,代码行数:47,代码来源:failure.go


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