OSDN Git Service

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