]> git.basschouten.com Git - openhab-addons.git/blob
0d5ddbb266a580866899f66a3893f85366d8709e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.volvooncall.internal;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.gson.JsonSyntaxException;
23
24 /**
25  * Exception for errors when using the VolvoOnCall API
26  *
27  * @author GaĆ«l L'hopital - Initial contribution
28  */
29 @NonNullByDefault
30 public class VolvoOnCallException extends Exception {
31     private final Logger logger = LoggerFactory.getLogger(VolvoOnCallException.class);
32     private static final long serialVersionUID = -6215621577081394328L;
33
34     public enum ErrorType {
35         UNKNOWN,
36         SERVICE_UNAVAILABLE,
37         SERVICE_UNABLE_TO_START,
38         IOEXCEPTION,
39         INTERRUPTED,
40         JSON_SYNTAX
41     }
42
43     private final ErrorType cause;
44
45     public VolvoOnCallException(String label, @Nullable String description) {
46         super(label);
47         if ("FoundationServicesUnavailable".equalsIgnoreCase(label)) {
48             cause = ErrorType.SERVICE_UNAVAILABLE;
49         } else if ("ServiceUnableToStart".equalsIgnoreCase(label)) {
50             cause = ErrorType.SERVICE_UNABLE_TO_START;
51         } else {
52             cause = ErrorType.UNKNOWN;
53             logger.warn("Unhandled VoC error : {} : {}", label, description);
54         }
55     }
56
57     public VolvoOnCallException(Exception e) {
58         super(e);
59         if (e instanceof IOException) {
60             cause = ErrorType.IOEXCEPTION;
61         } else if (e instanceof JsonSyntaxException) {
62             cause = ErrorType.JSON_SYNTAX;
63         } else if (e instanceof InterruptedException) {
64             cause = ErrorType.INTERRUPTED;
65         } else {
66             cause = ErrorType.UNKNOWN;
67             logger.warn("Unhandled VoC error : {}", e.getMessage());
68         }
69     }
70
71     public ErrorType getType() {
72         return cause;
73     }
74 }