Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chenjie199234/Corelib
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.115
Choose a base ref
...
head repository: chenjie199234/Corelib
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Loading
Showing with 6,486 additions and 5,322 deletions.
  1. +14 −13 Corelib.drawio
  2. BIN Corelib.jpg
  3. +21 −16 README.md
  4. +73 −96 cerror/cerror.go
  5. +18 −35 cerror/cerror.pb.go
  6. +0 −12 cerror/cerror_test.go
  7. +38 −37 cerror/const.go
  8. +99 −67 cgrpc/balancer.go
  9. +123 −119 cgrpc/client.go
  10. +146 −0 cgrpc/clientcontext.go
  11. +0 −108 cgrpc/context.go
  12. +89 −0 cgrpc/error.go
  13. +4 −4 cgrpc/mids/mids.go
  14. +272 −48 cgrpc/protoc-gen-go-cgrpc/cgrpc.go
  15. +6 −10 cgrpc/protoc-gen-go-cgrpc/main.go
  16. +29 −120 cgrpc/protoc-gen-go-cgrpc/test/test.proto
  17. +258 −163 cgrpc/server.go
  18. +4 −2 cgrpc/server_for_pick.go
  19. +290 −0 cgrpc/servercontext.go
  20. +62 −81 codegen/main.go
  21. +77 −24 codegen/tml/cmd/template_cmd.go
  22. +92 −107 codegen/tml/config/template_config.go
  23. +7 −1 codegen/tml/configfile/template_configfile.go
  24. +6 −5 codegen/tml/dao/template_dao.go
  25. +15 −11 codegen/tml/deploy/deploy.go
  26. +4 −3 codegen/tml/ecode/ecode.go
  27. +7 −7 codegen/tml/gomod/template_gomod.go
  28. +4 −4 codegen/tml/html/html.go
  29. +97 −9 codegen/tml/mainfile/template_main.go
  30. +7 −7 codegen/tml/model/template_model.go
  31. +8 −5 codegen/tml/readme/template_readme.go
  32. +23 −16 codegen/tml/server/xcrpc/template_xcrpc.go
  33. +23 −16 codegen/tml/server/xgrpc/template_xgrpc.go
  34. +92 −0 codegen/tml/server/xraw/template_xraw.go
  35. +26 −17 codegen/tml/server/xweb/template_xweb.go
  36. +101 −0 codegen/tml/service/raw/template_serviceraw.go
  37. +8 −8 codegen/tml/service/status/template_servicestatus.go
  38. +1 −1 codegen/tml/service/sub/template_sub.go
  39. +6 −0 codegen/tml/service/template_service.go
  40. +1 −1 container/hashtree/fixedhashtree.go
  41. 0 { → container}/id/id.go
  42. 0 { → container}/id/id_test.go
  43. +21 −8 container/list/blocklist.go
  44. +2 −2 container/list/blocklist_test.go
  45. +9 −8 container/list/list.go
  46. +161 −0 container/list/list_test.go
  47. +5 −5 container/lru/lru.go
  48. +92 −0 container/stack/blockstack.go
  49. +8 −0 container/stack/blockstack_test.go
  50. +5 −4 container/stack/stack.go
  51. +1 −1 container/stack/stack_test.go
  52. +3 −0 cotel/const.go
  53. +287 −0 cotel/cotel.go
  54. +34 −32 {monitor → cotel}/cpu.go
  55. +5 −5 {monitor → cotel}/golang.go
  56. +122 −0 cotel/mem.go
  57. +34 −57 crpc/balancer.go
  58. +254 −158 crpc/client.go
  59. +231 −0 crpc/clientcontext.go
  60. +0 −119 crpc/context.go
  61. +4 −4 crpc/mids/mids.go
  62. +225 −105 crpc/msg.pb.go
  63. +21 −10 crpc/msg.proto
  64. +271 −83 crpc/protoc-gen-go-crpc/crpc.go
  65. +6 −10 crpc/protoc-gen-go-crpc/main.go
  66. +28 −120 crpc/protoc-gen-go-crpc/test/test.proto
  67. +164 −0 crpc/rw.go
  68. +267 −275 crpc/server.go
  69. +64 −27 crpc/server_for_pick.go
  70. +344 −0 crpc/servercontext.go
  71. +1 −1 discover/discover.go
  72. +16 −18 discover/dns.go
  73. +39 −39 discover/kubernetes.go
  74. +2 −2 discover/static.go
  75. +25 −18 email/email.go
  76. +63 −37 go.mod
  77. +145 −96 go.sum
  78. +67 −41 internal/picker/picker.go
  79. +1 −1 internal/version/version.go
  80. +0 −213 log/log.go
  81. +0 −368 log/trace/trace.go
  82. +17 −9 metadata/metadata.go
  83. +11 −24 mids/access.go
  84. +37 −37 mids/rate.go
  85. +12 −16 mids/session.go
  86. +2 −3 mids/session_test.go
  87. +5 −7 mids/token.go
  88. +4 −4 mids/token_test.go
  89. +41 −31 mongo/mongo.go
  90. +0 −146 monitor/client.go
  91. +0 −122 monitor/mem.go
  92. +0 −563 monitor/monitor.go
  93. +0 −147 monitor/server.go
  94. +277 −110 mysql/operator.go
  95. +4 −0 pbex/pbex.go
  96. +27 −27 pbex/pbex.pb.go
  97. +15 −8 pbex/protoc-gen-go-pbex/main.go
  98. +11 −6 pool/bpool/bpool.go
  99. +4 −4 redis/broadcast.go
  100. +6 −6 redis/broadcast_test.go
  101. +5 −5 redis/prioritymq.go
  102. +3 −3 redis/prioritymq_test.go
  103. +1 −1 redis/ratelimit.go
  104. +38 −14 redis/redis.go
  105. +4 −4 redis/unicast.go
  106. +2 −2 redis/unicast_test.go
  107. +4 −4 rotatefile/rotatefile_test.go
  108. +4 −1 stream/config.go
  109. +60 −60 stream/conn.go
  110. +11 −12 stream/connmng.go
  111. +2 −0 stream/instance.go
  112. +38 −33 stream/peer.go
  113. +1 −1 stream/tcpclient_test.go
  114. +6 −6 stream/wsclient_test.go
  115. +17 −1 superd/super.go
  116. +0 −228 superd/util.go
  117. +1 −1 util/common/common.go
  118. +58 −6 util/name/name.go
  119. +1 −1 util/oneshot/oneshot_test.go
  120. +18 −15 web/balancer.go
  121. +150 −80 web/client.go
  122. +29 −68 web/context.go
  123. +31 −127 web/protoc-gen-browser/browser.go
  124. +12 −24 web/protoc-gen-browser/main.go
  125. +27 −4 web/protoc-gen-browser/test/test.proto
  126. +9 −10 web/protoc-gen-go-web/main.go
  127. +28 −5 web/protoc-gen-go-web/test/test.proto
  128. +64 −39 web/protoc-gen-go-web/web.go
  129. +9 −10 web/protoc-gen-markdown/main.go
  130. +4 −2 web/protoc-gen-markdown/markdown.go
  131. +27 −4 web/protoc-gen-markdown/test/test.proto
  132. +146 −183 web/router.go
  133. +15 −31 web/server.go
  134. +4 −2 web/server_for_pick.go
  135. +1 −3 ws/const.go
  136. +4 −13 ws/cupgrader.go
  137. +1 −9 ws/supgrader.go
