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.ThrowableUtil;
21  
22  import java.net.UnknownHostException;
23  
24  /**
25   * A metadata carrier exception, to propagate {@link DnsResponseCode} information as an enrichment
26   * within the {@link UnknownHostException} cause.
27   */
28  public final class DnsErrorCauseException extends RuntimeException {
29  
30      private static final long serialVersionUID = 7485145036717494533L;
31  
32      private final DnsResponseCode code;
33  
34      private DnsErrorCauseException(String message, DnsResponseCode code, boolean shared) {
35          super(message, null, false, true);
36          this.code = code;
37          assert shared;
38      }
39  
40      // Override fillInStackTrace() so we not populate the backtrace via a native call and so leak the
41      // Classloader.
42      @Override
43      public Throwable fillInStackTrace() {
44          return this;
45      }
46  
47      /**
48       * Returns the DNS error-code that caused the {@link UnknownHostException}.
49       *
50       * @return the DNS error-code that caused the {@link UnknownHostException}.
51       */
52      public DnsResponseCode getCode() {
53          return code;
54      }
55  
56      static DnsErrorCauseException newStatic(String message, DnsResponseCode code, Class<?> clazz, String method) {
57          final DnsErrorCauseException exception = new DnsErrorCauseException(message, code, true);
58          return ThrowableUtil.unknownStackTrace(exception, clazz, method);
59      }
60  }