diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ecc70..b46f3d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,12 +11,17 @@ 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.2] - 2024-01-20 ### Changed -- Changed the code by replacing ioutil.ReadAll and ioutil.NopCloser with io.ReadAll and io.NopCloser, respectively, due to their deprecation. - +- Changed the code by replacing ioutil.ReadAll and ioutil.NopCloser with io.ReadAll and io.NopCloser, respectively, due to their deprecation. ## [1.1.1] - 2023-11-22 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 6955b6c..a4dcc58 100644 --- a/nethttp_request_adapter.go +++ b/nethttp_request_adapter.go @@ -757,6 +757,8 @@ func (a *NetHttpRequestAdapter) throwIfFailedResponse(ctx context.Context, respo 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"] } } 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)