]> git.basschouten.com Git - openhab-addons.git/blob
54ad35670f9a1fea7d73e1bf947c415a16b6f210
[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.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 static enum ErrorType {
35         UNKNOWN,
36         SERVICE_UNAVAILABLE,
37         IOEXCEPTION,
38         JSON_SYNTAX;
39     }
40
41     private final ErrorType cause;
42
43     public VolvoOnCallException(String label, @Nullable String description) {
44         super(label);
45         if ("FoundationServicesUnavailable".equalsIgnoreCase(label)) {
46             cause = ErrorType.SERVICE_UNAVAILABLE;
47         } else {
48             cause = ErrorType.UNKNOWN;
49             logger.warn("Unhandled VoC error : {} : {}", label, description);
50         }
51     }
52
53     public VolvoOnCallException(Exception e) {
54         super(e);
55         if (e instanceof IOException) {
56             cause = ErrorType.IOEXCEPTION;
57         } else if (e instanceof JsonSyntaxException) {
58             cause = ErrorType.JSON_SYNTAX;
59         } else {
60             cause = ErrorType.UNKNOWN;
61             logger.warn("Unhandled VoC error : {}", e.getMessage());
62         }
63     }
64
65     public ErrorType getType() {
66         return cause;
67     }
68 }