billglover / starling

A Go client for the Starling Bank API
MIT License
6 stars 6 forks source link

AddMoney panics on a bad request #2

Closed billglover closed 6 years ago

billglover commented 6 years ago

If the server responds with a bad request, the following code will result in a panic as it tries to access UID on a nilb savingsGoalTransferResponse.

    var tuResp *savingsGoalTransferResponse
    resp, err := c.Do(ctx, req, &tuResp)
    if err != nil {
        return tuResp.UID, resp, err
    }
    return tuResp.UID, resp, nil
billglover commented 6 years ago

If we return an empty string inside the conditional we prevent the panic, but reveal the fact that the client isn't able to parse the error response from the server.

    var tuResp *savingsGoalTransferResponse
    resp, err := c.Do(ctx, req, &tuResp)
    if err != nil {
        return "", resp, err
    }
    return tuResp.UID, resp, nil

Results in the following response:

API returned an error but client was unable to parse the detail: json: cannot unmarshal array into Go value of type starling.ErrorDetail