]> git.basschouten.com Git - openhab-addons.git/blob
166c7b85e5a86f9d580ed05d28ffd928c73e472a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 final 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 @Nullable 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)
96                 || nonNullString(getMessage()).toLowerCase().contains("timeout"));
97     }
98
99     public boolean isHttpAccessUnauthorized() {
100         return apiResult.isHttpAccessUnauthorized();
101     }
102
103     public boolean isUnknownHost() {
104         return getCauseClass() == MalformedURLException.class;
105     }
106
107     public boolean isMalformedURL() {
108         return getCauseClass() == UnknownHostException.class;
109     }
110
111     public boolean isJSONException() {
112         return getCauseClass() == JsonSyntaxException.class;
113     }
114
115     public ShellyApiResult getApiResult() {
116         return apiResult;
117     }
118
119     private boolean isEmpty() {
120         return NONE.equals(nonNullString(super.getMessage()));
121     }
122
123     private static String nonNullString(@Nullable String s) {
124         return s != null ? s : "";
125     }
126
127     private Class<?> getCauseClass() {
128         Throwable cause = getCause();
129         if (cause != null) {
130             return cause.getClass();
131         }
132         return ShellyApiException.class;
133     }
134 }