// Package errcode 错误码+通用错误码定义
package errcode

import "gitlab.galaxy-immi.com/Backend-group/go-com/third/log"

/**
  项目组代号:01
  服务代号:06
  模块代号:0~99
  错误码：0~1000
*/

// ErrCode 错误码
type ErrCode struct {
	Code     uint32 `comment:"业务错误码"`
	HTTPCode int32  `comment:"http状态码"`
	Desc     string `comment:"中文错误描述"`
	EnDesc   string `comment:"英文错误描述"`
}

// Error 实现error
func (e ErrCode) Error() string {
	return e.Desc
}

func (e ErrCode) Wrap(err error) ErrCode {
	log.Sugar().Error(err)
	return e
}

func (e ErrCode) WrapWithMsg(err error, msg string) ErrCode {
	log.Sugar().Errorf("msg: %s, original: %v", msg, err)
	return e
}

func (e ErrCode) WithDesc(desc string) ErrCode {
	e.Desc = desc
	return e
}

func (e ErrCode) WithEnDesc(desc string) ErrCode {
	e.EnDesc = desc
	return e
}

var (
	// ErrNo 没有错误
	ErrNo = ErrCode{
		Code:     0,
		HTTPCode: 200,
		Desc:     "OK",
	}
	// ErrCanceled 操作被取消
	ErrCanceled = ErrCode{
		Code:     10001,
		HTTPCode: 499,
		Desc:     "操作被取消",
	}
	// ErrUnknown 未知错误
	ErrUnknown = ErrCode{
		Code:     10002,
		HTTPCode: 500,
		Desc:     "未知错误",
	}
	// ErrInvalidArgument 客户端指定了无效参数
	ErrInvalidArgument = ErrCode{
		Code:     10003,
		HTTPCode: 400,
		Desc:     "客户端指定了无效参数",
	}
	// ErrDeadlineExceeded 操作超时
	ErrDeadlineExceeded = ErrCode{
		Code:     10004,
		HTTPCode: 504,
		Desc:     "操作超时",
	}
	// ErrNotFound 未找到请求的实体
	ErrNotFound = ErrCode{
		Code:     10005,
		HTTPCode: 404,
		Desc:     "未找到请求的实体",
	}
	// ErrAlreadyExists 创建实体失败，因为实体已经存在
	ErrAlreadyExists = ErrCode{
		Code:     10006,
		HTTPCode: 409,
		Desc:     "创建实体失败，因为实体已经存在",
	}
	// ErrPermissionDenied 资源不可用
	ErrPermissionDenied = ErrCode{
		Code:     10006,
		HTTPCode: 403,
		Desc:     "资源不可用",
	}
	// ErrUnauthenticated 错误- 未授权
	ErrUnauthenticated = ErrCode{
		Code:     10006,
		HTTPCode: 401,
		Desc:     "错误- 未授权",
	}
	// ErrResourceExhausted 某个资源已耗尽
	ErrResourceExhausted = ErrCode{
		Code:     10008,
		HTTPCode: 429,
		Desc:     "某个资源已耗尽",
	}

	// ErrFailedPrecondition 操作被拒绝，因为系统不处于所需状态
	ErrFailedPrecondition = ErrCode{
		Code:     10009,
		HTTPCode: 400,
		Desc:     "操作被拒绝，因为系统不处于所需状态",
	}
	// ErrAborted 操作被中止，通常是由于并发问题
	ErrAborted = ErrCode{
		Code:     10010,
		HTTPCode: 409,
		Desc:     "操作被中止，通常是由于并发问题",
	}

	// ErrOutOfRange 操作超出有效范围
	ErrOutOfRange = ErrCode{
		Code:     10011,
		HTTPCode: 400,
		Desc:     "操作超出有效范围",
	}
	// ErrUnimplemented 操作未实现或不支持
	ErrUnimplemented = ErrCode{
		Code:     10012,
		HTTPCode: 501,
		Desc:     "操作未实现或不支持",
	}
	// ErrInternal 内部错误
	ErrInternal = ErrCode{
		Code:     10013,
		HTTPCode: 500,
		Desc:     "内部错误",
	}
	// ErrUnavailable 服务当前不可用
	ErrUnavailable = ErrCode{
		Code:     10014,
		HTTPCode: 503,
		Desc:     "服务当前不可用",
	}
	// ErrDataLoss 不可恢复的数据丢失或损坏
	ErrDataLoss = ErrCode{
		Code:     10015,
		HTTPCode: 500,
		Desc:     "不可恢复的数据丢失或损坏",
	}
	// ErrRequestExceeded 请求调用次数超过限制
	ErrRequestExceeded = ErrCode{
		Code:     10016,
		HTTPCode: 500,
		Desc:     "请求调用次数超过限制",
	}
	// ErrRPCError RPC 调用错误
	ErrRPCError = ErrCode{
		Code:     10017,
		HTTPCode: 500,
		Desc:     "Error 后台服务系统错误，一般为 RPC 调用错误",
	}
	// ErrErrorCode 后台服务系统错误，一般为编解码错误
	ErrErrorCode = ErrCode{
		Code:     10018,
		HTTPCode: 500,
		Desc:     "Error 后台服务系统错误，一般为编解码错误",
	}
	// ErrContentTypeError Content-Type 错误
	ErrContentTypeError = ErrCode{
		Code:     10019,
		HTTPCode: 500,
		Desc:     "Content-Type 错误",
	}
	// ErrDomainInvalid 域名不合法
	ErrDomainInvalid = ErrCode{
		Code:     10020,
		HTTPCode: 500,
		Desc:     "域名不合法",
	}
	// ErrCacheError 后台缓存读取错误
	ErrCacheError = ErrCode{
		Code:     10021,
		HTTPCode: 500,
		Desc:     "后台缓存读取错误",
	}
	// ErrRouteError 路由解析失败
	ErrRouteError = ErrCode{
		Code:     10022,
		HTTPCode: 404,
		Desc:     "路由解析失败",
	}
	// ErrVersionError 参数错误
	ErrParaVersionError = ErrCode{
		Code:     10023,
		HTTPCode: 200,
		Desc:     "参数错误",
	}
	// ErrSuggestVersionError app版本过低
	ErrSuggestVersionError = ErrCode{
		Code:     10024,
		HTTPCode: 200,
		Desc:     "建议升级APP版本",
	}
	// ErrForceVersionError app版本过低
	ErrForceVersionError = ErrCode{
		Code:     10025,
		HTTPCode: 200,
		Desc:     "app版本过低",
	}
)
