Blame


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