Skip to content

Commit 8ad129f

Browse files
committed
ADD: Add error and system code enums
1 parent 2220649 commit 8ad129f

File tree

5 files changed

+130
-6
lines changed

5 files changed

+130
-6
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## 0.34.0 - TBD
4+
5+
### Enhancements
6+
- Added `SystemCode` and `ErrorCode` enums to indicate types of system and error
7+
messages
8+
- Converting a `v1::SystemMsg` to a `v2::SystemMsg` now sets to `code` to the heartbeat
9+
value
10+
11+
### Breaking changes
12+
- Changed type of `code` field in `SystemMsg` to `SystemCode`
13+
- Changed type of `code` field in `ErrorMsg` to `ErrorCode`
14+
15+
### Bug fixes
16+
- Changed `TriState` to a regular enum to handle unexpected values
17+
318
## 0.33.0 - 2025-04-15
419

520
### Enhancements

include/databento/enums.hpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,55 @@ using trading_event::TradingEvent;
439439

440440
// An enum for representing unknown, true, or false values. Equivalent to a
441441
// std::optional<bool>.
442-
enum class TriState : char {
442+
namespace tri_state {
443+
enum TriState : char {
443444
// The value is not applicable or not known.
444445
NotAvailable = '~',
445446
// False
446447
No = 'N',
447448
// True
448449
Yes = 'Y',
449450
};
451+
} // namespace tri_state
452+
using tri_state::TriState;
453+
454+
namespace error_code {
455+
enum ErrorCode : std::uint8_t {
456+
// The authentication step failed.
457+
AuthFailed = 1,
458+
// The user account or API key were deactivated.
459+
ApiKeyDeactivated = 2,
460+
// The user has exceeded their open connection limit
461+
ConnectionLimitExceeded = 3,
462+
// One or more symbols failed to resolve.
463+
SymbolResolutionFailed = 4,
464+
// There was an issue with a subscription request (other than symbol
465+
// resolution).
466+
InvalidSubscription = 5,
467+
// An error occurred in the gateway.
468+
InternalError = 6,
469+
470+
Unset = 255,
471+
};
472+
} // namespace error_code
473+
using error_code::ErrorCode;
474+
475+
namespace system_code {
476+
enum SystemCode : std::uint8_t {
477+
// A message sent in the absence of other records to indicate the connection
478+
// remains open.
479+
Heartbeat = 0,
480+
// An acknowledgement of a subscription request.
481+
SubscriptionAck = 1,
482+
// The gateway has detected this session is falling behind real-time.
483+
SlowReaderWarning = 2,
484+
// Indicates a replay subscription has caught up with real-time data.
485+
ReplayCompleted = 3,
486+
487+
Unset = 255,
488+
};
489+
} // namespace system_code
490+
using system_code::SystemCode;
450491

451492
// Convert a HistoricalGateway to a URL.
452493
const char* UrlFromGateway(HistoricalGateway gateway);
@@ -474,6 +515,8 @@ const char* ToString(StatusReason status_reason);
474515
const char* ToString(TradingEvent trading_event);
475516
const char* ToString(TriState tri_state);
476517
const char* ToString(VersionUpgradePolicy upgrade_policy);
518+
const char* ToString(ErrorCode error_code);
519+
const char* ToString(SystemCode system_code);
477520

478521
std::ostream& operator<<(std::ostream& out, Schema schema);
479522
std::ostream& operator<<(std::ostream& out, Encoding encoding);
@@ -501,6 +544,8 @@ std::ostream& operator<<(std::ostream& out, TradingEvent trading_event);
501544
std::ostream& operator<<(std::ostream& out, TriState tri_state);
502545
std::ostream& operator<<(std::ostream& out,
503546
VersionUpgradePolicy upgrade_policy);
547+
std::ostream& operator<<(std::ostream& out, ErrorCode error_code);
548+
std::ostream& operator<<(std::ostream& out, SystemCode system_code);
504549

505550
template <>
506551
Schema FromString(const std::string& str);

include/databento/record.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <cstring> // strncmp
7+
#include <limits>
78
#include <string>
89
#include <tuple> // tie
910
#include <type_traits>
@@ -528,7 +529,7 @@ struct ErrorMsg {
528529

529530
RecordHeader hd;
530531
std::array<char, 302> err;
531-
std::uint8_t code;
532+
ErrorCode code;
532533
std::uint8_t is_last;
533534
};
534535
static_assert(sizeof(ErrorMsg) == 320, "ErrorMsg size must match Rust");
@@ -560,12 +561,17 @@ struct SystemMsg {
560561
UnixNanos IndexTs() const { return hd.ts_event; }
561562
const char* Msg() const { return msg.data(); }
562563
bool IsHeartbeat() const {
563-
return std::strncmp(msg.data(), "Heartbeat", 9) == 0;
564+
// Check if code is unset
565+
if (static_cast<std::uint8_t>(code) ==
566+
std::numeric_limits<std::uint8_t>::max()) {
567+
return std::strncmp(msg.data(), "Heartbeat", 9) == 0;
568+
}
569+
return code == SystemCode::Heartbeat;
564570
}
565571

566572
RecordHeader hd;
567573
std::array<char, 303> msg;
568-
std::uint8_t code;
574+
SystemCode code;
569575
};
570576
static_assert(sizeof(SystemMsg) == 320, "SystemMsg size must match Rust");
571577
static_assert(alignof(SystemMsg) == 8, "Must have 8-byte alignment");

src/enums.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,52 @@ const char* ToString(VersionUpgradePolicy upgrade_policy) {
760760
}
761761
}
762762

