gitee.com/hongliu9527/go-tools@v0.0.8/errors/gcode/gcode_local.go (about) 1 /* 2 * @Author: hongliu 3 * @Date: 2022-12-29 10:51:12 4 * @LastEditors: hongliu 5 * @LastEditTime: 2022-12-29 14:49:23 6 * @FilePath: \go-tools\errors\gcode\gcode_local.go 7 * @Description:外部错误处理包具体实现 8 * 9 * Copyright (c) 2022 by 洪流, All Rights Reserved. 10 */ 11 12 package gcode 13 14 import ( 15 "fmt" 16 ) 17 18 // localCode is an implementer for interface Code for internal usage only. 19 type localCode struct { 20 code int // Error code, usually an integer. 21 message string // Brief message for this error code. 22 detail interface{} // As type of interface, it is mainly designed as an extension field for error code. 23 } 24 25 // Code returns the integer number of current error code. 26 func (c localCode) Code() int { 27 return c.code 28 } 29 30 // Message returns the brief message for current error code. 31 func (c localCode) Message() string { 32 return c.message 33 } 34 35 // Detail returns the detailed information of current error code, 36 // which is mainly designed as an extension field for error code. 37 func (c localCode) Detail() interface{} { 38 return c.detail 39 } 40 41 // MarshalMap 变为可序列化的哈希表 42 func (c localCode) MarshalMap() map[string]interface{} { 43 mashalMap := make(map[string]interface{}) 44 mashalMap["code"] = c.code 45 mashalMap["message"] = c.message 46 mashalMap["detail"] = c.detail 47 48 return mashalMap 49 } 50 51 // String returns current error code as a string. 52 func (c localCode) String() string { 53 if c.detail != nil { 54 return fmt.Sprintf(`%d:%s %v`, c.code, c.message, c.detail) 55 } 56 if c.message != "" { 57 return fmt.Sprintf(`%d:%s`, c.code, c.message) 58 } 59 return fmt.Sprintf(`%d`, c.code) 60 }