27 changes: 14 additions & 13 deletions Corelib.drawio
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mxfile host="app.diagrams.net" modified="2023-05-03T05:55:21.234Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15" etag="1QnFc4UdSHFpoma89dGD" version="21.1.8" type="device">
<mxfile host="app.diagrams.net" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0" version="24.7.16">
<diagram id="rUxL1oKWxs3ilogCRd9T" name="Page-1">
<mxGraphModel dx="2021" dy="703" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<mxGraphModel dx="2015" dy="658" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
@@ -12,7 +12,7 @@
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="Cn2GFig116qeOLnLPgPs-3" value="&lt;br&gt;&lt;br&gt;admin Service&lt;br&gt;(user/role/permission)&lt;br&gt;(app/config/proxy)" style="rounded=1;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxCell id="Cn2GFig116qeOLnLPgPs-3" value="&lt;br&gt;&lt;br&gt;admin Service&lt;br&gt;(user/role/permission)&lt;br&gt;(app/remote config)" style="rounded=1;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="360" y="460" width="120" height="80" as="geometry" />
</mxCell>
<mxCell id="Cn2GFig116qeOLnLPgPs-25" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontColor=#FF0000;strokeColor=#FF0000;" parent="1" source="Cn2GFig116qeOLnLPgPs-7" target="Cn2GFig116qeOLnLPgPs-3" edge="1">
@@ -21,12 +21,13 @@
<mxCell id="wmVFEGKWtfOXgKhMhSeI-5" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.25;exitDx=0;exitDy=0;entryX=1;entryY=0.25;entryDx=0;entryDy=0;" parent="1" source="Cn2GFig116qeOLnLPgPs-7" target="wmVFEGKWtfOXgKhMhSeI-4" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="wmVFEGKWtfOXgKhMhSeI-24" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fontColor=#000000;strokeColor=#000000;" parent="1" source="Cn2GFig116qeOLnLPgPs-7" target="wmVFEGKWtfOXgKhMhSeI-21" edge="1">
<mxCell id="wmVFEGKWtfOXgKhMhSeI-24" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;fontColor=#000000;strokeColor=#000000;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="Cn2GFig116qeOLnLPgPs-7" target="wmVFEGKWtfOXgKhMhSeI-21" edge="1">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="110" y="395" />
<mxPoint x="110" y="680" />
<mxPoint x="110" y="695" />
</Array>
<mxPoint x="300" y="690" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="Cn2GFig116qeOLnLPgPs-7" value="&lt;br&gt;&lt;br&gt;business service A" style="rounded=1;whiteSpace=wrap;html=1;" parent="1" vertex="1">
@@ -42,7 +43,7 @@
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="730" y="395" />
<mxPoint x="730" y="680" />
<mxPoint x="730" y="695" />
</Array>
</mxGeometry>
</mxCell>
@@ -109,28 +110,28 @@
<mxCell id="wmVFEGKWtfOXgKhMhSeI-19" value="&lt;font color=&quot;#00cccc&quot;&gt;inner call&lt;/font&gt;" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontColor=#00CC00;" parent="1" vertex="1">
<mxGeometry x="500" y="340" width="60" height="20" as="geometry" />
</mxCell>
<mxCell id="wmVFEGKWtfOXgKhMhSeI-21" value="&lt;font color=&quot;#000000&quot;&gt;k8s&lt;br&gt;core dns&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#00CCCC;" parent="1" vertex="1">
<mxGeometry x="360" y="650" width="120" height="60" as="geometry" />
<mxCell id="wmVFEGKWtfOXgKhMhSeI-21" value="&lt;font color=&quot;#000000&quot;&gt;dicover:&lt;/font&gt;&lt;div&gt;&lt;font color=&quot;#000000&quot;&gt;1.static addr&lt;br&gt;&lt;/font&gt;&lt;div&gt;&lt;font color=&quot;#000000&quot;&gt;2.k8s api server&lt;/font&gt;&lt;/div&gt;&lt;div&gt;&lt;font color=&quot;#000000&quot;&gt;3.dns&lt;/font&gt;&lt;/div&gt;&lt;/div&gt;" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#00CCCC;" parent="1" vertex="1">
<mxGeometry x="360" y="650" width="120" height="90" as="geometry" />
</mxCell>
<mxCell id="wmVFEGKWtfOXgKhMhSeI-25" value="1.loop in x seconds to&lt;br&gt;update discover&lt;br&gt;2.connection lost triger&lt;br&gt;update discover" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="-30" y="420" width="140" height="60" as="geometry" />
</mxCell>
<mxCell id="wmVFEGKWtfOXgKhMhSeI-26" value="1.loop in x seconds to&lt;br&gt;update discover&lt;br&gt;2.connection lost triger&lt;br&gt;update discover" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="730" y="420" width="140" height="60" as="geometry" />
</mxCell>
<mxCell id="OUvODyNElc9ULqvnpfPR-2" value="&lt;font color=&quot;#ffb570&quot;&gt;proxy call&lt;/font&gt;" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#FFB570;" edge="1" parent="1" source="Cn2GFig116qeOLnLPgPs-3" target="wmVFEGKWtfOXgKhMhSeI-12">
<mxCell id="OUvODyNElc9ULqvnpfPR-2" value="&lt;font color=&quot;#ffb570&quot;&gt;proxy call&lt;/font&gt;" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#FFB570;" parent="1" source="Cn2GFig116qeOLnLPgPs-3" target="wmVFEGKWtfOXgKhMhSeI-12" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="OUvODyNElc9ULqvnpfPR-3" value="&lt;font color=&quot;#ffb570&quot;&gt;proxy call&lt;/font&gt;" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.25;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#FFB570;" edge="1" parent="1" source="Cn2GFig116qeOLnLPgPs-3" target="wmVFEGKWtfOXgKhMhSeI-11">
<mxCell id="OUvODyNElc9ULqvnpfPR-3" value="&lt;font color=&quot;#ffb570&quot;&gt;proxy call&lt;/font&gt;" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.25;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;strokeColor=#FFB570;" parent="1" source="Cn2GFig116qeOLnLPgPs-3" target="wmVFEGKWtfOXgKhMhSeI-11" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="OUvODyNElc9ULqvnpfPR-7" value="web" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxCell id="OUvODyNElc9ULqvnpfPR-7" value="web" style="rounded=1;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="400" y="460" width="40" height="20" as="geometry" />
</mxCell>
<mxCell id="OUvODyNElc9ULqvnpfPR-9" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.75;entryY=0;entryDx=0;entryDy=0;strokeColor=#FFB570;" edge="1" parent="1" source="OUvODyNElc9ULqvnpfPR-8" target="Cn2GFig116qeOLnLPgPs-29">
<mxCell id="OUvODyNElc9ULqvnpfPR-9" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.75;entryY=0;entryDx=0;entryDy=0;strokeColor=#FFB570;" parent="1" source="OUvODyNElc9ULqvnpfPR-8" target="Cn2GFig116qeOLnLPgPs-29" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="OUvODyNElc9ULqvnpfPR-8" value="&lt;font color=&quot;#ffb570&quot;&gt;manager&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#FFB570;" vertex="1" parent="1">
<mxCell id="OUvODyNElc9ULqvnpfPR-8" value="&lt;font color=&quot;#ffb570&quot;&gt;manager&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;strokeColor=#FFB570;" parent="1" vertex="1">
<mxGeometry x="460" y="180" width="120" height="60" as="geometry" />
</mxCell>
</root>
Binary file modified Corelib.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
# Corelib
![image](Corelib.jpg)
# Use
1.install golang(1.21+)
1.install golang(1.24.1+)
2.install git
3.install protoc
4.install protoc-gen-go
5.[download the codegen in release] or [clone this lib and go into the codegen dir and install it by go install]
5.[download the codegen in release] or [clone this repo and go into the codegen dir and install it by go install]
6.make sure the codegen's path is in the system's or user's PATH env
7.codegen -d path -n projectname -p packagename
(without -d or -d is empty the default:current dir will be used)
(projectname format,character must in [a-z][0-9],first character must in [a-z])
(packagename must end with projectname,default:projectname will be used as packagename,e.g. github.com/path_to_the_repo/projectname)
7.use this command to generate the base code:codegen -n projectname -p packagename
(projectname format:character must in [a-z][0-9],first character must in [a-z])
(packagename format:must end with projectname,default:projectname,e.g. github.com/path_to_the_repo/projectname)
# Description
## codegen
generate the base code,if you want to use this lib,this is the start
## web
Web framework,this contains protobuf plugin to generate code from proto file
Web framework contains trace and metrics function from opentelemetry,this also contains protobuf plugin to generate code from proto file
## cgrpc
grpc framework,this contains protobuf plugin to generate code from proto file
grpc framework contains trace and metrics function from opentelemetry,this also contains protobuf plugin to generate code from proto file
## crpc
a new rpc framework contains trace and metrics function from opentelemetry,this also contains protobuf plugin to generate code from proto file
## pbex
protobuf extension for message validate,this is used in the web/crpc/cgrpc framework
## stream
stream connection framework,use websocket's frame format,so this can support rawtcp and websocket at the same time on same port
## crpc
a new rpc framework(20-30% fast then grpc),this contains protobuf plugin to generate code from proto file
## id
snow flake
## log
log and trace log
## pool
sync.Pool for []byte and bufio.Reader and bufio.Writer
## ws
a simple ws handshake and message recv and send framework
## container
some useful struct
## mysql
mysql driver wrapper with trace support and read write seprate
## mongo
mongodb driver wrapper with trace support
## redis
redis driver wrapper with trace support and some useful function(bloom filter,rate limiter,message queue,verify code check)
## email
simple email client used to send email
## superd
fork child process to exec some other program,like supervisor
169 changes: 73 additions & 96 deletions cerror/cerror.go
Original file line number Diff line number Diff line change
@@ -3,73 +3,60 @@ package cerror
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"strconv"
"strings"

