github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/protocol/error/error_code.go (about)

     1  package error
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/nyan233/littlerpc/core/utils/convert"
     6  )
     7  
     8  // 定义LittleRpc内部会使用到的错误码
     9  
    10  type Code int
    11  
    12  func (c Code) String() string {
    13  	bytes, _ := c.MarshalJSON()
    14  	return convert.BytesToString(bytes)
    15  }
    16  
    17  func (c Code) MarshalJSON() ([]byte, error) {
    18  	codeStr, ok := mappingStr[c]
    19  	// 用户自定义的错误玛
    20  	if !ok {
    21  		return convert.StringToBytes(fmt.Sprintf("\"Custom(%d)\"", c)), nil
    22  	}
    23  	return convert.StringToBytes(codeStr), nil
    24  }
    25  
    26  const (
    27  	Success               = 200  // 成功返回
    28  	Unknown               = 730  // 用户过程返回了错误,但不是LittleRpc可以识别的错误
    29  	ServiceNotFound       = 750  // 需要调用的服务不存在
    30  	MessageDecodingFailed = 780  // 载荷消息解码失败
    31  	MessageEncodingFailed = 1060 // 载荷消息编码失败
    32  	ServerError           = 690  // 服务器的其它错误
    33  	ClientError           = 580  // 客户端产生的错误
    34  	// CallArgsTypeErr TODO: 计划删除, v0.2.0时代的遗留产物
    35  	CallArgsTypeErr = 1030 // 过程的调用参数类型错误
    36  	CodecMarshalErr = 1050 // Codec在序列化数据时出错
    37  	ConnectionErr   = 1070 // 连接错误
    38  	ContextNotFound = 1080 // 要取消的context不存在
    39  	UnsafeOption    = 2060 // 不安全的选项, 通常在服务器需要的东西没有准备好时触发
    40  )
    41  
    42  // NOTE: 不要尝试修改这个表,这个表不应该在运行时被改变或者被使用到
    43  // NOTE: Little-Rpc的用户代码改变
    44  var mappingStr = map[Code]string{
    45  	Success:               "\"Success\"",
    46  	Unknown:               "\"Unknown\"",
    47  	ServiceNotFound:       "\"ServiceNotFound\"",
    48  	MessageDecodingFailed: "\"MessageDecodingFailed\"",
    49  	MessageEncodingFailed: "\"MessageEncodingFailed\"",
    50  	ServerError:           "\"ServerError\"",
    51  	ClientError:           "\"ClientError\"",
    52  	CallArgsTypeErr:       "\"CallArgsTypeErr\"",
    53  	CodecMarshalErr:       "\"CodecMarshalErr\"",
    54  	ConnectionErr:         "\"ConnectionErr\"",
    55  	ContextNotFound:       "\"ContextNotFound\"",
    56  	UnsafeOption:          "\"UnsafeOption\"",
    57  }