aboutsummaryrefslogtreecommitdiff
path: root/rpc/wrapper.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-31 20:17:31 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-01-31 20:25:19 +0300
commit4cf58de9bb7b109dddbc80adefe52cdf61328d0d (patch)
treedbc4c46eb64672e077305aebbe694acd1acf4828 /rpc/wrapper.go
parent35fee8155bb6835d372c364ef721077b0dcb9d38 (diff)
Small refactoring
Diffstat (limited to 'rpc/wrapper.go')
-rw-r--r--rpc/wrapper.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/rpc/wrapper.go b/rpc/wrapper.go
new file mode 100644
index 0000000..0bc0a5c
--- /dev/null
+++ b/rpc/wrapper.go
@@ -0,0 +1,25 @@
+package rpc
+
+import (
+ "context"
+ "encoding/json"
+)
+
+func Wrap[RQ any, RS any](handler func(context.Context, *RQ) (RS, error)) Handler {
+ return func(ctx context.Context, in json.RawMessage) (json.RawMessage, error) {
+ req := new(RQ)
+ if err := json.Unmarshal(in, req); err != nil {
+ return nil, NewError(ErrCodeParseError)
+ }
+ resp, err := handler(ctx, req)
+ if err != nil {
+ return nil, Error{
+ Code: ErrUser,
+ Message: err.Error(),
+ }
+ }
+ return json.Marshal(resp)
+ }
+}
+
+type Handler func(context.Context, json.RawMessage) (json.RawMessage, error)