Blob


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