]> git.basschouten.com Git - openhab-addons.git/blob
1f7f42dbca143de39829198c6ec48467ef060720
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.shelly.internal.api;
14
15 import java.net.MalformedURLException;
16 import java.net.UnknownHostException;
17 import java.text.MessageFormat;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 import com.google.gson.JsonSyntaxException;
25
26 /**
27  * The {@link CarNetException} implements an extension to the standard Exception class. This allows to keep also the
28  * result of the last API call (e.g. including the http status code in the message).
29  *
30  * @author Markus Michels - Initial contribution
31  */
32 @NonNullByDefault
33 public class ShellyApiException extends Exception {
34     private static final long serialVersionUID = -5809459454769761821L;
35
36     private ShellyApiResult apiResult = new ShellyApiResult();
37     private static String NONE = "none";
38
39     public ShellyApiException(Exception exception) {
40         super(exception);
41     }
42
43     public ShellyApiException(String message) {
44         super(message);
45     }
46
47     public ShellyApiException(ShellyApiResult res) {
48         super(NONE);
49         apiResult = res;
50     }
51
52     public ShellyApiException(String message, Exception exception) {
53         super(message, exception);
54     }
55
56     public ShellyApiException(ShellyApiResult result, Exception exception) {
57         super(exception);
58         apiResult = result;
59     }
60
61     @Override
62     public String getMessage() {
63         return isEmpty() ? "" : nonNullString(super.getMessage());
64     }
65
66     @Override
67     public String toString() {
68         String message = nonNullString(super.getMessage());
69         String cause = getCauseClass().toString();
70         if (!isEmpty()) {
71             if (isUnknownHost()) {
72                 String[] string = message.split(": "); // java.net.UnknownHostException: api.rach.io
73                 message = MessageFormat.format("Unable to connect to {0} (Unknown host / Network down / Low signal)",
74                         string[1]);
75             } else if (isMalformedURL()) {
76                 message = MessageFormat.format("Invalid URL: {0}", apiResult.getUrl());
77             } else if (isTimeout()) {
78                 message = MessageFormat.format("Device unreachable or API Timeout ({0})", apiResult.getUrl());
79             } else {
80                 message = MessageFormat.format("{0} ({1})", message, cause);
81             }
82         } else {
83             message = apiResult.toString();
84         }
85         return message;
86     }
87
88     public boolean isApiException() {
89         return getCauseClass() == ShellyApiException.class;
90     }
91
92     public boolean isTimeout() {
93         Class<?> extype = !isEmpty() ? getCauseClass() : null;
94         return (extype != null) && ((extype == TimeoutException.class) || (extype == ExecutionException.class)
95                 || (extype == InterruptedException.class) || getMessage().toLowerCase().contains("timeout"));
96     }
97
98     public boolean isHttpAccessUnauthorized() {
99         return apiResult.isHttpAccessUnauthorized();
100     }
101
102     public boolean isUnknownHost() {
103         return getCauseClass() == MalformedURLException.class;
104     }
105
106     public boolean isMalformedURL() {
107         return getCauseClass() == UnknownHostException.class;
108     }
109
110     public boolean isJSONException() {
111         return getCauseClass() == JsonSyntaxException.class;
112     }
113
114     public ShellyApiResult getApiResult() {
115         return apiResult;
116     }
117
118     private boolean isEmpty() {
119         return nonNullString(super.getMessage()).equals(NONE);
120     }
121
122     private static String nonNullString(@Nullable String s) {
123         return s != null ? s : "";
124     }
125
126     private Class<?> getCauseClass() {
127         Throwable cause = getCause();
128         if (getCause() != null) {
129             return cause.getClass();
130         }
131         return ShellyApiException.class;
132     }
133 }