From 764a6ea643309aef76dc2072134cc2f417227bf8 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:11:06 +0300 Subject: [PATCH 1/2] Adds support for XX status code --- CHANGELOG.md | 6 +++++ internal/mock_parse_node_factory.go | 6 +++++ nethttp_request_adapter.go | 4 +++- nethttp_request_adapter_test.go | 35 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e32faf..7c1ccaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.2.0] - 2024-01-22 + +### Added + +- Adds support for XXX status code. + ## [1.1.1] - 2023-11-22 ### Added diff --git a/internal/mock_parse_node_factory.go b/internal/mock_parse_node_factory.go index 20a083e..15503bd 100644 --- a/internal/mock_parse_node_factory.go +++ b/internal/mock_parse_node_factory.go @@ -57,6 +57,12 @@ func (e *MockParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([] return nil, nil } func (e *MockParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Parsable, error) { + if ctor != nil { + _, err := ctor(e) + if err != nil { + return nil, err + } + } return &MockEntity{}, nil } func (e *MockParseNode) GetStringValue() (*string, error) { diff --git a/nethttp_request_adapter.go b/nethttp_request_adapter.go index 7896394..939480c 100644 --- a/nethttp_request_adapter.go +++ b/nethttp_request_adapter.go @@ -702,7 +702,7 @@ func (a *NetHttpRequestAdapter) SendNoContent(ctx context.Context, requestInfo * func (a *NetHttpRequestAdapter) getRootParseNode(ctx context.Context, response *nethttp.Response, spanForAttributes trace.Span) (absser.ParseNode, context.Context, error) { ctx, span := otel.GetTracerProvider().Tracer(a.observabilityOptions.GetTracerInstrumentationName()).Start(ctx, "getRootParseNode") defer span.End() - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { spanForAttributes.RecordError(err) return nil, ctx, err @@ -754,6 +754,8 @@ func (a *NetHttpRequestAdapter) throwIfFailedResponse(ctx context.Context, respo if len(errorMappings) != 0 { if errorMappings[statusAsString] != nil { errorCtor = errorMappings[statusAsString] + } else if errorMappings["XXX"] != nil && response.StatusCode >= 400 && response.StatusCode < 600 { + errorCtor = errorMappings["XXX"] } else if response.StatusCode >= 400 && response.StatusCode < 500 && errorMappings["4XX"] != nil { errorCtor = errorMappings["4XX"] } else if response.StatusCode >= 500 && response.StatusCode < 600 && errorMappings["5XX"] != nil { diff --git a/nethttp_request_adapter_test.go b/nethttp_request_adapter_test.go index 47cd4c5..5ac0215 100644 --- a/nethttp_request_adapter_test.go +++ b/nethttp_request_adapter_test.go @@ -2,6 +2,7 @@ package nethttplibrary import ( "context" + "github.com/microsoft/kiota-abstractions-go/serialization" nethttp "net/http" httptest "net/http/httptest" "net/url" @@ -75,6 +76,40 @@ func TestItThrowsApiError(t *testing.T) { assert.Equal(t, "example-guid", apiError.ResponseHeaders.Get("client-request-id")[0]) } +func TestGenericError(t *testing.T) { + testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) { + res.WriteHeader(500) + res.Write([]byte("test")) + })) + defer func() { testServer.Close() }() + authProvider := &absauth.AnonymousAuthenticationProvider{} + adapter, err := NewNetHttpRequestAdapterWithParseNodeFactory(authProvider, &internal.MockParseNodeFactory{}) + assert.Nil(t, err) + assert.NotNil(t, adapter) + + uri, err := url.Parse(testServer.URL) + assert.Nil(t, err) + assert.NotNil(t, uri) + request := abs.NewRequestInformation() + request.SetUri(*uri) + request.Method = abs.GET + + result := 0 + errorMapping := abs.ErrorMappings{ + "XXX": func(parseNode serialization.ParseNode) (serialization.Parsable, error) { + result++ + return nil, &abs.ApiError{ + Message: "test XXX message", + } + }, + } + + _, err2 := adapter.SendPrimitive(context.TODO(), request, "[]byte", errorMapping) + assert.NotNil(t, err2) + assert.Equal(t, 1, result) + assert.Equal(t, "test XXX message", err2.Error()) +} + func TestImplementationHonoursInterface(t *testing.T) { authProvider := &absauth.AnonymousAuthenticationProvider{} adapter, err := NewNetHttpRequestAdapter(authProvider) From 1b3943169fed6bb70e38540f74cdfa576fcb248f Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:28:08 +0300 Subject: [PATCH 2/2] Change order of requests --- nethttp_request_adapter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nethttp_request_adapter.go b/nethttp_request_adapter.go index 939480c..03205ab 100644 --- a/nethttp_request_adapter.go +++ b/nethttp_request_adapter.go @@ -754,12 +754,12 @@ func (a *NetHttpRequestAdapter) throwIfFailedResponse(ctx context.Context, respo if len(errorMappings) != 0 { if errorMappings[statusAsString] != nil { errorCtor = errorMappings[statusAsString] - } else if errorMappings["XXX"] != nil && response.StatusCode >= 400 && response.StatusCode < 600 { - errorCtor = errorMappings["XXX"] } else if response.StatusCode >= 400 && response.StatusCode < 500 && errorMappings["4XX"] != nil { errorCtor = errorMappings["4XX"] } else if response.StatusCode >= 500 && response.StatusCode < 600 && errorMappings["5XX"] != nil { errorCtor = errorMappings["5XX"] + } else if errorMappings["XXX"] != nil && response.StatusCode >= 400 && response.StatusCode < 600 { + errorCtor = errorMappings["XXX"] } }