From a155ac40cc852a7fc5d0f6ab7e06210717c39145 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 7 Feb 2022 13:23:05 -0500 Subject: [PATCH] - fixes go error deserialization - moves parsableFactory to serialization package Signed-off-by: Vincent Biret --- abstractions/go/request_adapter.go | 9 +++---- abstractions/go/serialization/parsable.go | 3 +++ http/go/nethttp/nethttp_request_adapter.go | 28 ++++++++++++++++------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/abstractions/go/request_adapter.go b/abstractions/go/request_adapter.go index 6a57c58494..7d75e5b799 100644 --- a/abstractions/go/request_adapter.go +++ b/abstractions/go/request_adapter.go @@ -12,18 +12,15 @@ import ( s "github.com/microsoft/kiota/abstractions/go/serialization" ) -// ParsableFactory is a factory for creating Parsable. -type ParsableFactory func() s.Parsable - // ErrorMappings is a mapping of status codes to error types factories. -type ErrorMappings map[string]ParsableFactory +type ErrorMappings map[string]s.ParsableFactory // RequestAdapter is the service responsible for translating abstract RequestInformation into native HTTP requests. type RequestAdapter interface { // SendAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model. - SendAsync(requestInfo RequestInformation, constructor ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) (s.Parsable, error) + SendAsync(requestInfo RequestInformation, constructor s.ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) (s.Parsable, error) // SendCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection. - SendCollectionAsync(requestInfo RequestInformation, constructor ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) ([]s.Parsable, error) + SendCollectionAsync(requestInfo RequestInformation, constructor s.ParsableFactory, responseHandler ResponseHandler, errorMappings ErrorMappings) ([]s.Parsable, error) // SendPrimitiveAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model. SendPrimitiveAsync(requestInfo RequestInformation, typeName string, responseHandler ResponseHandler, errorMappings ErrorMappings) (interface{}, error) // SendPrimitiveCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model collection. diff --git a/abstractions/go/serialization/parsable.go b/abstractions/go/serialization/parsable.go index 320873c453..3a4ca7696f 100644 --- a/abstractions/go/serialization/parsable.go +++ b/abstractions/go/serialization/parsable.go @@ -13,3 +13,6 @@ type Parsable interface { // IsNil returns whether the current object is nil or not. IsNil() bool } + +// ParsableFactory is a factory for creating Parsable. +type ParsableFactory func() Parsable diff --git a/http/go/nethttp/nethttp_request_adapter.go b/http/go/nethttp/nethttp_request_adapter.go index 416088bea8..cb6c731258 100644 --- a/http/go/nethttp/nethttp_request_adapter.go +++ b/http/go/nethttp/nethttp_request_adapter.go @@ -139,7 +139,7 @@ func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(requestInfo abs } // SendAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model. -func (a *NetHttpRequestAdapter) SendAsync(requestInfo abs.RequestInformation, constructor abs.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) (absser.Parsable, error) { +func (a *NetHttpRequestAdapter) SendAsync(requestInfo abs.RequestInformation, constructor absser.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) (absser.Parsable, error) { response, err := a.getHttpResponseMessage(requestInfo) if err != nil { return nil, err @@ -167,7 +167,7 @@ func (a *NetHttpRequestAdapter) SendAsync(requestInfo abs.RequestInformation, co } // SendCollectionAsync executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection. -func (a *NetHttpRequestAdapter) SendCollectionAsync(requestInfo abs.RequestInformation, constructor abs.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) ([]absser.Parsable, error) { +func (a *NetHttpRequestAdapter) SendCollectionAsync(requestInfo abs.RequestInformation, constructor absser.ParsableFactory, responseHandler abs.ResponseHandler, errorMappings abs.ErrorMappings) ([]absser.Parsable, error) { response, err := a.getHttpResponseMessage(requestInfo) if err != nil { return nil, err @@ -300,18 +300,32 @@ func (a *NetHttpRequestAdapter) throwFailedResponses(response *nethttp.Response, } statusAsString := strconv.Itoa(response.StatusCode) + var errorCtor absser.ParsableFactory = nil if len(errorMappings) != 0 { if errorMappings[statusAsString] != nil { - return (errorMappings[statusAsString]()).(error) + errorCtor = errorMappings[statusAsString] } else if response.StatusCode >= 400 && response.StatusCode < 500 && errorMappings["4XX"] != nil { - return (errorMappings["4XX"]()).(error) + errorCtor = errorMappings["4XX"] } else if response.StatusCode >= 500 && response.StatusCode < 600 && errorMappings["5XX"] != nil { - return (errorMappings["5XX"]()).(error) + errorCtor = errorMappings["5XX"] } } - return &abs.ApiError{ - Message: "The server returned an unexpected status code and no error factory is registered for this code: " + statusAsString, + if errorCtor == nil { + return &abs.ApiError{ + Message: "The server returned an unexpected status code and no error factory is registered for this code: " + statusAsString, + } + } + + rootNode, err := a.getRootParseNode(response) + if err != nil { + return err + } + + errValue, err := rootNode.GetObjectValue(errorCtor) + if err != nil { + return err } + return errValue.(error) }