northwesternmutual / grammes

A Go package built to communicate with Apache TinkerPop™ Graph computing framework using Gremlin; a graph traversal language used by graph databases such as JanusGraph®, MS Cosmos DB, AWS Neptune, and DataStax® Enterprise Graph.
Apache License 2.0
125 stars 45 forks source link

MarshalResponse maybe panic #26

Open wen313 opened 4 years ago

wen313 commented 4 years ago
func MarshalResponse(msg []byte) (Response, error) {
    var j map[string]interface{}

    err := jsonUnmarshal(msg, &j)
    if err != nil {
        return Response{}, gremerror.NewUnmarshalError("MarshalResponse", msg, err)
    }

    var (
        status = j["status"].(map[string]interface{})
        result = j["result"].(map[string]interface{})
        code   = status["code"].(float64)
        resp   = Response{Code: int(code)}
    )

    err = responseDetectError(resp.Code)
    if err != nil {
        resp.Data = err // Use the Data field as a vehicle for the error.
    } else {
        resp.Data = result["data"]
    }
    resp.RequestID = j["requestId"].(string)

    return resp, nil
}

resp.RequestID = j["requestId"].(string) maybe cause panic,panic message: interface conversion: interface {} is nil, not string

AdallomRoy commented 4 years ago

I can happen when your request is too long (larger than the gorilla buffer, 8192 bytes I think) and the request is split. In that case the server doesn't see the request ID so in the response it doesn't send one. The current solution is to increase the request buffer, or make your request smaller.