-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketDataException.java
More file actions
96 lines (81 loc) · 3.16 KB
/
Copy pathMarketDataException.java
File metadata and controls
96 lines (81 loc) · 3.16 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.marketdata.sdk.exception;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import org.jspecify.annotations.Nullable;
public abstract sealed class MarketDataException extends RuntimeException
permits AuthenticationError,
BadRequestError,
NotFoundError,
RateLimitError,
ServerError,
NetworkError,
ParseError {
private static final long serialVersionUID = 1L;
private final ErrorContext context;
protected MarketDataException(String message, ErrorContext context, @Nullable Throwable cause) {
super(message, cause);
this.context = context;
}
public ErrorContext getContext() {
return context;
}
public @Nullable String getRequestId() {
return context.requestId();
}
/**
* The request URL with any query string redacted (replaced by {@code ?…}). Mirrors the SDK's
* ambient-log policy — query strings can carry PII (account IDs), competitive signal (queried
* symbols), or hypothetical future credentials, none of which should land in consumer logs just
* because someone called {@code logger.error("Request failed: " + ex.getRequestUrl())}. The full
* URI (with query) is preserved internally; use {@link #getContext()} when raw access is
* genuinely needed for diagnostics that won't be persisted.
*/
public String getRequestUrl() {
return redactQuery(context.requestUrl());
}
private static String redactQuery(String rawUrl) {
try {
URI uri = new URI(rawUrl);
if (uri.getRawQuery() == null) {
return rawUrl;
}
int qIndex = rawUrl.indexOf('?');
return qIndex < 0 ? rawUrl : rawUrl.substring(0, qIndex) + "?…";
} catch (URISyntaxException e) {
// Defensive: never throw from a getter. If the stored URL is malformed, return verbatim —
// it's the consumer's problem to diagnose, but not one to compound by hiding everything.
return rawUrl;
}
}
public int getStatusCode() {
return context.statusCode();
}
public Instant getTimestamp() {
return context.timestamp();
}
public String getExceptionType() {
return getClass().getSimpleName();
}
public String getSupportInfo() {
String requestId = getRequestId();
String message = getMessage();
return String.join(
System.lineSeparator(),
"--- MARKET DATA SUPPORT INFO ---",
formatField("request_id:", requestId == null ? "(not provided)" : requestId),
formatField("request_url:", getRequestUrl()),
formatField("status_code:", String.valueOf(getStatusCode())),
formatField("timestamp:", EASTERN_FORMATTER.format(getTimestamp())),
formatField("message:", message == null ? "" : message),
formatField("exception_type:", getExceptionType()),
"--------------------------------");
}
private static final DateTimeFormatter EASTERN_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("America/New_York"));
private static String formatField(String name, String value) {
return String.format("%-16s%s", name, value);
}
}