aboutsummaryrefslogtreecommitdiff
path: root/wrapper.go
diff options
context:
space:
mode:
Diffstat (limited to 'wrapper.go')
-rw-r--r--wrapper.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/wrapper.go b/wrapper.go
new file mode 100644
index 0000000..8058a27
--- /dev/null
+++ b/wrapper.go
@@ -0,0 +1,25 @@
+package jsonrpc2
+
+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)