Skip to content

Commit

Permalink
- fixes go error deserialization
Browse files Browse the repository at this point in the history
- moves parsableFactory to serialization package

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Feb 7, 2022
1 parent 6f9fc7f commit a155ac4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
9 changes: 3 additions & 6 deletions abstractions/go/request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions abstractions/go/serialization/parsable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 21 additions & 7 deletions http/go/nethttp/nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit a155ac4

Please sign in to comment.