-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Expand file tree
/
Copy pathErrorHandling.java
More file actions
82 lines (71 loc) · 2.92 KB
/
Copy pathErrorHandling.java
File metadata and controls
82 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package examples;
import io.github.ccxt.exchanges.Binance;
import io.github.ccxt.errors.*;
import io.github.ccxt.types.Ticker;
import java.util.Map;
import java.util.concurrent.CompletionException;
/**
* Demonstrates error handling patterns with CCXT Java.
*
* Usage:
* cd java && ./gradlew :examples:run -PmainClass=examples.ErrorHandling
*/
public class ErrorHandling {
public static void main(String[] args) {
Binance exchange = new Binance();
exchange.loadMarkets(false);
// 1. Handle bad symbol
System.out.println("--- Test 1: Invalid symbol ---");
try {
exchange.fetchTicker("INVALID/NOTEXIST");
} catch (CompletionException e) {
Throwable cause = unwrap(e);
if (cause instanceof BadSymbol) {
System.out.println("Caught BadSymbol: " + cause.getMessage());
} else if (cause instanceof ExchangeError) {
System.out.println("Caught ExchangeError: " + cause.getMessage());
} else {
System.out.println("Caught: " + cause.getClass().getSimpleName() + ": " + cause.getMessage());
}
}
// 2. Handle authentication error
System.out.println("\n--- Test 2: Auth required without credentials ---");
try {
exchange.fetchBalance((Map<String, Object>) null);
} catch (CompletionException e) {
Throwable cause = unwrap(e);
if (cause instanceof AuthenticationError) {
System.out.println("Caught AuthenticationError: " + shorten(cause.getMessage()));
} else if (cause instanceof ExchangeError) {
System.out.println("Caught ExchangeError: " + shorten(cause.getMessage()));
} else {
System.out.println("Caught: " + cause.getClass().getSimpleName() + ": " + shorten(cause.getMessage()));
}
}
// 3. Successful request
System.out.println("\n--- Test 3: Successful ticker fetch ---");
try {
Ticker ticker = exchange.fetchTicker("BTC/USDT");
System.out.println("Success: BTC/USDT last=" + ticker.last);
} catch (CompletionException e) {
Throwable cause = unwrap(e);
System.out.println("Unexpected error: " + cause.getMessage());
}
System.out.println("\nAll error handling tests completed.");
}
static Throwable unwrap(Throwable t) {
while ((t instanceof CompletionException || t instanceof java.util.concurrent.ExecutionException)
&& t.getCause() != null) {
t = t.getCause();
}
while (t instanceof RuntimeException && t.getCause() != null
&& !(t instanceof BaseError)) {
t = t.getCause();
}
return t;
}
static String shorten(String s) {
if (s == null) return "null";
return s.length() > 100 ? s.substring(0, 100) + "..." : s;
}
}