View Javadoc
1   /*
2    * Copyright 2023 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.resolver.dns;
18  
19  import io.netty.handler.codec.dns.DnsResponseCode;
20  import io.netty.util.internal.PlatformDependent;
21  import io.netty.util.internal.SuppressJava6Requirement;
22  import io.netty.util.internal.ThrowableUtil;
23  
24  import java.net.UnknownHostException;
25  
26  /**
27   * A metadata carrier exception, to propagate {@link DnsResponseCode} information as an enrichment
28   * within the {@link UnknownHostException} cause.
29   */
30  public final class DnsErrorCauseException extends RuntimeException {
31  
32      private static final long serialVersionUID = 7485145036717494533L;
33  
34      private final DnsResponseCode code;
35  
36      private DnsErrorCauseException(String message, DnsResponseCode code) {
37          super(message);
38          this.code = code;
39      }
40  
41      @SuppressJava6Requirement(reason = "uses Java 7+ Exception.<init>(String, Throwable, boolean, boolean)" +
42              " but is guarded by version checks")
43      private DnsErrorCauseException(String message, DnsResponseCode code, boolean shared) {
44          super(message, null, false, true);
45          this.code = code;
46          assert shared;
47      }
48  
49      // Override fillInStackTrace() so we not populate the backtrace via a native call and so leak the
50      // Classloader.
51      @Override
52      public Throwable fillInStackTrace() {
53          return this;
54      }
55  
56      /**
57       * Returns the DNS error-code that caused the {@link UnknownHostException}.
58       *
59       * @return the DNS error-code that caused the {@link UnknownHostException}.
60       */
61      public DnsResponseCode getCode() {
62          return code;
63      }
64  
65      static DnsErrorCauseException newStatic(String message, DnsResponseCode code, Class<?> clazz, String method) {
66          final DnsErrorCauseException exception;
67          if (PlatformDependent.javaVersion() >= 7) {
68              exception = new DnsErrorCauseException(message, code, true);
69          } else {
70              exception = new DnsErrorCauseException(message, code);
71          }
72          return ThrowableUtil.unknownStackTrace(exception, clazz, method);
73      }
74  }