"github.com/chenjie199234/Corelib/util/common"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)

//if error was not in this error's format,code will return -1,msg will use the origin error.Error()

func MakeError(code, httpcode int32, msg string) *Error {
return &Error{Code: code, Httpcode: httpcode, Msg: msg}
}
func GetCodeFromErrorstr(e string) int32 {
ee := ConvertErrorstr(e)
if ee == nil {
return 0
func MakeCError(code int32, httpcode int32, msg string) *Error {
if code == 0 {
panic("error code can't be 0")
}
return ee.Code
}
func GetCodeFromStdError(e error) int32 {
ee := ConvertStdError(e)
if ee == nil {
return 0
if httpcode < 400 {
panic("error's http code must >= 400")
}
return ee.Code
}
func GetHttpcodeFromErrorstr(e string) int32 {
ee := ConvertErrorstr(e)
if ee == nil {
return http.StatusOK
if http.StatusText(int(httpcode)) == "" {
panic("error's http code unknown")
}
return ee.Httpcode
}
func GetHttpcodeFromStdError(e error) int32 {
ee := ConvertStdError(e)
if ee == nil {
return http.StatusOK
return &Error{
Code: code,
Httpcode: httpcode,
Msg: msg,
}
return ee.Httpcode
}
func GetMsgFromErrorstr(e string) string {
ee := ConvertErrorstr(e)
if ee == nil {
return ""
}
return ee.Msg
func (this *Error) Error() string {
return "code=" + strconv.FormatInt(int64(this.Code), 10) + ",msg=" + this.Msg
}
func GetMsgFromStdError(e error) string {
ee := ConvertStdError(e)
if ee == nil {
return ""
}
return ee.Msg
func (this *Error) Json() string {
d, _ := json.Marshal(this.Msg)
return "{\"code\":" + strconv.FormatInt(int64(this.Code), 10) + ",\"msg\":" + common.BTS(d) + "}"
}
func ConvertErrorstr(e string) *Error {
if e == "" {
return nil
}
if e == ErrDeadlineExceeded.Error() {
return ErrDeadlineExceeded
} else if e == ErrCanceled.Error() {
return ErrCanceled
func (this *Error) GRPCStatus() *status.Status {
return status.New(codes.Code(this.Httpcode), this.Error())
}
func (this *Error) SlogAttr() *slog.Attr {
return &slog.Attr{Key: "error", Value: slog.GroupValue(slog.Int64("code", int64(this.Code)), slog.String("msg", this.Msg))}
}
func (this *Error) SetHttpcode(httpcode int32) {
this.Httpcode = httpcode
}
func Equal(a, b error) bool {
aa := Convert(a)
bb := Convert(b)
if aa == nil && bb == nil {
return true
} else if (aa == nil && bb != nil) || (aa != nil && bb == nil) {
return false
}
return transStdErrorStr(e)
return aa.Code == bb.Code && aa.Msg == bb.Msg
}
func ConvertStdError(e error) *Error {
func Convert(e error) *Error {
if e == nil {
return nil
}
@@ -82,57 +69,47 @@ func ConvertStdError(e error) *Error {
if ok {
return result
}
return transStdErrorStr(e.Error())
return MakeCError(-1, 500, e.Error())
}
func transStdErrorStr(e string) *Error {
result := &Error{}
if e[0] == '{' && e[len(e)-1] == '}' {
func Decode(estr string) *Error {
if estr == "" {
return nil
}
if estr == ErrDeadlineExceeded.Json() || estr == ErrDeadlineExceeded.Error() {
return ErrDeadlineExceeded
} else if estr == ErrCanceled.Json() || estr == ErrCanceled.Error() {
return ErrCanceled
}
if estr[0] == '{' && estr[len(estr)-1] == '}' {
//json format
if ee := json.Unmarshal(common.STB(e), result); ee != nil || result.Code == 0 {
result.Code = -1
result.Httpcode = http.StatusInternalServerError
result.Msg = e
} else if result.Httpcode == 0 {
result.Httpcode = http.StatusInternalServerError
tmp := &Error{}
//protojson can support "number string" or "number" for field:code
if e := (protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}).Unmarshal(common.STB(estr), tmp); e != nil {
return MakeCError(-1, 500, estr)
}
if tmp.Code == 0 {
return nil
}
if tmp.Httpcode == 0 {
tmp.Httpcode = 500
}
return tmp
} else {
//text format
result.Code = -1
result.Httpcode = http.StatusInternalServerError
result.Msg = e
}
return result
}
func Equal(a, b error) bool {
aa := ConvertStdError(a)
bb := ConvertStdError(b)
if aa == nil && bb == nil {
return true
} else if (aa == nil && bb != nil) || (aa != nil && bb == nil) {
return false
}
return aa.Code == bb.Code && aa.Msg == bb.Msg
}
func (this *Error) Error() string {
if this == nil {
return ""
}
special := false
for _, v := range this.Msg {
if v == '\\' || v == '"' {
special = true
break
index := strings.Index(estr, ",")
if index == -1 {
return MakeCError(-1, 500, estr)
}
p1 := estr[:index]
p2 := estr[index+1:]
if !strings.HasPrefix(p1, "code=") || !strings.HasPrefix(p2, "msg=") {
return MakeCError(-1, 500, estr)
}
code, e := strconv.ParseInt(p1[5:], 10, 32)
if e != nil {
return MakeCError(-1, 500, estr)
}
msg := p2[4:]
return MakeCError(int32(code), 500, msg)
}
if special {
d, _ := json.Marshal(this.Msg)
return "{\"code\":" + strconv.FormatInt(int64(this.Code), 10) + ",\"msg\":" + common.BTS(d) + "}"
}
return "{\"code\":" + strconv.FormatInt(int64(this.Code), 10) + ",\"msg\":\"" + this.Msg + "\"}"
}
func (this *Error) GRPCStatus() *status.Status {
return status.New(codes.Code(this.Httpcode), this.Error())
}
func (this *Error) SetHttpcode(httpcode int32) {
this.Httpcode = httpcode
}
Loading