763+
const char* ToString(ErrorCode error_code) {
764+
switch (error_code) {
765+
case ErrorCode::AuthFailed: {
766+
return "auth_failed";
767+
}
768+
case ErrorCode::ApiKeyDeactivated: {
769+
return "api_key_deactivated";
770+
}
771+
case ErrorCode::ConnectionLimitExceeded: {
772+
return "connection_limit_exceeded";
773+
}
774+
case ErrorCode::SymbolResolutionFailed: {
775+
return "symbol_resolution_failed";
776+
}
777+
case ErrorCode::InvalidSubscription: {
778+
return "invalid_subscription";
779+
}
780+
case ErrorCode::InternalError: {
781+
return "internal_error";
782+
}
783+
default: {
784+
return "Unknown";
785+
}
786+
}
787+
}
788+
789+
const char* ToString(SystemCode system_code) {
790+
switch (system_code) {
791+
case SystemCode::Heartbeat: {
792+
return "heartbeat";
793+
}
794+
case SystemCode::SubscriptionAck: {
795+
return "subscription_ack";
796+
}
797+
case SystemCode::SlowReaderWarning: {
798+
return "slow_reader_warning";
799+
}
800+
case SystemCode::ReplayCompleted: {
801+
return "replay_completed";
802+
}
803+
default: {
804+
return "Unknown";
805+
}
806+
}
807+
}
808+
763809
std::ostream& operator<<(std::ostream& out, Schema schema) {
764810
out << ToString(schema);
765811
return out;
@@ -876,6 +922,16 @@ std::ostream& operator<<(std::ostream& out,
876922
return out;
877923
}
878924

925+
std::ostream& operator<<(std::ostream& out, ErrorCode error_code) {
926+
out << ToString(error_code);
927+
return out;
928+
}
929+
930+
std::ostream& operator<<(std::ostream& out, SystemCode system_code) {
931+
out << ToString(system_code);
932+
return out;
933+
}
934+
879935
template <>
880936
Schema FromString(const std::string& str) {
881937
if (str == "mbo") {

src/v1.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include <algorithm> // copy
44
#include <cstdint>
5+
#include <cstring>
56
#include <limits> // numeric_limits
67

78
#include "databento/constants.hpp"
9+
#include "databento/enums.hpp"
810
#include "databento/fixed_price.hpp" // FixedPx
911
#include "databento/record.hpp"
1012
#include "databento/v2.hpp"
@@ -193,7 +195,7 @@ v2::ErrorMsg ErrorMsg::ToV2() const {
193195
RType::Error, hd.publisher_id, hd.instrument_id,
194196
hd.ts_event},
195197
{},
196-
std::numeric_limits<std::uint8_t>::max(),
198+
ErrorCode::Unset,
197199
std::numeric_limits<std::uint8_t>::max()};
198200
std::copy(err.begin(), err.end(), ret.err.begin());
199201
return ret;
@@ -227,7 +229,7 @@ v2::SystemMsg SystemMsg::ToV2() const {
227229
RType::System, hd.publisher_id, hd.instrument_id,
228230
hd.ts_event},
229231
{},
230-
std::numeric_limits<std::uint8_t>::max()};
232+
IsHeartbeat() ? SystemCode::Heartbeat : SystemCode::Unset};
231233
std::copy(msg.begin(), msg.end(), ret.msg.begin());
232234
return ret;
233235
}

0 commit comments

Comments
 (0)