You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
goctls-template/rpc/dberrorhandler.tpl

41 lines
1.7 KiB
Smarty

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package dberrorhandler
import (
"git.noahlan.cn/noahlan/ntool/nlog"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"
// api
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
"{{.package}}/ent"
)
// HandleEntErr returns errors dealing with default functions.
func HandleEntErr(logger logx.Logger, err error, detail any) error {
logger := nlog.WithCallerSkip(1)
if err != nil {
switch {
case ent.IsNotFound(err):
//
logger.Errorw(err.Error(), nlog.Field("detail", detail))
return nstatus.NewBizErr(code.StatusNotFound, msg.ObjectNotFound)
case ent.IsConstraintError(err):
// /
logger.Errorw(err.Error(), nlog.Field("detail", detail))
return nstatus.NewBizErr(code.StatusConflict, msg.ConstraintError)
case ent.IsValidationError(err):
//
logger.Errorw(err.Error(), nlog.Field("detail", detail))
return nstatus.NewBizErr(code.StatusBadRequest, msg.ValidationError)
case ent.IsNotSingular(err):
// Limit
logger.Errorw(err.Error(), nlog.Field("detail", detail))
return nstatus.NewBizErr(code.StatusBadRequest, msg.NotSingularError)
default:
logger.Errorw("database error", nlog.Field("err", err.Error()), nlog.Field("detail", detail))
return nstatus.NewGrpcInternalErr(msg.DatabaseError)
}
}
return err
}