Blame


1 c74282f8 2016-11-16 noreply package p9p
2 c74282f8 2016-11-16 noreply
3 c74282f8 2016-11-16 noreply import "fmt"
4 c74282f8 2016-11-16 noreply
5 c74282f8 2016-11-16 noreply // Overflow will return a positive number, indicating there was an overflow for
6 c74282f8 2016-11-16 noreply // the error.
7 c74282f8 2016-11-16 noreply func Overflow(err error) int {
8 c74282f8 2016-11-16 noreply if of, ok := err.(overflow); ok {
9 c74282f8 2016-11-16 noreply return of.Size()
10 c74282f8 2016-11-16 noreply }
11 c74282f8 2016-11-16 noreply
12 c74282f8 2016-11-16 noreply // traverse cause, if above fails.
13 c74282f8 2016-11-16 noreply if causal, ok := err.(interface {
14 c74282f8 2016-11-16 noreply Cause() error
15 c74282f8 2016-11-16 noreply }); ok {
16 c74282f8 2016-11-16 noreply return Overflow(causal.Cause())
17 c74282f8 2016-11-16 noreply }
18 c74282f8 2016-11-16 noreply
19 c74282f8 2016-11-16 noreply return 0
20 c74282f8 2016-11-16 noreply }
21 c74282f8 2016-11-16 noreply
22 c74282f8 2016-11-16 noreply // overflow is a resolvable error type that can help callers negotiate
23 c74282f8 2016-11-16 noreply // session msize. If this error is encountered, no message was sent.
24 c74282f8 2016-11-16 noreply //
25 c74282f8 2016-11-16 noreply // The return value of `Size()` represents the number of bytes that would have
26 c74282f8 2016-11-16 noreply // been truncated if the message were sent. This IS NOT the optimal buffer size
27 c74282f8 2016-11-16 noreply // for operations like read and write.
28 c74282f8 2016-11-16 noreply //
29 c74282f8 2016-11-16 noreply // In the case of `Twrite`, the caller can Size() from the local size to get an
30 c74282f8 2016-11-16 noreply // optimally size buffer or the write can simply be truncated to `len(buf) -
31 c74282f8 2016-11-16 noreply // err.Size()`.
32 c74282f8 2016-11-16 noreply //
33 c74282f8 2016-11-16 noreply // For the most part, no users of this package should see this error in
34 c74282f8 2016-11-16 noreply // practice. If this escapes the Session interface, it is a bug.
35 c74282f8 2016-11-16 noreply type overflow interface {
36 c74282f8 2016-11-16 noreply Size() int // number of bytes overflowed.
37 c74282f8 2016-11-16 noreply }
38 c74282f8 2016-11-16 noreply
39 c74282f8 2016-11-16 noreply type overflowErr struct {
40 c74282f8 2016-11-16 noreply size int // number of bytes overflowed
41 c74282f8 2016-11-16 noreply }
42 c74282f8 2016-11-16 noreply
43 c74282f8 2016-11-16 noreply func (o overflowErr) Error() string {
44 c74282f8 2016-11-16 noreply return fmt.Sprintf("message overflowed %d bytes", o.size)
45 c74282f8 2016-11-16 noreply }
46 c74282f8 2016-11-16 noreply
47 c74282f8 2016-11-16 noreply func (o overflowErr) Size() int {
48 c74282f8 2016-11-16 noreply return o.size
49 c74282f8 2016-11-16 noreply }