microsoftgraph / msgraph-sdk-go-core

Microsoft Graph SDK for Go - Core Library
https://learn.microsoft.com/graph/sdks/sdks-overview
MIT License
20 stars 10 forks source link

GetResponseById doesn't deserialize errors #247

Closed eric-millin closed 10 months ago

eric-millin commented 1 year ago

I'm trying to get the error message from the body of a batch response item. The code has a path for doing so, but it fails in this block: https://github.com/microsoftgraph/msgraph-sdk-go-core/blob/main/batch_requests.go#L208-L220.

Is this a bug or am I missing something? You can put the following snippet in your tests to repro.


func TestGetErrorResponseBodyById(t *testing.T) {
    var blob = `{
        "responses": [{
            "id": "3",
            "status": 400,
            "headers": {
                "Cache-Control": "no-cache",
                "Content-Type": "application/json"
            },
            "body": {
                "error": {
                    "code": "ExtensionError",
                    "message": "Operation: Create; Exception: [Status Code: BadRequest; Reason: One of the provided arguments is not acceptable.]",
                    "innerError": {
                        "date": "2023-11-21T22:48:33",
                        "request-id": "0cd2",
                        "client-request-id": "91d9"
                    }
                }
            }
        }]
    }`

       // uncomment to get past error "gob: type not registered for interface: map[string]interface {}"
       // gob.Register(map[string]interface{}{})

    errorMapping := abstractions.ErrorMappings{
        "4XX": internal.CreateSampleErrorFromDiscriminatorValue,
        "5XX": internal.CreateSampleErrorFromDiscriminatorValue,
    }
    err := RegisterError(BatchRequestErrorRegistryKey, errorMapping)
    require.NoError(t, err)

    mockServer := makeMockRequest(200, blob)
    defer mockServer.Close()

    mockPath := mockServer.URL + "/$batch"
    reqAdapter.SetBaseUrl(mockPath)

    reqInfo := getRequestInfo()
    batch := NewBatchRequest(reqAdapter)
    _, err = batch.AddBatchRequestStep(*reqInfo)
    require.NoError(t, err)

    resp, err := batch.Send(context.Background(), reqAdapter)
    require.NoError(t, err)

       // Set the default deserializer to JSON
       // if you've uncommented the gob::Register call above, you'll get a deserialization error because it doesn't encode the body as json
    _, err = GetBatchResponseById[Userable](resp, "3", CreateUser)
    assert.Equal(t, "Some ODataError message", err.Error())
}