forked from docker-archive/go-p9p
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoverflow.go
49 lines (41 loc) · 1.28 KB
/
overflow.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
package p9p
import "fmt"
// Overflow will return a positive number, indicating there was an overflow for
// the error.
func Overflow(err error) int {
if of, ok := err.(overflow); ok {
return of.Size()
}
// traverse cause, if above fails.
if causal, ok := err.(interface {
Cause() error
}); ok {
return Overflow(causal.Cause())
}
return 0
}
// overflow is a resolvable error type that can help callers negotiate
// session msize. If this error is encountered, no message was sent.
//
// The return value of `Size()` represents the number of bytes that would have
// been truncated if the message were sent. This IS NOT the optimal buffer size
// for operations like read and write.
//
// In the case of `Twrite`, the caller can Size() from the local size to get an
// optimally size buffer or the write can simply be truncated to `len(buf) -
// err.Size()`.
//
// For the most part, no users of this package should see this error in
// practice. If this escapes the Session interface, it is a bug.
type overflow interface {
Size() int // number of bytes overflowed.
}
type overflowErr struct {
size int // number of bytes overflowed
}
func (o overflowErr) Error() string {
return fmt.Sprintf("message overflowed %d bytes", o.size)
}
func (o overflowErr) Size() int {
return o.size
}