Skip to content

[feature] DecodeClientResponse to allow 'reply == nil' #86

@pvyleta

Description

@pvyleta

Is your feature request related to a problem? Please describe.

When I send JSON2-RPC (and others), I would like to check if there were any errors, but I do not need to parse the content of the message, for example, because I know it will always be empty. Currently, the DecodeClientResponse() will fail if I provide parameter reply == nil, as the Unmarshall() is not able to handle it. It would make sense in my eyes to allow for such an option.

Describe the solution you'd like

The API change would be negligible - only from now on, on providing reply == nil, an error would be returned only if the RPC itself would return an error. it would be enough just to add:

if reply == nil {
    return nil
}

to the DecodeClientResponse() code:

// DecodeClientResponse decodes the response body of a client request into
// the interface reply.
func DecodeClientResponse(r io.Reader, reply interface{}) error {
    var c clientResponse
    if err := json.NewDecoder(r).Decode(&c); err != nil {
	    return err
    }
    if c.Error != nil {
	    jsonErr := &Error{}
	    if err := json.Unmarshal(*c.Error, jsonErr); err != nil {
		    return &Error{
			    Code:    E_SERVER,
			    Message: string(*c.Error),
		    }
	    }
	    return jsonErr
    }

    if c.Result == nil {
	    return ErrNullResult
    }

    if reply == nil {
	    return nil
    }

    return json.Unmarshal(*c.Result, reply)
}

Describe alternatives you've considered

The workaround is to set the reply to a struct with string inside and ignore the results. This seems unnecessarily complicated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions