-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
/
Errors.java
195 lines (174 loc) · 8.09 KB
/
Errors.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty5.channel.unix;
import io.netty5.util.internal.EmptyArrays;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.nio.channels.AlreadyConnectedException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.NotYetConnectedException;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoEAGAIN;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoEBADF;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoECONNRESET;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoEINPROGRESS;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoENOENT;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoENOTCONN;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoEPIPE;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoEWOULDBLOCK;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errorEALREADY;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errorECONNREFUSED;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errorEHOSTUNREACH;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errorEISCONN;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.errorENETUNREACH;
import static io.netty5.channel.unix.ErrorsStaticallyReferencedJniMethods.strError;
/**
* <strong>Internal usage only!</strong>
* <p>Static members which call JNI methods must be defined in {@link ErrorsStaticallyReferencedJniMethods}.
*/
public final class Errors {
// As all our JNI methods return -errno on error we need to compare with the negative errno codes.
public static final int ERRNO_ENOENT_NEGATIVE = -errnoENOENT();
public static final int ERRNO_ENOTCONN_NEGATIVE = -errnoENOTCONN();
public static final int ERRNO_EBADF_NEGATIVE = -errnoEBADF();
public static final int ERRNO_EPIPE_NEGATIVE = -errnoEPIPE();
public static final int ERRNO_ECONNRESET_NEGATIVE = -errnoECONNRESET();
public static final int ERRNO_EAGAIN_NEGATIVE = -errnoEAGAIN();
public static final int ERRNO_EWOULDBLOCK_NEGATIVE = -errnoEWOULDBLOCK();
public static final int ERRNO_EINPROGRESS_NEGATIVE = -errnoEINPROGRESS();
public static final int ERROR_ECONNREFUSED_NEGATIVE = -errorECONNREFUSED();
public static final int ERROR_EISCONN_NEGATIVE = -errorEISCONN();
public static final int ERROR_EALREADY_NEGATIVE = -errorEALREADY();
public static final int ERROR_ENETUNREACH_NEGATIVE = -errorENETUNREACH();
public static final int ERROR_EHOSTUNREACH_NEGATIVE = -errorEHOSTUNREACH();
/**
* Holds the mappings for errno codes to String messages.
* This eliminates the need to call back into JNI to get the right String message on an exception
* and thus is faster.
*
* Choose an array length which should give us enough space in the future even when more errno codes
* will be added.
*/
private static final String[] ERRORS = new String[2048];
/**
* <strong>Internal usage only!</strong>
*/
public static final class NativeIoException extends IOException {
private static final long serialVersionUID = 8222160204268655526L;
private final int expectedErr;
private final boolean fillInStackTrace;
public NativeIoException(String method, int expectedErr) {
this(method, expectedErr, true);
}
public NativeIoException(String method, int expectedErr, boolean fillInStackTrace) {
super(method + "(..) failed: " + errnoString(-expectedErr));
this.expectedErr = expectedErr;
this.fillInStackTrace = fillInStackTrace;
}
public int expectedErr() {
return expectedErr;
}
@Override
public synchronized Throwable fillInStackTrace() {
if (fillInStackTrace) {
return super.fillInStackTrace();
}
return this;
}
}
static {
for (int i = 0; i < ERRORS.length; i++) {
// This is ok as strerror returns 'Unknown error i' when the message is not known.
ERRORS[i] = strError(i);
}
}
static boolean handleConnectErrno(String method, int err) throws IOException {
if (err == ERRNO_EINPROGRESS_NEGATIVE || err == ERROR_EALREADY_NEGATIVE) {
// connect not complete yet need to wait for EPOLLOUT event.
// EALREADY has been observed when using tcp fast open on centos8.
return false;
}
throw newConnectException0(method, err);
}
private static String errnoString(int err) {
// Check first if we had it cached, if not we need to do a JNI call.
if (err < ERRORS.length - 1) {
return ERRORS[err];
}
return strError(err);
}
private static IOException newConnectException0(String method, int err) {
if (err == ERROR_ENETUNREACH_NEGATIVE) {
return new NoRouteToHostException();
}
if (err == ERROR_EISCONN_NEGATIVE) {
throw new AlreadyConnectedException();
}
if (err == ERRNO_ENOENT_NEGATIVE) {
return new FileNotFoundException();
}
return new ConnectException(method + "(..) failed: " + errnoString(-err));
}
public static NativeIoException newConnectionResetException(String method, int errnoNegative) {
NativeIoException exception = new NativeIoException(method, errnoNegative, false);
exception.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
return exception;
}
public static NativeIoException newIOException(String method, int err) {
return new NativeIoException(method, err);
}
@Deprecated
public static int ioResult(String method, int err, NativeIoException resetCause,
ClosedChannelException closedCause) throws IOException {
// network stack saturated... try again later
if (err == ERRNO_EAGAIN_NEGATIVE || err == ERRNO_EWOULDBLOCK_NEGATIVE) {
return 0;
}
if (err == resetCause.expectedErr()) {
throw resetCause;
}
if (err == ERRNO_EBADF_NEGATIVE) {
throw closedCause;
}
if (err == ERRNO_ENOTCONN_NEGATIVE) {
throw new NotYetConnectedException();
}
if (err == ERRNO_ENOENT_NEGATIVE) {
throw new FileNotFoundException();
}
// TODO: We could even go further and use a pre-instantiated IOException for the other error codes, but for
// all other errors it may be better to just include a stack trace.
throw newIOException(method, err);
}
public static int ioResult(String method, int err) throws IOException {
// network stack saturated... try again later
if (err == ERRNO_EAGAIN_NEGATIVE || err == ERRNO_EWOULDBLOCK_NEGATIVE) {
return 0;
}
if (err == ERRNO_EBADF_NEGATIVE) {
throw new ClosedChannelException();
}
if (err == ERRNO_ENOTCONN_NEGATIVE) {
throw new NotYetConnectedException();
}
if (err == ERRNO_ENOENT_NEGATIVE) {
throw new FileNotFoundException();
}
throw new NativeIoException(method, err, false);
}
private Errors() { }
}