1 Star 0 Fork 0

go-spring2/spring-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rpc-result.go 3.06 KB
一键复制 编辑 原始数据 按行查看 历史
kzhu 提交于 27天前 . feat: update package name
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package web
import (
"fmt"
"math"
"runtime"
"gitee.com/go-spring2/spring-base/util"
)
var (
ERROR = NewRpcError(-1, "ERROR")
SUCCESS = NewRpcSuccess(200, "SUCCESS")
DEFAULT = NewErrorCode(math.MaxInt32, "DEFAULT")
)
// ErrorCode 错误码
type ErrorCode struct {
Code int32 `json:"code"` // 错误码
Msg string `json:"msg"` // 错误信息
}
// NewErrorCode ErrorCode 的构造函数
func NewErrorCode(code int32, msg string) ErrorCode {
return ErrorCode{Code: code, Msg: msg}
}
// RpcResult 定义 RPC 返回值
type RpcResult struct {
ErrorCode
Err string `json:"err,omitempty"` // 错误源
Data interface{} `json:"data,omitempty"` // 返回值
}
// RpcSuccess 定义一个 RPC 成功值
type RpcSuccess ErrorCode
// NewRpcSuccess RpcSuccess 的构造函数
func NewRpcSuccess(code int32, msg string) RpcSuccess {
return RpcSuccess(NewErrorCode(code, msg))
}
// Data 绑定一个值
func (r RpcSuccess) Data(data interface{}) *RpcResult {
return &RpcResult{ErrorCode: ErrorCode(r), Data: data}
}
// RpcError 定义一个 RPC 异常值
type RpcError ErrorCode
// NewRpcError RpcError 的构造函数
func NewRpcError(code int32, msg string) RpcError {
return RpcError(NewErrorCode(code, msg))
}
// Error 绑定一个错误
func (r RpcError) Error(err error) *RpcResult {
return r.error(1, err, nil)
}
// ErrorWithData 绑定一个错误和一个值
func (r RpcError) ErrorWithData(err error, data interface{}) *RpcResult {
return r.error(1, err, data)
}
// WithFileLine 返回错误发生的文件行号,skip 是相对于当前函数的深度。
func WithFileLine(err error, skip int) error {
_, file, line, _ := runtime.Caller(skip + 1)
return fmt.Errorf("%s:%d: %w", file, line, err)
}
// error skip 是相对于当前函数的调用深度
func (r RpcError) error(skip int, err error, data interface{}) *RpcResult {
str := WithFileLine(err, skip+1).Error()
return &RpcResult{ErrorCode: ErrorCode(r), Err: str, Data: data}
}
// Panic 抛出一个异常值
func (r RpcError) Panic(err error) *util.PanicCond {
return util.NewPanicCond(func() interface{} {
return r.error(2, err, nil)
})
}
// Panicf 抛出一段需要格式化的错误字符串
func (r RpcError) Panicf(format string, a ...interface{}) *util.PanicCond {
return util.NewPanicCond(func() interface{} {
return r.error(2, fmt.Errorf(format, a...), nil)
})
}
// PanicImmediately 立即抛出一个异常值
func (r RpcError) PanicImmediately(err error) {
panic(r.error(1, err, nil))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-spring2/spring-core.git
git@gitee.com:go-spring2/spring-core.git
go-spring2
spring-core
spring-core
v1.1.3

搜索帮助