package gatecode

import (
	"errors"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
)

var (
	UndefinedRouter = errors.New("未知路由")
	RouterUpholding = errors.New("接口维护中")
)

// gatewayCode 错误信息累加器
func getErrCode(code int) func() int {
	return func() int {
		code++
		return code
	}
}

var (
	//网关应用错误类型 1000-2000
	gatewayErrCode            = getErrCode(1000)
	GatewayUndefinedRouterErr = gerror.WrapCode(gcode.New(gatewayErrCode(), UndefinedRouter.Error(), "未知路由地址"), UndefinedRouter)

	//服务接口错误类型 3000-4000
	serverErrCode            = getErrCode(2000)
	ServerRouterUpholdingErr = gerror.WrapCode(gcode.New(serverErrCode(), RouterUpholding.Error(), "接口维护中"), RouterUpholding)
)

// NewErrorWithCode 创建异常
func NewErrorWithCode(err ErrCode, detail string) error {
	return gerror.WrapCode(gcode.New(int(err), err.String(), detail), errors.New(err.String()))
}
