exception

package
v0.0.0-...-ad531c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2023 License: MIT Imports: 7 Imported by: 20

README

包装网络层对象通用异常返回

注意:这里仅用于包装网络层的异常返回结果。因为官方给的接口文档有点头大

这里需要配合constant里的枚举类:CodeMsg

提供两个方法,WithCodeMsg(枚举值)、WithMsg(错误消息)


// WithCodeMsg 传递CodeMsg
func WithCodeMsg(msg *constant.CodeMsg) *Exception {
	return &Exception{
		Code: msg.StatusCode,
		Msg:  msg.StatusMsg,
		Meta: msg,
	}
}

// WithMsg 传递Msg
func WithMsg(format string, a ...any) *Exception {
	// 错误只带 msg,返回的 code 默认为 1
	return WithCodeMsg(constant.NewCodeMsg(1, fmt.Sprintf(format, a...)))
}

之后可继续封装,这里比较随意

Documentation

Overview

@Author: Ciusyan 2023/2/7

@Author: Ciusyan 2023/2/13

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Err

func Err(c int32, msg string) error

Err returns an error representing c and msg. If c is OK, returns nil.

func Errorf

func Errorf(c int32, format string, a ...interface{}) error

Errorf returns Error(c, fmt.Sprintf(format, a...)).

func GinErrWrapper

func GinErrWrapper(handler AppHandler) func(c *gin.Context)

GinErrWrapper 用于统一处理控制层 error

func GrpcErrWrapper

func GrpcErrWrapper(err error) *custom.Exception

GrpcErrWrapper 用于包装 GPRC 调用产生的 err

func WithCodeMsg

func WithCodeMsg(codeMsg *custom.CodeMsg) *custom.Exception

WithCodeMsg 传递CodeMsg

func WithDetails

func WithDetails(code int32, msg string, details ...interface{}) *custom.Exception

WithDetails 传递Details

func WithStatusCode

func WithStatusCode(code constant.StatusCode) *custom.Exception

func WithStatusMsg

func WithStatusMsg(msg string) *custom.Exception

func WithStatusMsgf

func WithStatusMsgf(format string, a ...any) *custom.Exception

WithStatusMsgf 传递Msg

Types

type AppHandler

type AppHandler func(c *gin.Context) error

Directories

Path Synopsis
@Author: Ciusyan 2023/2/14
@Author: Ciusyan 2023/2/14

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL