forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose DNS error codes through the UnknownHostException (netty#13721)
Motivation: UnknownHostException are covering a broader range of discovery failures, where the API consumer has no visibility of the underlying reason. Modification: Expose initial-cause for UnknownHostExceptions to enrich the result. Result: The consumer now can decide whether the exception is due to failures or due an authoritative NXDOMAIN.
- Loading branch information
Showing
2 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
resolver-dns/src/main/java/io/netty/resolver/dns/DnsErrorCauseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2023 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.netty.resolver.dns; | ||
|
||
import io.netty.handler.codec.dns.DnsResponseCode; | ||
import io.netty.util.internal.PlatformDependent; | ||
import io.netty.util.internal.SuppressJava6Requirement; | ||
import io.netty.util.internal.ThrowableUtil; | ||
|
||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* A metadata carrier exception, to propagate {@link DnsResponseCode} information as an enrichment | ||
* within the {@link UnknownHostException} cause. | ||
*/ | ||
public final class DnsErrorCauseException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 7485145036717494533L; | ||
|
||
private final DnsResponseCode code; | ||
|
||
private DnsErrorCauseException(String message, DnsResponseCode code) { | ||
super(message); | ||
this.code = code; | ||
} | ||
|
||
@SuppressJava6Requirement(reason = "uses Java 7+ Exception.<init>(String, Throwable, boolean, boolean)" + | ||
" but is guarded by version checks") | ||
private DnsErrorCauseException(String message, DnsResponseCode code, boolean shared) { | ||
super(message, null, false, true); | ||
this.code = code; | ||
assert shared; | ||
} | ||
|
||
// Override fillInStackTrace() so we not populate the backtrace via a native call and so leak the | ||
// Classloader. | ||
@Override | ||
public Throwable fillInStackTrace() { | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns the DNS error-code that caused the {@link UnknownHostException}. | ||
* | ||
* @return the DNS error-code that caused the {@link UnknownHostException}. | ||
*/ | ||
public DnsResponseCode getCode() { | ||
return code; | ||
} | ||
|
||
static DnsErrorCauseException newStatic(String message, DnsResponseCode code, Class<?> clazz, String method) { | ||
final DnsErrorCauseException exception; | ||
if (PlatformDependent.javaVersion() >= 7) { | ||
exception = new DnsErrorCauseException(message, code, true); | ||
} else { | ||
exception = new DnsErrorCauseException(message, code); | ||
} | ||
return ThrowableUtil.unknownStackTrace(exception, clazz, method); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters