]> git.basschouten.com Git - openhab-addons.git/blob
d89570bd2f36183449f52caf5ab537463bf0db3e
[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.netatmo.internal.api;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.ServiceError;
20
21 /**
22  * An exception that occurred while communicating with Netatmo server or related processes.
23  *
24  * @author GaĆ«l L'hopital - Initial contribution
25  */
26 @NonNullByDefault
27 public class NetatmoException extends IOException {
28     private static final long serialVersionUID = 1513549973502021727L;
29     private ServiceError statusCode = ServiceError.UNKNOWN;
30
31     public NetatmoException(String format, Object... args) {
32         super(String.format(format, args));
33     }
34
35     public NetatmoException(Exception e, String format, Object... args) {
36         super(String.format(format, args), e);
37     }
38
39     public NetatmoException(String message) {
40         super(message);
41     }
42
43     public NetatmoException(ApiError error) {
44         super(error.getMessage());
45         this.statusCode = error.getCode();
46     }
47
48     public ServiceError getStatusCode() {
49         return statusCode;
50     }
51
52     @Override
53     public @Nullable String getMessage() {
54         String message = super.getMessage();
55         return message == null ? null
56                 : ServiceError.UNKNOWN.equals(statusCode) ? message
57                         : String.format("Rest call failed: statusCode=%s, message=%s", statusCode, message);
58     }
59 }