2024-08-20 15:32:06 +00:00
|
|
|
package errors
|
2024-08-19 17:15:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-08-20 15:32:06 +00:00
|
|
|
"code.30cm.net/digimon/library-go/errors/code"
|
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2024-08-20 15:32:06 +00:00
|
|
|
func newErr(scope, detail uint32, msg string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
cat := detail / 100 * 100
|
2024-08-20 15:32:06 +00:00
|
|
|
|
|
|
|
return &LibError{
|
2024-08-19 17:15:23 +00:00
|
|
|
category: cat,
|
|
|
|
code: detail,
|
|
|
|
scope: scope,
|
|
|
|
msg: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:32:06 +00:00
|
|
|
func newBuiltinGRPCErr(scope, detail uint32, msg string) *LibError {
|
|
|
|
return &LibError{
|
2024-08-19 17:15:23 +00:00
|
|
|
category: code.CatGRPC,
|
|
|
|
code: detail,
|
|
|
|
scope: scope,
|
|
|
|
msg: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromError tries to let error as Err
|
|
|
|
// it supports to unwrap error that has Err
|
|
|
|
// return nil if failed to transfer
|
2024-08-20 15:32:06 +00:00
|
|
|
func FromError(err error) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:32:06 +00:00
|
|
|
var e *LibError
|
2024-08-19 17:15:23 +00:00
|
|
|
if errors.As(err, &e) {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromCode parses code as following
|
|
|
|
// Decimal: 120314
|
|
|
|
// 12 represents Scope
|
|
|
|
// 03 represents Category
|
|
|
|
// 14 represents Detail error code
|
2024-08-20 15:32:06 +00:00
|
|
|
func FromCode(code uint32) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
scope := code / 10000
|
|
|
|
detail := code % 10000
|
2024-08-20 15:32:06 +00:00
|
|
|
|
|
|
|
return &LibError{
|
2024-08-19 17:15:23 +00:00
|
|
|
category: detail / 100 * 100,
|
|
|
|
code: detail,
|
|
|
|
scope: scope,
|
|
|
|
msg: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromGRPCError transfer error to Err
|
|
|
|
// useful for gRPC client
|
2024-08-20 15:32:06 +00:00
|
|
|
func FromGRPCError(err error) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
s, _ := status.FromError(err)
|
|
|
|
e := FromCode(uint32(s.Code()))
|
|
|
|
e.msg = s.Message()
|
|
|
|
|
|
|
|
// For GRPC built-in code
|
|
|
|
if e.Scope() == code.Unset && e.Category() == 0 && e.Code() != code.OK {
|
|
|
|
e = newBuiltinGRPCErr(Scope, e.Code(), s.Message())
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: check GRPCStatus() in Errs struct
|
|
|
|
// ToGRPCError returns the status.Status
|
|
|
|
// Useful to return error in gRPC server
|
2024-08-20 15:32:06 +00:00
|
|
|
func ToGRPCError(e *LibError) error {
|
2024-08-19 17:15:23 +00:00
|
|
|
return status.New(codes.Code(e.FullCode()), e.Error()).Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** System ***/
|
|
|
|
|
|
|
|
// SystemTimeoutError returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func SystemTimeoutError(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.SystemTimeoutError, fmt.Sprintf("system timeout: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SystemTimeoutErrorL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func SystemTimeoutErrorL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := SystemTimeoutError(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// SystemInternalError returns Err struct
|
2024-08-20 15:32:06 +00:00
|
|
|
func SystemInternalError(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.SystemInternalError, fmt.Sprintf("internal error: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SystemInternalErrorL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func SystemInternalErrorL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := SystemInternalError(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// SystemMaintainErrorL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func SystemMaintainErrorL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := SystemMaintainError(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// SystemMaintainError returns Err struct
|
2024-08-20 15:32:06 +00:00
|
|
|
func SystemMaintainError(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.SystemMaintainError, fmt.Sprintf("service under maintenance: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CatInput ***/
|
|
|
|
|
|
|
|
// InvalidFormat returns Err struct
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidFormat(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InvalidFormat, fmt.Sprintf("invalid format: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidFormatL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidFormatL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InvalidFormat(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidRange returns Err struct
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidRange(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InvalidRange, fmt.Sprintf("invalid range: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidRangeL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidRangeL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InvalidRange(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotValidImplementation returns Err struct
|
2024-08-20 15:32:06 +00:00
|
|
|
func NotValidImplementation(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.NotValidImplementation, fmt.Sprintf("not valid implementation: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotValidImplementationL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func NotValidImplementationL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := NotValidImplementation(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CatDB ***/
|
|
|
|
|
|
|
|
// DBError returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func DBError(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.DBError, fmt.Sprintf("db error: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBErrorL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func DBErrorL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := DBError(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBDataConvert returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func DBDataConvert(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.DBDataConvert, fmt.Sprintf("data from db convert error: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBDataConvertL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func DBDataConvertL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := DBDataConvert(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBDuplicate returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func DBDuplicate(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.DBDuplicate, fmt.Sprintf("data Duplicate key error: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBDuplicateL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func DBDuplicateL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := DBDuplicate(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CatResource ***/
|
|
|
|
|
|
|
|
// ResourceNotFound returns Err and logging
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceNotFound(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.ResourceNotFound, fmt.Sprintf("resource not found: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceNotFoundL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceNotFoundL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := ResourceNotFound(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidResourceFormat returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidResourceFormat(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InvalidResourceFormat, fmt.Sprintf("invalid resource format: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidResourceFormatL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidResourceFormatL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InvalidResourceFormat(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidResourceState returns status not correct.
|
|
|
|
// for example: company should be destroy, agent should be no-sensor/fail-install ...
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidResourceState(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InvalidResourceState, fmt.Sprintf("invalid resource state: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidResourceStateL logs error message and returns status not correct.
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidResourceStateL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InvalidResourceState(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceInsufficient(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.ResourceInsufficient,
|
|
|
|
fmt.Sprintf("insufficient resource: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceInsufficientL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := ResourceInsufficient(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsufficientPermission returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InsufficientPermission(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InsufficientPermission,
|
|
|
|
fmt.Sprintf("insufficient permission: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsufficientPermissionL returns Err and log
|
2024-08-20 15:32:06 +00:00
|
|
|
func InsufficientPermissionL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InsufficientPermission(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceAlreadyExist returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceAlreadyExist(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.ResourceAlreadyExist, fmt.Sprintf("resource already exist: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceAlreadyExistL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceAlreadyExistL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := ResourceAlreadyExist(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidMeasurementID returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidMeasurementID(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InvalidMeasurementID, fmt.Sprintf("missing measurement id: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidMeasurementIDL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidMeasurementIDL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InvalidMeasurementID(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceExpired returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceExpired(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.ResourceExpired, fmt.Sprintf("resource expired: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceExpiredL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceExpiredL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := ResourceExpired(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceMigrated returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceMigrated(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.ResourceMigrated, fmt.Sprintf("resource migrated: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceMigratedL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ResourceMigratedL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := ResourceMigrated(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsufficientQuota returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InsufficientQuota(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InsufficientQuota, fmt.Sprintf("insufficient quota: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsufficientQuotaL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InsufficientQuotaL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InsufficientQuota(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CatAuth ***/
|
|
|
|
|
|
|
|
// Unauthorized returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func Unauthorized(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.Unauthorized, fmt.Sprintf("unauthorized: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnauthorizedL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func UnauthorizedL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := Unauthorized(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthExpired returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func AuthExpired(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.AuthExpired, fmt.Sprintf("expired: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthExpiredL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func AuthExpiredL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := AuthExpired(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidPosixTime returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidPosixTime(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.InvalidPosixTime, fmt.Sprintf("invalid posix time: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidPosixTimeL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func InvalidPosixTimeL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := InvalidPosixTime(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// SigAndPayloadNotMatched returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func SigAndPayloadNotMatched(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.SigAndPayloadNotMatched, fmt.Sprintf("signature and the payload are not match: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SigAndPayloadNotMatchedL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func SigAndPayloadNotMatchedL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := SigAndPayloadNotMatched(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forbidden returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func Forbidden(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.Forbidden, fmt.Sprintf("forbidden: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForbiddenL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ForbiddenL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := Forbidden(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAuthUnauthorizedError check the err is unauthorized error
|
2024-08-20 15:32:06 +00:00
|
|
|
func IsAuthUnauthorizedError(err *LibError) bool {
|
2024-08-19 17:15:23 +00:00
|
|
|
switch err.Code() {
|
|
|
|
case code.Unauthorized, code.AuthExpired, code.InvalidPosixTime,
|
|
|
|
code.SigAndPayloadNotMatched, code.Forbidden,
|
|
|
|
code.InvalidFormat, code.ResourceNotFound:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CatXBC ***/
|
|
|
|
|
|
|
|
// ArkInternal returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ArkInternal(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.ArkInternal, fmt.Sprintf("ark internal error: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArkInternalL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func ArkInternalL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := ArkInternal(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** CatPubSub ***/
|
|
|
|
|
|
|
|
// Publish returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func Publish(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.Publish, fmt.Sprintf("publish: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func PublishL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := Publish(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func Consume(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.Consume, fmt.Sprintf("consume: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MsgSizeTooLarge returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func MsgSizeTooLarge(s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
return newErr(Scope, code.MsgSizeTooLarge, fmt.Sprintf("kafka error: %s", strings.Join(s, " ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MsgSizeTooLargeL logs error message and returns Err
|
2024-08-20 15:32:06 +00:00
|
|
|
func MsgSizeTooLargeL(l logx.Logger, s ...string) *LibError {
|
2024-08-19 17:15:23 +00:00
|
|
|
e := MsgSizeTooLarge(s...)
|
|
|
|
l.WithCallerSkip(1).Error(e.Error())
|
2024-08-20 15:32:06 +00:00
|
|
|
|
2024-08-19 17:15:23 +00:00
|
|
|
return e
|
|
|
|
}
|