Skip to content

Commit 61d852e

Browse files
committed
quic: error codes and types
Constants for the transport error codes in RFC 9000 Section 20, types representing transport errors sent to or received from the peer, and a type representing application protocol errors. For golang/go#58547 Change-Id: Ib4325e1272f6e0984f233ef494827a1799d7dc26 Reviewed-on: https://go-review.googlesource.com/c/net/+/495235 Reviewed-by: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Damien Neil <dneil@google.com>
1 parent d40f154 commit 61d852e

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

internal/quic/errors.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package quic
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
// A transportError is an transport error code from RFC 9000 Section 20.1.
12+
//
13+
// The transportError type doesn't implement the error interface to ensure we always
14+
// distinguish between errors sent to and received from the peer.
15+
// See the localTransportError and peerTransportError types below.
16+
type transportError uint64
17+
18+
// https://www.rfc-editor.org/rfc/rfc9000.html#section-20.1
19+
const (
20+
errNo = transportError(0x00)
21+
errInternal = transportError(0x01)
22+
errConnectionRefused = transportError(0x02)
23+
errFlowControl = transportError(0x03)
24+
errStreamLimit = transportError(0x04)
25+
errStreamState = transportError(0x05)
26+
errFinalSize = transportError(0x06)
27+
errFrameEncoding = transportError(0x07)
28+
errTransportParameter = transportError(0x08)
29+
errConnectionIDLimit = transportError(0x09)
30+
errProtocolViolation = transportError(0x0a)
31+
errInvalidToken = transportError(0x0b)
32+
errApplicationError = transportError(0x0c)
33+
errCryptoBufferExceeded = transportError(0x0d)
34+
errKeyUpdateError = transportError(0x0e)
35+
errAEADLimitReached = transportError(0x0f)
36+
errNoViablePath = transportError(0x10)
37+
errTLSBase = transportError(0x0100) // 0x0100-0x01ff; base + TLS code
38+
)
39+
40+
func (e transportError) String() string {
41+
switch e {
42+
case errNo:
43+
return "NO_ERROR"
44+
case errInternal:
45+
return "INTERNAL_ERROR"
46+
case errConnectionRefused:
47+
return "CONNECTION_REFUSED"
48+
case errFlowControl:
49+
return "FLOW_CONTROL_ERROR"
50+
case errStreamLimit:
51+
return "STREAM_LIMIT_ERROR"
52+
case errStreamState:
53+
return "STREAM_STATE_ERROR"
54+
case errFinalSize:
55+
return "FINAL_SIZE_ERROR"
56+
case errFrameEncoding:
57+
return "FRAME_ENCODING_ERROR"
58+
case errTransportParameter:
59+
return "TRANSPORT_PARAMETER_ERROR"
60+
case errConnectionIDLimit:
61+
return "CONNECTION_ID_LIMIT_ERROR"
62+
case errProtocolViolation:
63+
return "PROTOCOL_VIOLATION"
64+
case errInvalidToken:
65+
return "INVALID_TOKEN"
66+
case errApplicationError:
67+
return "APPLICATION_ERROR"
68+
case errCryptoBufferExceeded:
69+
return "CRYPTO_BUFFER_EXCEEDED"
70+
case errKeyUpdateError:
71+
return "KEY_UPDATE_ERROR"
72+
case errAEADLimitReached:
73+
return "AEAD_LIMIT_REACHED"
74+
case errNoViablePath:
75+
return "NO_VIABLE_PATH"
76+
}
77+
if e >= 0x0100 && e <= 0x01ff {
78+
return fmt.Sprintf("CRYPTO_ERROR(%v)", uint64(e)&0xff)
79+
}
80+
return fmt.Sprintf("ERROR %d", uint64(e))
81+
}
82+
83+
// A localTransportError is an error sent to the peer.
84+
type localTransportError transportError
85+
86+
func (e localTransportError) Error() string {
87+
return "closed connection: " + transportError(e).String()
88+
}
89+
90+
// A peerTransportError is an error received from the peer.
91+
type peerTransportError struct {
92+
code transportError
93+
reason string
94+
}
95+
96+
func (e peerTransportError) Error() string {
97+
return fmt.Sprintf("peer closed connection: %v: %q", e.code, e.reason)
98+
}
99+
100+
// An ApplicationError is an application protocol error code (RFC 9000, Section 20.2).
101+
// Application protocol errors may be sent when terminating a stream or connection.
102+
type ApplicationError struct {
103+
Code uint64
104+
Reason string
105+
}
106+
107+
func (e ApplicationError) Error() string {
108+
// TODO: Include the Reason string here, but sanitize it first.
109+
return fmt.Sprintf("AppError %v", e.Code)
110+
}

0 commit comments

Comments
 (0)