-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketDataException.java
More file actions
80 lines (69 loc) · 2.74 KB
/
Copy pathMarketDataException.java
File metadata and controls
80 lines (69 loc) · 2.74 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
package com.marketdata.sdk.exception;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.jspecify.annotations.Nullable;
/**
* Root of the SDK exception hierarchy.
*
* <p>Sealed so consumer {@code switch} statements over its subtypes are compile-time exhaustive.
* Every instance carries the support context fields required by SDK requirements §6.2 and exposes a
* {@link #getSupportInfo()} string per §6.3.
*
* <p>Subtypes use {@link ErrorContext#empty()} for client-side validation errors that occur before
* any HTTP request is dispatched.
*/
public abstract sealed class MarketDataException extends RuntimeException
permits AuthenticationError,
BadRequestError,
NotFoundError,
RateLimitError,
ServerError,
NetworkError,
ParseError {
private static final ZoneId EASTERN = ZoneId.of("America/New_York");
private static final DateTimeFormatter TIMESTAMP_FORMAT =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final @Nullable String requestId;
private final @Nullable String requestUrl;
private final @Nullable Integer statusCode;
private final ZonedDateTime timestamp;
protected MarketDataException(String message, ErrorContext context, @Nullable Throwable cause) {
super(message, cause);
this.requestId = context.requestId();
this.requestUrl = context.requestUrl();
this.statusCode = context.statusCode();
this.timestamp = ZonedDateTime.now(EASTERN);
}
public @Nullable String getRequestId() {
return requestId;
}
public @Nullable String getRequestUrl() {
return requestUrl;
}
public @Nullable Integer getStatusCode() {
return statusCode;
}
public ZonedDateTime getTimestamp() {
return timestamp;
}
public String getExceptionType() {
return getClass().getSimpleName();
}
/**
* Multi-line, human-readable summary of the error and its context, intended to be copy-pasted
* into a support ticket. Never contains the API token or request body.
*/
public String getSupportInfo() {
StringBuilder sb = new StringBuilder(256);
sb.append("Market Data SDK Error\n");
sb.append("---------------------\n");
sb.append("Type: ").append(getExceptionType()).append('\n');
sb.append("Message: ").append(getMessage()).append('\n');
sb.append("Status code: ").append(statusCode != null ? statusCode : "(n/a)").append('\n');
sb.append("Request ID: ").append(requestId != null ? requestId : "(n/a)").append('\n');
sb.append("Request URL: ").append(requestUrl != null ? requestUrl : "(n/a)").append('\n');
sb.append("Timestamp: ").append(timestamp.format(TIMESTAMP_FORMAT)).append(" (US/Eastern)");
return sb.toString();
}
}