OSDN Git Service

Merge pull request #9 from Bytom/adjust_structure
[bytom/bytom-java-sdk.git] / tx-signer / src / main / java / io / bytom / offline / exception / ConnectivityException.java
1 package io.bytom.offline.exception;\r
2 \r
3 import com.squareup.okhttp.Response;\r
4 \r
5 import java.io.IOException;\r
6 \r
7 /**\r
8  * ConnectivityException wraps errors due to connectivity issues with the server.\r
9  */\r
10 public class ConnectivityException extends BytomException {\r
11     /**\r
12      * Initializes exception with its message attribute.\r
13      *\r
14      * @param resp the server response used to create error message\r
15      */\r
16     public ConnectivityException(Response resp) {\r
17         super(formatMessage(resp));\r
18     }\r
19 \r
20     /**\r
21      * Parses the the server response into a detailed error message.\r
22      *\r
23      * @param resp the server response\r
24      * @return detailed error message\r
25      */\r
26     private static String formatMessage(Response resp) {\r
27         String s =\r
28                 "Response HTTP header field Chain-Request-ID is unset. There may be network issues. Please check your local network settings.";\r
29         // TODO(kr): include client-generated reqid here once we have that.\r
30         String body;\r
31         try {\r
32             body = resp.body().string();\r
33         } catch (IOException ex) {\r
34             body = "[unable to read response body: " + ex.toString() + "]";\r
35         }\r
36         return String.format("%s status=%d body=%s", s, resp.code(), body);\r
37     }\r
38 }\r