func main() { if _, err := os.Stat("mate.conf"); err != nil { if os.IsNotExist(err) { fmt.Println("Please copy mate.conf.example to mate.conf, and configure this file.") return } fmt.Println(err) return } logrus_mate.Logger().Infoln("=== Using internal defualt logurs mate ===") logrus_mate.Logger().Debugln("Hello Default Logrus Mate") loggerConf := logrus_mate.LoggerConfig{ Level: "info", Formatter: logrus_mate.FormatterConfig{ Name: "json", }, } if _, err := logrus_mate.NewLogger("jack", loggerConf); err != nil { logrus_mate.Logger().Error(err) return } logrus_mate.Logger().Warnln("*** Add Logger named jack, and it will use json format") logrus_mate.Logger("jack").Debugln("not print") logrus_mate.Logger("jack").Infoln("Hello, I am A Logger from jack") fmt.Println("") os.Setenv("RUN_MODE", "production") logrus_mate.Logger().Infoln("=== Load logrus mate config from mate.conf ===") if mateConf, err := logrus_mate.LoadLogrusMateConfig("mate.conf"); err != nil { logrus_mate.Logger().Error(err) return } else { logrus_mate.Logger().Debugf("Run mode is %s", mateConf.RunEnv()) if newMate, err := logrus_mate.NewLogrusMate(mateConf); err != nil { logrus_mate.Logger().Error(err) return } else { newMate.Logger("mike").Errorln("I am mike in new logrus mate") ErrTest := errors.TN("GOGAP", 1000, "hello {{.param}}") ErrTest2 := errors.TN("GOGAP", 1002, "hello") e := ErrTest.New(errors.Params{"param": "world"}).Append("append error").Append(ErrTest2).WithContext("key", "Value") newMate.Logger("mike").WithError(e).Error(e) // This sleep is for output of redisio to write data to redis time.Sleep(time.Second) } } }
package main import ( "fmt" "github.com/gogap/errors" ) var ( ERR_PARSE_TEST = errors.T(10001, "test error") ERR_PARSE_TEST2 = errors.T(10002, "test {{.param1}} error") ERR_STACK_TEST = errors.T(10003, "call stack test") ERR_NAMESPACE_TEST = errors.TN("GOOD", 10001, "haha error") ) func main() { if e := errors.LoadMessageTemplate("./test.txt"); e != nil { fmt.Println(e) return } err1 := ERR_PARSE_TEST.New() equal1 := ERR_PARSE_TEST.IsEqual(err1) fmt.Println(err1) fmt.Println(err1, "Equal", ERR_PARSE_TEST, "?:", equal1) fmt.Println("==FullError=======================") fmt.Println(err1.FullError()) err2 := ERR_PARSE_TEST2.New(errors.Params{"param1": "example"})
package http_json_api import ( "encoding/json" "github.com/gogap/errors" "github.com/gogap/spirit" "github.com/rs/xid" "strings" ) var ( ErrHttpResponseGenericError = errors.TN(HttpJsonApiErrNamespace, 100, "") ) type JsonPayload struct { Id string `json:"id"` Data interface{} `json:"data"` Errors []*spirit.Error `json:"errors"` Context spirit.Map `json:"context"` } type HttpJsonApiPayload struct { id string data interface{} errs []*spirit.Error context spirit.Map } func NewHttpJsonApiPayload() (payload *HttpJsonApiPayload) { payload = &HttpJsonApiPayload{ id: xid.New().String(),
package ali_mns import ( "github.com/gogap/errors" ) const ( ALI_MNS_ERR_NS = "MNS" ali_MNS_ERR_TEMPSTR = "ali_mns response status error,code: {{.resp.Code}}, message: {{.resp.Message}}, resource: {{.resource}} request id: {{.resp.RequestId}}, host id: {{.resp.HostId}}" ) var ( ERR_SIGN_MESSAGE_FAILED = errors.TN(ALI_MNS_ERR_NS, 1, "sign message failed, {{.err}}") ERR_MARSHAL_MESSAGE_FAILED = errors.TN(ALI_MNS_ERR_NS, 2, "marshal message filed, {{.err}}") ERR_GENERAL_AUTH_HEADER_FAILED = errors.TN(ALI_MNS_ERR_NS, 3, "general auth header failed, {{.err}}") ERR_CREATE_NEW_REQUEST_FAILED = errors.TN(ALI_MNS_ERR_NS, 4, "create new request failed, {{.err}}") ERR_SEND_REQUEST_FAILED = errors.TN(ALI_MNS_ERR_NS, 5, "send request failed, {{.err}}") ERR_READ_RESPONSE_BODY_FAILED = errors.TN(ALI_MNS_ERR_NS, 6, "read response body failed, {{.err}}") ERR_UNMARSHAL_ERROR_RESPONSE_FAILED = errors.TN(ALI_MNS_ERR_NS, 7, "unmarshal error response failed, {{.err}}") ERR_UNMARSHAL_RESPONSE_FAILED = errors.TN(ALI_MNS_ERR_NS, 8, "unmarshal response failed, {{.err}}") ERR_DECODE_BODY_FAILED = errors.TN(ALI_MNS_ERR_NS, 9, "decode body failed, {{.err}}, body: \"{{.body}}\"") ERR_GET_BODY_DECODE_ELEMENT_ERROR = errors.TN(ALI_MNS_ERR_NS, 10, "get body decode element error, local: {{.local}}, error: {{.err}}") ERR_MNS_ACCESS_DENIED = errors.TN(ALI_MNS_ERR_NS, 100, ali_MNS_ERR_TEMPSTR) ERR_MNS_INVALID_ACCESS_KEY_ID = errors.TN(ALI_MNS_ERR_NS, 101, ali_MNS_ERR_TEMPSTR) ERR_MNS_INTERNAL_ERROR = errors.TN(ALI_MNS_ERR_NS, 102, ali_MNS_ERR_TEMPSTR) ERR_MNS_INVALID_AUTHORIZATION_HEADER = errors.TN(ALI_MNS_ERR_NS, 103, ali_MNS_ERR_TEMPSTR) ERR_MNS_INVALID_DATE_HEADER = errors.TN(ALI_MNS_ERR_NS, 104, ali_MNS_ERR_TEMPSTR)
package aop import ( "github.com/gogap/errors" ) const ( AOPErrorNamespace = "AOP" ) var ( ErrBeanInstanceIsNil = errors.TN(AOPErrorNamespace, 1, "aop error namespace is nil, id: {{.id}}") ErrBeanIsNotAnPtr = errors.TN(AOPErrorNamespace, 2, "bean should be an ptr, id: {{.id}}") ErrBeanAlreadyRegistered = errors.TN(AOPErrorNamespace, 3, "bean already regitered, id: {{.id}}") ErrBeanIDShouldNotBeEmpty = errors.TN(AOPErrorNamespace, 4, "bean id should not be empty") ErrBeanNotExist = errors.TN(AOPErrorNamespace, 5, "bean not exist, id: {{.id}}") ErrAspectIDShouldNotBeEmpty = errors.TN(AOPErrorNamespace, 6, "aspect id should not be empty") ErrBeanMethodNotExit = errors.TN(AOPErrorNamespace, 7, "bean method not exist, id: {{.id}}, class: {{.class}}, method: {{.method}}") ErrWrongAdviceFuncArgsNum = errors.TN(AOPErrorNamespace, 8, "wrong advice func args number, id: {{.id}}, class: {{.class}}, method: {{.method}}") ErrEndInvokeParamsIsNotFunc = errors.TN(AOPErrorNamespace, 9, "en invoke params is not func, bean id: {{.id}}, method: {{.method}}") ErrWrongEndInvokeFuncArgsNum = errors.TN(AOPErrorNamespace, 10, "wrong end invoke func args number, bean id: {{.id}}, method: {{.method}}") ErrInvokeParamsIsNotFunc = errors.TN(AOPErrorNamespace, 11, "invoke params is not func, bean id: {{.id}}, method: {{.method}}") ErrWrongInvokeFuncArgsNum = errors.TN(AOPErrorNamespace, 12, "wrong invoke func args number, bean id: {{.id}}, method: {{.method}}") ErrInvokeFuncNotExist = errors.TN(AOPErrorNamespace, 13, "invoke func not exist, bean id: {{.id}}, method: {{.method}}") ErrInvokeFuncTypeError = errors.TN(AOPErrorNamespace, 14, "invoke func is not func type, bean id: {{.id}}, method: {{.method}}") ErrEndInvokeTwice = errors.TN(AOPErrorNamespace, 15, "end invoke twice, bean id: {{.id}}, method: {{.method}}") ErrBadInvokeMethodType = errors.TN(AOPErrorNamespace, 16, "invoke method params should be func name or func type") ErrPointcutNotExist = errors.TN(AOPErrorNamespace, 17, "pointcut no exist, pointcut id: {{.id}}") ErrMethodIsNil = errors.TN(AOPErrorNamespace, 18, "method is nil") ErrBadMethodType = errors.TN(AOPErrorNamespace, 19, "method type error") ErrBadPointcutExpr = errors.TN(AOPErrorNamespace, 20, "bad pointcut expression, expression: {{.expr}}")
package spirit import ( "github.com/gogap/errors" ) const SPIRIT_ERR_NS = "SPIRIT" var ( ERR_MESSAGE_GRAPH_ADDRESS_NOT_FOUND = errors.TN(SPIRIT_ERR_NS, 1, "could not found next address in graph, current component name: {{.compName}}, port name: {{.portName}}") ERR_MESSAGE_GRAPH_IS_NIL = errors.TN(SPIRIT_ERR_NS, 2, "component graph is nil") ERR_MESSAGE_ADDRESS_IS_EMPTY = errors.TN(SPIRIT_ERR_NS, 3, "component message address is empty") ERR_MESSAGE_SERIALIZE_FAILED = errors.TN(SPIRIT_ERR_NS, 4, "component message serialize failed, err: {{.err}}") ERR_COMPONENT_HANDLER_RETURN_ERROR = errors.TN(SPIRIT_ERR_NS, 5, "component handler return error, component name: {{.name}}, port: {{.port}}, error: {{.err}}") ERR_COMPONENT_HANDLER_NOT_EXIST = errors.TN(SPIRIT_ERR_NS, 6, "component handler not exist, component name: {{.name}}, handler name: {{.handlerName}}") ERR_SENDER_CREDENTIAL_IS_NIL = errors.TN(SPIRIT_ERR_NS, 8, "credential is nil, type: {{.type}}, url: {{.url}}") ERR_SENDER_MNS_CLIENT_IS_NIL = errors.TN(SPIRIT_ERR_NS, 9, "sender mns client is nil, type: {{.type}}, url: {{.url}}") ERR_SENDER_SEND_FAILED = errors.TN(SPIRIT_ERR_NS, 10, "component message send failed, type: {{.type}}, url: {{.url}}, err:{{.err}}") ERR_SENDER_CREATE_FAILED = errors.TN(SPIRIT_ERR_NS, 11, "create sender failed, driver type: {{.type}}") ERR_SENDER_DRIVER_NOT_EXIST = errors.TN(SPIRIT_ERR_NS, 12, "message sender driver not exist, type: {{.type}}") ERR_SENDER_BAD_DRIVER = errors.TN(SPIRIT_ERR_NS, 13, "bad message sender driver of {{.type}}") ERR_RECEIVER_CREDENTIAL_IS_NIL = errors.TN(SPIRIT_ERR_NS, 14, "credential is nil, type: {{.type}}") ERR_RECEIVER_MNS_CLIENT_IS_NIL = errors.TN(SPIRIT_ERR_NS, 15, "receiver mns client is nil, type: {{.type}}, url: {{.url}}") ERR_RECEIVER_DELETE_MSG_ERROR = errors.TN(SPIRIT_ERR_NS, 16, "delete message error, type: {{.type}}, url: {{.url}}, err: {{.err}}") ERR_RECEIVER_UNMARSHAL_MSG_FAILED = errors.TN(SPIRIT_ERR_NS, 17, "receiver unmarshal message failed, type: {{.type}}, err: {{.err}}") ERR_RECEIVER_RECV_ERROR = errors.TN(SPIRIT_ERR_NS, 18, "receiver recv error, type: {{.type}}, url: {{.url}}, err: {{.err}}") ERR_RECEIVER_BAD_DRIVER = errors.TN(SPIRIT_ERR_NS, 19, "bad message receiver driver of {{.type}}")
"encoding/base64" "github.com/gogap/errors" "github.com/gogap/spirit" "strings" "github.com/gogap/spirit_tutorial/todo/controllers" ) var _ spirit.Component = new(AuthComponent) const ( taskAuthComponentURN = "urn:spirit_tutorial:component:todo:auth" ) var ( ErrAuthorizationHeaderNotExist = errors.TN(TodoErrorNamespace, 30001, "header of Authorization not exist!") ErrAuthorizationHeaderFmtError = errors.TN(TodoErrorNamespace, 30002, "header of Authorization format error.") ) type AuthComponent struct { name string *controllers.Auth } func init() { spirit.RegisterComponent(taskAuthComponentURN, NewAuthComponent) } func NewAuthComponent(name string, options spirit.Map) (component spirit.Component, err error) { component = &AuthComponent{ name: name,
package event_center import ( "github.com/gogap/errors" ) const EVENT_CENTER_ERR_NS = "EVENT_CENTER" var ( ERR_SUBSCRIBER_NOT_EXIST = errors.TN(EVENT_CENTER_ERR_NS, 1, "subscriber {{.id}} not exist") ERR_COULD_NOT_GET_ANONYMOUS_SUBSCRIBER = errors.TN(EVENT_CENTER_ERR_NS, 2, "could not get subscriber anonymous by name") ERR_INVALIDATE_INDEX_RANGE = errors.TN(EVENT_CENTER_ERR_NS, 3, "invalidate index range") ERR_SUBSCRIBER_ID_IS_EMPTY = errors.TN(EVENT_CENTER_ERR_NS, 4, "subscriber id is empty") ERR_SUBSCRIBER_HANDLER_IS_NIL = errors.TN(EVENT_CENTER_ERR_NS, 5, "subscriber handler is nil, index: {{.index}}, name: {{.id}}") ERR_SUBSCRIBER_ID_GENERATE_FAILED = errors.TN(EVENT_CENTER_ERR_NS, 6, "generate subscriber id failed, err: {{.err}}") ERR_SUBSCRIBER_IS_NIL = errors.TN(EVENT_CENTER_ERR_NS, 7, "subscriber is nil") ERR_EVENT_ALREADY_REGISTERED = errors.TN(EVENT_CENTER_ERR_NS, 8, "event already registered, name: {{.name}}") ERR_EVENT_NAME_IS_EMPTY = errors.TN(EVENT_CENTER_ERR_NS, 9, "event name is empty") ERR_EVENT_NOT_EXIST = errors.TN(EVENT_CENTER_ERR_NS, 10, "event not exist, name: {{.name}}") ERR_EVENT_CENTER_NAME_IS_EMPTY = errors.TN(EVENT_CENTER_ERR_NS, 11, "event center name is empty") )
package api_client import ( "github.com/gogap/errors" ) const ( HttpJsonApiErrNamespace = "JSON_APIClient" ) var JsonApiClientErrorNamespace = "APIClient" var ( ErrAPINameIsEmpty = errors.TN(JsonApiClientErrorNamespace, 1, "api name is empty") ErrAPIClientSendFailed = errors.TN(JsonApiClientErrorNamespace, 2, "api client send failed, api: {{.api}}, url: {{.url}}") ErrAPIClientResponseUnmarshalFailed = errors.TN(JsonApiClientErrorNamespace, 3, "api response unmarshal failed, api: {{.api}}, url: {{.url}}") ErrAPIClientReadResponseBodyFailed = errors.TN(JsonApiClientErrorNamespace, 4, "read api response body failed, api is: {{.api}}") ErrAPIClientBadStatusCode = errors.TN(JsonApiClientErrorNamespace, 5, "bad response status code, api is: {{.api}}, code is: {{.code}}") ErrAPIClientCreateNewRequestFailed = errors.TN(JsonApiClientErrorNamespace, 6, "create new request failed") ErrUnknownPayloadError = errors.TN(JsonApiClientErrorNamespace, 7, "") )
package http_json_api import ( "github.com/gogap/errors" ) const ( HttpJsonApiErrNamespace = "JSON_API" ) var ( ErrApiNameIsEmpty = errors.New("api name is empty") ) var ( ErrTmplVarAlreadyExist = errors.TN(HttpJsonApiErrNamespace, 400, "template var already exist, key: {{.key}}, value: {{.value}}, original value: {{.originalValue}}") ErrApiAlreadyRelatedTmpl = errors.TN(HttpJsonApiErrNamespace, 401, "api already related template, api: {{.apiName}}, template: {{.tmplName}}") ErrTmplNotExit = errors.TN(HttpJsonApiErrNamespace, 402, "template of {{.tmplName}} not exist") ErrRequestTimeout = errors.TN(HttpJsonApiErrNamespace, 408, "request timeout") ErrApiGenericError = errors.TN(HttpJsonApiErrNamespace, 500, "") ErrNotSupportMultiCallForward = errors.TN(HttpJsonApiErrNamespace, 501, "not support multi call forward") ErrRenderApiDataFailed = errors.TN(HttpJsonApiErrNamespace, 502, "render api data failed") )
package access_limiter import ( "github.com/gogap/errors" ) var ACCESS_LIMITER_NS = "ACCESS_LIMITER" var ( ERR_QPS_REACHED_UPPER_LIMIT = errors.TN(ACCESS_LIMITER_NS, 1, "qps reached upper limit, counter: {{.counter}}, {{.dimensions}}") ERR_QUOTA_REACHED_UPPER_LIMIT = errors.TN(ACCESS_LIMITER_NS, 2, "quota reached upper limit, counter: {{.counter}}, {{.dimensions}}") ERR_INCREASE_COUNT_FAILED = errors.TN(ACCESS_LIMITER_NS, 3, "increase counter failed, counter: {{.counter}}, err: {{.err}}") ERR_INCREASE_QPS_COUNT_FAILED = errors.TN(ACCESS_LIMITER_NS, 4, "increase qps count failed, counter: {{.counter}}, err: {{.err}}") ERR_RESET_QPS_COUNT_FAILED = errors.TN(ACCESS_LIMITER_NS, 5, "reset next qps count failed, counter: {{.counter}}, err: {{.err}}") ERR_RESET_COUNT_FAILED = errors.TN(ACCESS_LIMITER_NS, 6, "reset counter failed, counter: {{.counter}}, err: {{.err}}") ERR_GET_OPTIONS_FAILED = errors.TN(ACCESS_LIMITER_NS, 7, "get options failed, counter: {{.counter}}") ERR_UPDATE_OPTIONS_FAILED = errors.TN(ACCESS_LIMITER_NS, 8, "update options failed, counter: {{.counter}}, err: {{.err}}") )
package controllers import ( "github.com/gogap/errors" ) var ( ErrUserNotExist = errors.TN(TodoErrorNamespace, 40000, "user not exist: {{.username}}") ErrWrongPassword = errors.TN(TodoErrorNamespace, 40001, "wrong password !") ) type Auth struct { users map[string]string } func NewAuth() *Auth { users := make(map[string]string) users["zeal"] = "123456" users["gogap"] = "123456" return &Auth{ users: users, } } func (p *Auth) CheckAuth(username, password string) (err error) { if pwd, exist := p.users[username]; !exist { err = ErrUserNotExist.New(errors.Params{"username": username}) return } else if pwd != password { err = ErrWrongPassword.New()
import ( "time" "github.com/gogap/errors" "github.com/rs/xid" "github.com/gogap/spirit_tutorial/todo/models" ) type TaskManager struct { tasks map[string]*models.Task } var ( ErrTaskNotExist = errors.TN(TodoErrorNamespace, 50404, "task not exist, id: {{.id}}") ) func NewTaskManager() *TaskManager { return &TaskManager{ tasks: make(map[string]*models.Task), } } func (p *TaskManager) NewTask(task models.Task) (id string, err error) { task.Id = xid.New().String() task.CreateTime = time.Now() task.UpdateTime = time.Now() p.tasks[task.Id] = &task