Skip to content

Commit 07c3475

Browse files
committed
fix: add client error unwrap
1 parent f8e193b commit 07c3475

File tree

11 files changed

+132
-4
lines changed

11 files changed

+132
-4
lines changed

clienterror.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ func NewClientError(err error) *ClientError {
99
error: err,
1010
}
1111
}
12+
13+
// Unwrap interface
14+
func (e *ClientError) Unwrap() error {
15+
if e != nil && e.error != nil {
16+
return e.error
17+
}
18+
return nil
19+
}

example/basic/client/src/service-client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export class ServiceClient {
1616
async boolSlice(v:Array<boolean>|null):Promise<Array<boolean>|null> {
1717
return (await this.transport<{0:Array<boolean>|null}>("BoolSlice", [v]))[0]
1818
}
19+
async context():Promise<void> {
20+
await this.transport<void>("Context", [])
21+
}
1922
async empty():Promise<void> {
2023
await this.transport<void>("Empty", [])
2124
}

example/basic/main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main_test
2+
3+
import (
4+
"context"
5+
"net/http/httptest"
6+
"testing"
7+
"time"
8+
9+
"github.com/foomo/gotsrpc/v2/example/basic/service"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestContextCanceled(t *testing.T) {
14+
ctx, cancel := context.WithCancel(context.TODO())
15+
go func() {
16+
time.Sleep(time.Second)
17+
cancel()
18+
}()
19+
20+
svr := httptest.NewServer(service.NewDefaultServiceGoTSRPCProxy(&service.Handler{}))
21+
defer svr.Close()
22+
23+
client := service.NewDefaultServiceGoTSRPCClient(svr.URL)
24+
25+
clientErr := client.Context(ctx)
26+
assert.ErrorIs(t, clientErr, context.Canceled)
27+
}

example/basic/service/gorpc_gen.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/basic/service/gorpcclient_gen.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/basic/service/gotsrpc_gen.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/basic/service/gotsrpcclient_gen.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/basic/service/handler.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package service
22

3+
import (
4+
"net/http"
5+
"time"
6+
)
7+
38
type Handler struct{}
49

5-
func (h *Handler) Empty() {
10+
func (h *Handler) Context(w http.ResponseWriter, r *http.Request) {
11+
for r.Context().Err() == nil {
12+
time.Sleep(100 * time.Millisecond)
13+
}
614
}
715

16+
func (h *Handler) Empty() {}
17+
818
func (h *Handler) Bool(v bool) bool {
919
return v
1020
}

example/basic/service/service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package service
22

3+
import (
4+
"net/http"
5+
)
6+
37
type Service interface {
8+
Context(w http.ResponseWriter, r *http.Request)
49
Empty()
510
Bool(v bool) bool
611
BoolPtr(v bool) *bool

example/errors/handler/frontend/handler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package frontend
22

33
import (
4+
"fmt"
45
"net/http"
6+
"time"
57

68
"github.com/foomo/gotsrpc/v2/example/errors/service/backend"
79
"github.com/foomo/gotsrpc/v2/example/errors/service/frontend"
@@ -18,6 +20,12 @@ func New(client backend.ServiceGoTSRPCClient) *Handler {
1820
}
1921

2022
func (h *Handler) Simple(w http.ResponseWriter, r *http.Request) (e *frontend.ErrSimple) {
23+
fmt.Println("==========> incoming")
24+
time.Sleep(time.Second)
25+
if r.Context().Err() != nil {
26+
fmt.Println("==========>" + r.Context().Err().Error())
27+
}
28+
fmt.Println("<========== outgoing")
2129
return
2230
}
2331

0 commit comments

Comments
 (0)