refactor: Return error if md cfg invalid rather than assert/crash

This commit is contained in:
John Colvin committed 2023-01-03 14:53:00 -05:00
1 parent 569ac8e3e0
commit fdf4fce5fa
3 files changed
+56 -12

No files matched your search

+39 -11
View File
@@ -1,7 +1,7 @@
#ifdef SPARK_ENABLE_CCAPI
#include "MarketDataGateway.h"
#include "MarketDataRegistration.h"
#include "lib/api/domain/Venue.pb.h"
#include "lib/api/md/DepthStreamConfig.pb.h"
#include "lib/api/md/MarketDataConfig.pb.h"
#include "lib/core/ProtoPrinter.h"
#include "lib/domain/Venue.h"
@@ -10,25 +10,47 @@
#include "lib/md/IMarketDataListener.h"
#include "lib/md/MarketDataListenerData.h"
#include "lib/md/MarketDataListenerID.h"
#include "lib/md/MarketDataRegistration.h"
#include "lib/md/MarketDataRegistrationError.h"
#include "lib/persistence/IDGenerator.h"
#include "lib/utils/ContainsKey.h"
#include "lib/utils/bts_assert.h"
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_request.h>
#include <ccapi_cpp/ccapi_session.h>
#include <ccapi_cpp/ccapi_subscription.h>
#include <optional>
#include <unordered_map>
#include <vector>
namespace bts::md {
static void _validate_cfg(const api::md::MarketDataConfig &cfg) {
bts_assert(!cfg.depth_streams().empty());
using std::string;
using std::vector;
static void validate(
const api::md::DepthStreamConfig &ds,
std::uint32_t index,
vector<string> &errors) {
if (!api::domain::Venue_IsValid(ds.venue())) {
errors.push_back(fmt::format(
"DepthStreamConfig at index {} 'venue' is invalid", index));
}
if (ds.num_levels() == 0) {
errors.push_back(fmt::format(
"DepthStreamConfig at index {} 'num_levels' is 0", index));
}
if (ds.symbols().empty()) {
errors.push_back(fmt::format(
"DepthStreamConfig at index {} 'symbols' is empty", index));
}
}
static void validate(
const api::md::MarketDataConfig &cfg, vector<string> errs) {
if (cfg.depth_streams().empty()) {
errs.push_back("MarketDataConfig 'depth_streams' is empty");
}
std::uint32_t ds_idx = 0;
for (const auto &ds : cfg.depth_streams()) {
bts_assert(ds.num_levels() > 0);
bts_assert(!ds.symbols().empty());
bts_assert(api::domain::Venue_IsValid(ds.venue()));
validate(ds, ds_idx++, errs);
}
}
@@ -52,7 +74,13 @@ MarketDataRegistration MarketDataGateway::subscribe(
LOG(md, bts::Info, "MarketDataGateway subscribing to data.\n{}\n",
core::ProtoPrinterFactory::create(&cfg));
_validate_cfg(cfg);
vector<string> cfg_errs;
validate(cfg, cfg_errs);
if (!cfg_errs.empty()) {
return MarketDataRegistration{
{MarketDataRegistrationError::Type::CONFIG_ERROR,
fmt::to_string(fmt::join(cfg_errs, "\n"))}};
}
MarketDataListenerData listener_data;
listener_data.id = *reg.listener_id;
+15
View File
@@ -8,10 +8,25 @@ namespace bts::md {
class MarketDataRegistration {
public:
MarketDataRegistration() = default;
explicit MarketDataRegistration(MarketDataListenerID listener_id);
explicit MarketDataRegistration(MarketDataRegistrationError error);
// will have a value when there is no error
std::optional<MarketDataListenerID> listener_id;
// domain::Venue venue = domain::Venue::Glbx;
MarketDataRegistrationError error = {};
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline MarketDataRegistration::MarketDataRegistration(
MarketDataListenerID listener_id)
: listener_id(listener_id), error() {}
inline MarketDataRegistration::MarketDataRegistration(
MarketDataRegistrationError error)
: listener_id(std::nullopt), error(error) {}
} // namespace bts::md
+2 -1
View File
@@ -15,7 +15,8 @@ struct MarketDataRegistrationError {
// Any error that occurred on our end. As this error type is vague, the
// accompanying error message should provide adequate information and
// context
INTERNAL_ERROR = 3
INTERNAL_ERROR = 3,
CONFIG_ERROR = 4
};
Type error_type{Type::NO_ERROR};