Skip to content

Commit

Permalink
Update error propagation mechanism, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed Aug 23, 2021
1 parent 932be3b commit 5efa8e4
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 8 deletions.
19 changes: 14 additions & 5 deletions clients/RestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
crefer "github.com/pip-services3-go/pip-services3-commons-go/refer"
ccount "github.com/pip-services3-go/pip-services3-components-go/count"
ctrace "github.com/pip-services3-go/pip-services3-components-go/trace"
clog "github.com/pip-services3-go/pip-services3-components-go/log"
ctrace "github.com/pip-services3-go/pip-services3-components-go/trace"
rpccon "github.com/pip-services3-go/pip-services3-rpc-go/connect"
service "github.com/pip-services3-go/pip-services3-rpc-go/services"
)
Expand Down Expand Up @@ -88,7 +88,7 @@ type RestClient struct {
//The performance counters.
Counters *ccount.CompositeCounters
// The tracer.
Tracer *ctrace.CompositeTracer
Tracer *ctrace.CompositeTracer
//The configuration options.
Options cconf.ConfigParams
//The base route.
Expand Down Expand Up @@ -169,9 +169,9 @@ func (c *RestClient) Instrument(correlationId string, name string) *service.Inst
c.Logger.Trace(correlationId, "Calling %s method", name)
c.Counters.IncrementOne(name + ".call_count")
counterTiming := c.Counters.BeginTiming(name + ".call_time")
traceTiming := c.Tracer.BeginTrace(correlationId, name, "")
return service.NewInstrumentTiming(correlationId, name, "call",
c.Logger, c.Counters, counterTiming, traceTiming)
traceTiming := c.Tracer.BeginTrace(correlationId, name, "")
return service.NewInstrumentTiming(correlationId, name, "call",
c.Logger, c.Counters, counterTiming, traceTiming)
}

