本文整理匯總了Golang中github.com/hailocab/platform-layer/errors.Error.Type方法的典型用法代碼示例。如果您正苦於以下問題:Golang Error.Type方法的具體用法?Golang Error.Type怎麽用?Golang Error.Type使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hailocab/platform-layer/errors.Error
的用法示例。
在下文中一共展示了Error.Type方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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)
}
}
示例2: HandleRequest
// HandleRequest and send back response
func HandleRequest(req *Request) {
defer func() {
if r := recover(); r != nil {
log.Criticalf("[Server] Panic \"%v\" when handling request: (id: %s, endpoint: %s, content-type: %s,"+
" content-length: %d)", r, req.MessageID(), req.Destination(), req.delivery.ContentType,
len(req.delivery.Body))
inst.Counter(1.0, "runtime.panic", 1)
publishFailure(r)
debug.PrintStack()
}
}()
if len(req.Service()) > 0 && req.Service() != Name {
log.Criticalf(`[Server] Message meant for "%s" not "%s"`, req.Service(), Name)
return
}
reqProcessor:
switch {
case req.isHeartbeat():
if dsc.IsConnected() {
log.Tracef("[Server] Inbound heartbeat from: %s", req.ReplyTo())
dsc.hb.beat()
raven.SendResponse(PongResponse(req), InstanceID)
} else {
log.Warnf("[Server] Not connected but heartbeat from: %s", req.ReplyTo())
}
case req.IsPublication():
log.Tracef("[Server] Inbound publication on topic: %s", req.Topic())
if endpoint, ok := reg.find(req.Topic()); ok { // Match + call handler
if data, err := endpoint.unmarshalRequest(req); err != nil {
log.Warnf("[Server] Failed to unmarshal published message: %s", err.Error())
break reqProcessor
} else {
req.unmarshaledData = data
}
if _, err := endpoint.Handler(req); err != nil {
// don't do anything on error apart from log - it's a pub sub call so no response required
log.Warnf("[Server] Failed to process published message: %v", err)
}
}
default:
log.Tracef("[Server] Inbound message %s from %s", req.MessageID(), req.ReplyTo())
// Match a handler
endpoint, ok := reg.find(req.Endpoint())
if !ok {
if rsp, err := ErrorResponse(req, errors.InternalServerError("com.hailocab.kernel.handler.missing", fmt.Sprintf("No handler registered for %s", req.Destination()))); err != nil {
log.Criticalf("[Server] Unable to build response: %v", err)
} else {
raven.SendResponse(rsp, InstanceID)
}
return
}
// Unmarshal the request data
var (
reqData, rspData proto.Message
err errors.Error
)
if reqData, err = endpoint.unmarshalRequest(req); err == nil {
req.unmarshaledData = reqData
}
// Call handler if no errors so far
if err == nil {
rspData, err = endpoint.Handler(req)
}
// Check response type matches what's registered
if err == nil && rspData != nil {
_, rspProtoT := endpoint.ProtoTypes()
rspDataT := reflect.TypeOf(rspData)
if rspProtoT != nil && rspProtoT != rspDataT {
err = errors.InternalServerError("com.hailocab.kernel.server.mismatchedprotocol",
fmt.Sprintf("Mismatched response protocol. %s != %s", rspDataT.String(), rspProtoT.String()))
}
}
if err != nil {
switch err.Type() {
case errors.ErrorBadRequest, errors.ErrorForbidden, errors.ErrorNotFound:
log.Debugf("[Server] Handler error %s calling %v.%v from %v: %v", err.Type(), req.Service(),
req.Endpoint(), req.From(), err)
case errors.ErrorInternalServer:
go publishError(req, err)
fallthrough
default:
log.Errorf("[Server] Handler error %s calling %v.%v from %v: %v", err.Type(), req.Service(),
req.Endpoint(), req.From(), err)
}
if rsp, err := ErrorResponse(req, err); err != nil {
log.Criticalf("[Server] Unable to build response: %v", err)
} else {
//.........這裏部分代碼省略.........