Skip to content

Commit

Permalink
Improve error handling when resolving codec
Browse files Browse the repository at this point in the history
And remove the go.work files that masked a faulty test setup.

This is replaced with replace statements in the examples, which is OK as those are only used in
tests/as examples.
  • Loading branch information
bep committed Aug 15, 2022
1 parent e69c95e commit 72c8dc1
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 39 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ client, err := execrpc.StartClient(
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Cmd: "go",
Args: []string{"run", "./examples/servers/typed"},
Dir: "./examples/servers/typed"
Args: []string{"run", "."},
},
Codec: codecs.JSONCodec[model.ExampleRequest, model.ExampleResponse]{},
},
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func StartClientRaw(opts ClientRawOptions) (*ClientRaw, error) {
}

if err := conn.Start(); err != nil {
return nil, err
return nil, fmt.Errorf("failed start server: %s: %s", err, conn.stdErr.String())
}

if opts.OnMessage == nil {
Expand Down
12 changes: 8 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func TestExecRaw(t *testing.T) {
execrpc.ClientRawOptions{
Version: 1,
Cmd: "go",
Args: []string{"run", "./examples/servers/raw"},
Dir: "./examples/servers/raw",
Args: []string{"run", "."},
})

c.Assert(err, qt.IsNil)
Expand All @@ -41,7 +42,8 @@ func newTestClient(t testing.TB, codec codecs.Codec[model.ExampleRequest, model.
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Cmd: "go",
Args: []string{"run", "./examples/servers/typed"},
Dir: "./examples/servers/typed",
Args: []string{"run", "."},
Env: env,
},
Codec: codec,
Expand All @@ -62,7 +64,8 @@ func TestExecTyped(t *testing.T) {
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Cmd: "go",
Args: []string{"run", "./examples/servers/typed"},
Dir: "./examples/servers/typed",
Args: []string{"run", "."},
Env: env,
Timeout: 4 * time.Second,
},
Expand Down Expand Up @@ -108,7 +111,8 @@ func TestExecTyped(t *testing.T) {
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Cmd: "go",
Args: []string{"run", "./examples/servers/typed"},
Dir: "./examples/servers/typed",
Args: []string{"run", "."},
Env: []string{"EXECRPC_SEND_TWO_LOG_MESSAGES=true"},
Timeout: 4 * time.Second,
OnMessage: func(msg execrpc.Message) {
Expand Down
10 changes: 9 additions & 1 deletion examples/servers/raw/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/bep/execrpc/examples/servers/raw

go 1.19

require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
require github.com/bep/execrpc v0.3.0

require (
github.com/bep/helpers v0.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
)

replace github.com/bep/execrpc => ../../..
21 changes: 21 additions & 0 deletions examples/servers/raw/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
github.com/bep/execrpc v0.3.0 h1:cZCobakmpgFSWRtJBmOmksSirSQZzz3m3+Ojc0YroGs=
github.com/bep/execrpc v0.3.0/go.mod h1:wDgrl/LwaUp6+EFCx0w1uvqP1cbwln/4OhB608TIREk=
github.com/bep/helpers v0.1.0 h1:HFLG+W6axHackmKMk0houEnz9G2aiBrDMZyOvL9J0WM=
github.com/bep/helpers v0.1.0/go.mod h1:/QpHdmcPagDw7+RjkLFCvnlUc8lQ5kg4KDrEkb2Yyco=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 8 additions & 1 deletion examples/servers/typed/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ module github.com/bep/execrpc/examples/servers/typed

go 1.19

require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
require (
github.com/bep/execrpc v0.3.0 // indirect
github.com/bep/helpers v0.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
)

replace github.com/bep/execrpc => ../../..
12 changes: 12 additions & 0 deletions examples/servers/typed/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
github.com/bep/execrpc v0.3.0 h1:cZCobakmpgFSWRtJBmOmksSirSQZzz3m3+Ojc0YroGs=
github.com/bep/execrpc v0.3.0/go.mod h1:wDgrl/LwaUp6+EFCx0w1uvqP1cbwln/4OhB608TIREk=
github.com/bep/helpers v0.1.0 h1:HFLG+W6axHackmKMk0houEnz9G2aiBrDMZyOvL9J0WM=
github.com/bep/helpers v0.1.0/go.mod h1:/QpHdmcPagDw7+RjkLFCvnlUc8lQ5kg4KDrEkb2Yyco=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module github.com/bep/execrpc
go 1.18

require (
github.com/bep/helpers v0.1.0 // indirect
github.com/frankban/quicktest v1.14.3 // indirect
github.com/bep/helpers v0.1.0
github.com/frankban/quicktest v1.14.3
github.com/pelletier/go-toml/v2 v2.0.2
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
)

require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
)
14 changes: 9 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/bep/helpers v0.1.0 h1:HFLG+W6axHackmKMk0houEnz9G2aiBrDMZyOvL9J0WM=
github.com/bep/helpers v0.1.0/go.mod h1:/QpHdmcPagDw7+RjkLFCvnlUc8lQ5kg4KDrEkb2Yyco=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.2 h1:SPb1KFFmM+ybpEjPUhCCkZOM5xlovT5UbrMvWnXyBns=
github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
Expand All @@ -18,15 +18,19 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 0 additions & 7 deletions go.work

This file was deleted.

8 changes: 0 additions & 8 deletions go.work.sum

This file was deleted.

13 changes: 6 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package execrpc

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -42,13 +41,13 @@ func NewServer[Q, R any](opts ServerOptions[Q, R]) (*Server[Q, R], error) {
if opts.Call == nil {
return nil, fmt.Errorf("opts: Call function is required")
}

if opts.Codec == nil {
if opts.Codec == nil {
var err error
opts.Codec, err = codecs.ForName[R, Q](os.Getenv(envClientCodec))
if err != nil {
return nil, errors.New("opts: Codec is required")
}
codecName := os.Getenv(envClientCodec)
var err error
opts.Codec, err = codecs.ForName[R, Q](codecName)
if err != nil {
return nil, fmt.Errorf("failed to resolve codec from env variable %s with value %q (set by client); it can optionally be set in ServerOptions", envClientCodec, codecName)
}
}

Expand Down

0 comments on commit 72c8dc1

Please sign in to comment.