1 /*
2 * Copyright (c) 2002-2026 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 Mike Bowler
23 * @author Marc Guillemot
24 * @author Ronald Brill
25 */
26 public class FailingHttpStatusCodeException extends RuntimeException {
27
28 private final WebResponse response_;
29
30 /**
31 * Creates an instance.
32 * @param failingResponse the failing response
33 */
34 public FailingHttpStatusCodeException(final WebResponse failingResponse) {
35 this(buildMessage(failingResponse), failingResponse);
36 }
37
38 /**
39 * Creates an instance.
40 * @param message the message
41 * @param failingResponse the failing response
42 */
43 public FailingHttpStatusCodeException(final String message, final WebResponse failingResponse) {
44 super(message);
45 response_ = failingResponse;
46 }
47
48 /**
49 * Returns the failing status code.
50 * @return the code
51 */
52 public int getStatusCode() {
53 return response_.getStatusCode();
54 }
55
56 /**
57 * Returns the message associated with the failing status code.
58 * @return the message
59 */
60 public String getStatusMessage() {
61 return response_.getStatusMessage();
62 }
63
64 private static String buildMessage(final WebResponse failingResponse) {
65 final int code = failingResponse.getStatusCode();
66 final String msg = failingResponse.getStatusMessage();
67 final URL url = failingResponse.getWebRequest().getUrl();
68 return code + " " + msg + " for " + url;
69 }
70
71 /**
72 * Gets the failing response.
73 * @return the response
74 */
75 public WebResponse getResponse() {
76 return response_;
77 }
78
79 }