// InstrumentError method are dds instrumentation to error handling.
Expand Down Expand Up @@ -417,6 +417,15 @@ func (c *RestClient) Call(prototype reflect.Type, method string, route string, c
if resp.StatusCode >= 400 {
appErr := cerr.ApplicationError{}
json.Unmarshal(r, &appErr)
if appErr.Status == 0 && len(r) > 0 { // not standart Pip.Services error
values := make(map[string]interface{})
decodeErr := json.Unmarshal(r, &values)
if decodeErr != nil { // not json response
appErr.Message = (string)(r)
}
appErr.Details = values
}
appErr.Status = resp.StatusCode
return nil, &appErr
}

Expand Down
9 changes: 6 additions & 3 deletions services/HttpResponseSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ type THttpResponseSender struct {
// - err error an error object to be sent.
func (c *THttpResponseSender) SendError(res http.ResponseWriter, req *http.Request, err error) {

appErr := cerr.ApplicationError{}
appErr := cerr.ApplicationError{
Status: 500,
}
appErr = *appErr.Wrap(err)
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(500)
jsonObj, jsonErr := json.Marshal(appErr.Wrap(err))
res.WriteHeader(appErr.Status)
jsonObj, jsonErr := json.Marshal(appErr)
if jsonErr == nil {
io.WriteString(res, (string)(jsonObj))
}
Expand Down
12 changes: 12 additions & 0 deletions test/clients/DummyClientFixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

cdata "github.com/pip-services3-go/pip-services3-commons-go/data"
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
tdata "github.com/pip-services3-go/pip-services3-rpc-go/test/data"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -61,11 +62,22 @@ func (c *DummyClientFixture) TestCrudOperations(t *testing.T) {
assert.Nil(t, err)
assert.Nil(t, dummy)

// Check correlation id propagation
values, err := c.client.CheckCorrelationId("test_cor_id")
assert.Nil(t, err)
assert.Equal(t, values["correlationId"], "test_cor_id")

values, err = c.client.CheckCorrelationId("test cor id")
assert.Nil(t, err)
assert.Equal(t, values["correlationId"], "test cor id")

// Check error propagation
err = c.client.CheckErrorPropagation("test_error_propagation")
appErr, ok := err.(*cerr.ApplicationError)

assert.True(t, ok)
assert.Equal(t, appErr.CorrelationId, "test_error_propagation")
assert.Equal(t, appErr.Status, 404)
assert.Equal(t, appErr.Code, "NOT_FOUND_TEST")
assert.Equal(t, appErr.Message, "Not found error")
}
6 changes: 6 additions & 0 deletions test/clients/DummyCommandableHttpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ func (c *DummyCommandableHttpClient) CheckCorrelationId(correlationId string) (r
val, _ := calValue.(*(map[string]string))
return *val, err
}

func (c *DummyCommandableHttpClient) CheckErrorPropagation(correlationId string) error {
params := cdata.NewEmptyAnyValueMap()
_, calErr := c.CallCommand(nil, "check_error_propagation", correlationId, params)
return calErr
}
7 changes: 7 additions & 0 deletions test/clients/DummyRestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,10 @@ func (c *DummyRestClient) CheckCorrelationId(correlationId string) (result map[s
c.Instrument(correlationId, "dummy.check_correlation_id")
return *val, nil
}

func (c *DummyRestClient) CheckErrorPropagation(correlationId string) error {

_, calErr := c.Call(nil, "get", "/dummies/check/error_propagation", correlationId, nil, nil)
c.Instrument(correlationId, "dummy.check_error_propagation")
return calErr
}
2 changes: 2 additions & 0 deletions test/clients/IDummyClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ type IDummyClient interface {
DeleteDummy(correlationId string, dummyId string) (result *tdata.Dummy, err error)

CheckCorrelationId(correlationId string) (result map[string]string, err error)

CheckErrorPropagation(correlationId string) error
}
11 changes: 11 additions & 0 deletions test/logic/DummyCommandSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewDummyCommandSet(controller IDummyController) *DummyCommandSet {
c.AddCommand(c.makeUpdateCommand())
c.AddCommand(c.makeDeleteByIdCommand())
c.AddCommand(c.makeCheckCorrelationIdCommand())
c.AddCommand(c.makeCheckErrorPropagationCommand())
return &c
}

Expand Down Expand Up @@ -101,3 +102,13 @@ func (c *DummyCommandSet) makeCheckCorrelationIdCommand() ccomand.ICommand {
},
)
}

func (c *DummyCommandSet) makeCheckErrorPropagationCommand() ccomand.ICommand {
return ccomand.NewCommand(
"check_error_propagation",
cvalid.NewObjectSchema(),
func(correlationId string, args *crun.Parameters) (result interface{}, err error) {
return nil, c.controller.CheckErrorPropagation(correlationId)
},
)
}
5 changes: 5 additions & 0 deletions test/logic/DummyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test_logic
import (
ccomand "github.com/pip-services3-go/pip-services3-commons-go/commands"
cdata "github.com/pip-services3-go/pip-services3-commons-go/data"
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
tdata "github.com/pip-services3-go/pip-services3-rpc-go/test/data"
)

Expand Down Expand Up @@ -114,3 +115,7 @@ func (c *DummyController) CheckCorrelationId(correlationId string) (result map[s
result = map[string]string{"correlationId": correlationId}
return result, nil
}

func (c *DummyController) CheckErrorPropagation(correlationId string) error {
return cerr.NewNotFoundError(correlationId, "NOT_FOUND_TEST", "Not found error")
}
2 changes: 2 additions & 0 deletions test/logic/IDummyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ type IDummyController interface {
DeleteById(correlationId string, id string) (result *tdata.Dummy, err error)

CheckCorrelationId(correlationId string) (result map[string]string, err error)

CheckErrorPropagation(correlationId string) error
}
19 changes: 19 additions & 0 deletions test/services/DummyCommandableHttpService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
tdata "github.com/pip-services3-go/pip-services3-rpc-go/test/data"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -153,6 +154,24 @@ func TestDummyCommandableHttpService(t *testing.T) {
assert.NotNil(t, values)
assert.Equal(t, values["correlationId"], "test_cor_id")

// Testing error propagation
getResponse, getErr = http.Post(url+"/dummies/check_error_propagation?correlation_id=test_error_propagation", "application/json", nil)
assert.Nil(t, getErr)
assert.NotNil(t, getResponse)

resBody, bodyErr = ioutil.ReadAll(getResponse.Body)
assert.Nil(t, bodyErr)
getResponse.Body.Close()

appErr := cerr.ApplicationError{}
jsonErr = json.Unmarshal(resBody, &appErr)
assert.Nil(t, jsonErr)

assert.Equal(t, appErr.CorrelationId, "test_error_propagation")
assert.Equal(t, appErr.Status, 404)
assert.Equal(t, appErr.Code, "NOT_FOUND_TEST")
assert.Equal(t, appErr.Message, "Not found error")

// Get OpenApi Spec From String
// -----------------------------------------------------------------
getResponse, getErr = http.Get(url + "/dummies/swagger")
Expand Down
11 changes: 11 additions & 0 deletions test/services/DummyRestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (c *DummyRestService) checkCorrelationId(res http.ResponseWriter, req *http
c.SendResult(res, req, result, err)
}

func (c *DummyRestService) checkErrorPropagation(res http.ResponseWriter, req *http.Request) {
err := c.controller.CheckErrorPropagation(c.GetCorrelationId(req))
c.SendError(res, req, err)
}

func (c *DummyRestService) Register() {
c.RegisterInterceptor("/dummies", c.incrementNumberOfCalls)

Expand All @@ -181,6 +186,12 @@ func (c *DummyRestService) Register() {
c.checkCorrelationId,
)

c.RegisterRoute(
"get", "/dummies/check/error_propagation",
&cvalid.NewObjectSchema().Schema,
c.checkErrorPropagation,
)

c.RegisterRoute(
"get", "/dummies/{dummy_id}",
&cvalid.NewObjectSchema().
Expand Down
19 changes: 19 additions & 0 deletions test/services/DummyRestService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"testing"

cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
tdata "github.com/pip-services3-go/pip-services3-rpc-go/test/data"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -138,6 +139,24 @@ func TestDummyRestService(t *testing.T) {
assert.NotNil(t, values)
assert.Equal(t, values["correlationId"], "test_cor_id")

// Testing error propagation
getResponse, getErr = http.Get(url + "/dummies/check/error_propagation?correlation_id=test_error_propagation")
assert.Nil(t, getErr)
assert.NotNil(t, getResponse)

resBody, bodyErr = ioutil.ReadAll(getResponse.Body)
assert.Nil(t, bodyErr)
getResponse.Body.Close()

appErr := cerr.ApplicationError{}
jsonErr = json.Unmarshal(resBody, &appErr)
assert.Nil(t, jsonErr)

assert.Equal(t, appErr.CorrelationId, "test_error_propagation")
assert.Equal(t, appErr.Status, 404)
assert.Equal(t, appErr.Code, "NOT_FOUND_TEST")
assert.Equal(t, appErr.Message, "Not found error")

// Get OpenApi Spec From String
// -----------------------------------------------------------------
getResponse, getErr = http.Get(url + "/swagger")
Expand Down

0 comments on commit 5efa8e4

Please sign in to comment.