/
handle_result.go
66 lines (54 loc) · 1.5 KB
/
handle_result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package mtproto
import (
"github.com/go-faster/errors"
"go.uber.org/zap"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/mt"
"github.com/gotd/td/internal/proto"
"github.com/gotd/td/tgerr"
)
func (c *Conn) handleResult(b *bin.Buffer) error {
// Response to an RPC query.
var res proto.Result
if err := res.Decode(b); err != nil {
return errors.Wrap(err, "decode")
}
// Now b contains result message.
b.ResetTo(res.Result)
msgID := zap.Int64("msg_id", res.RequestMessageID)
c.logWithBuffer(b).Debug("Handle result", msgID)
// Handling gzipped results.
id, err := b.PeekID()
if err != nil {
return err
}
if id == proto.GZIPTypeID {
content, err := gzip(b)
if err != nil {
return errors.Wrap(err, "decompress")
}
// Replacing buffer so callback will deal with uncompressed data.
b = content
c.logWithBuffer(b).Debug("Decompressed", msgID)
// Replacing id with inner id if error is compressed for any reason.
if id, err = b.PeekID(); err != nil {
return errors.Wrap(err, "peek id")
}
}
if id == mt.RPCErrorTypeID {
var rpcErr mt.RPCError
if err := rpcErr.Decode(b); err != nil {
return errors.Wrap(err, "error decode")
}
c.log.Debug("Got error", msgID,
zap.Int("err_code", rpcErr.ErrorCode),
zap.String("err_msg", rpcErr.ErrorMessage),
)
c.rpc.NotifyError(res.RequestMessageID, tgerr.New(rpcErr.ErrorCode, rpcErr.ErrorMessage))
return nil
}
if id == mt.PongTypeID {
return c.handlePong(b)
}
return c.rpc.NotifyResult(res.RequestMessageID, b)
}