View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.net.URL;
18  
19  /**
20   * An exception that is thrown when the server returns a failing status code.
21   *
22   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
23   * @author Marc Guillemot
24   */
25  public class FailingHttpStatusCodeException extends RuntimeException {
26  
27      private final WebResponse response_;
28  
29      /**
30       * Creates an instance.
31       * @param failingResponse the failing response
32       */
33      public FailingHttpStatusCodeException(final WebResponse failingResponse) {
34          this(buildMessage(failingResponse), failingResponse);
35      }
36  
37      /**
38       * Creates an instance.
39       * @param message the message
40       * @param failingResponse the failing response
41       */
42      public FailingHttpStatusCodeException(final String message, final WebResponse failingResponse) {
43          super(message);
44          response_ = failingResponse;
45      }
46  
47      /**
48       * Returns the failing status code.
49       * @return the code
50       */
51      public int getStatusCode() {
52          return response_.getStatusCode();
53      }
54  
55      /**
56       * Returns the message associated with the failing status code.
57       * @return the message
58       */
59      public String getStatusMessage() {
60          return response_.getStatusMessage();
61      }
62  
63      private static String buildMessage(final WebResponse failingResponse) {
64          final int code = failingResponse.getStatusCode();
65          final String msg = failingResponse.getStatusMessage();
66          final URL url = failingResponse.getWebRequest().getUrl();
67          return code + " " + msg + " for " + url;
68      }
69  
70      /**
71       * Gets the failing response.
72       * @return the response
73       */
74      public WebResponse getResponse() {
75          return response_;
76      }
77  
78  }