(satori_publisher) better error handling
This commit is contained in:
@ -88,9 +88,18 @@ namespace ix
|
||||
}
|
||||
}
|
||||
|
||||
void SatoriConnection::logError(const std::string& error)
|
||||
void SatoriConnection::setErrorCallback(const ErrorCallback& errorCallback)
|
||||
{
|
||||
std::cerr << "SatoriConnection: " << error;
|
||||
_errorCallback = errorCallback;
|
||||
}
|
||||
|
||||
void SatoriConnection::invokeErrorCallback(const std::string& errorMsg)
|
||||
{
|
||||
|
||||
if (_errorCallback)
|
||||
{
|
||||
_errorCallback(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
void SatoriConnection::disconnect()
|
||||
@ -145,13 +154,13 @@ namespace ix
|
||||
Json::Value data;
|
||||
if (!parseJson(str, data))
|
||||
{
|
||||
logError(std::string("Invalid json: ") + str);
|
||||
invokeErrorCallback(std::string("Invalid json: ") + str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.isMember("action"))
|
||||
{
|
||||
logError("Missing action");
|
||||
invokeErrorCallback("Missing action");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -161,12 +170,12 @@ namespace ix
|
||||
{
|
||||
if (!handleHandshakeResponse(data))
|
||||
{
|
||||
logError("Error extracting nonce from handshake response");
|
||||
invokeErrorCallback("Error extracting nonce from handshake response");
|
||||
}
|
||||
}
|
||||
else if (action == "auth/handshake/error")
|
||||
{
|
||||
logError("Handshake error."); // print full message ?
|
||||
invokeErrorCallback("Handshake error."); // print full message ?
|
||||
}
|
||||
else if (action == "auth/authenticate/ok")
|
||||
{
|
||||
@ -176,7 +185,7 @@ namespace ix
|
||||
}
|
||||
else if (action == "auth/authenticate/error")
|
||||
{
|
||||
logError("Authentication error."); // print full message ?
|
||||
invokeErrorCallback("Authentication error."); // print full message ?
|
||||
}
|
||||
else if (action == "rtm/subscription/data")
|
||||
{
|
||||
@ -184,7 +193,7 @@ namespace ix
|
||||
}
|
||||
else
|
||||
{
|
||||
logError(std::string("Un-handled message type: ") + action);
|
||||
invokeErrorCallback(std::string("Un-handled message type: ") + action);
|
||||
}
|
||||
}
|
||||
else if (messageType == ix::WebSocket_MessageType_Error)
|
||||
@ -194,7 +203,7 @@ namespace ix
|
||||
ss << "#retries: " << error.retries << std::endl;
|
||||
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
||||
ss << "HTTP Status: " << error.http_status << std::endl;
|
||||
logError(ss.str());
|
||||
invokeErrorCallback(ss.str());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ namespace ix
|
||||
{
|
||||
using SubscriptionCallback = std::function<void(const Json::Value&)>;
|
||||
using AuthenticatedCallback = std::function<void()>;
|
||||
using ErrorCallback = std::function<void(const std::string& errorMsg)>;
|
||||
using OnTrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
|
||||
|
||||
class SatoriConnection
|
||||
@ -40,8 +41,10 @@ namespace ix
|
||||
/// Reset the traffic tracker callback to an no-op one.
|
||||
static void resetTrafficTrackerCallback();
|
||||
|
||||
/// Reset the traffic tracker callback to an no-op one.
|
||||
/// Set the authenticated callback
|
||||
void setAuthenticatedCallback(const AuthenticatedCallback& authenticatedCallback);
|
||||
/// Set the error callback
|
||||
void setErrorCallback(const ErrorCallback& errorCallback);
|
||||
|
||||
/// Start the worker thread, used for background publishing
|
||||
void start();
|
||||
@ -67,8 +70,6 @@ namespace ix
|
||||
|
||||
/// Returns true only if we're connected
|
||||
bool isConnected() const;
|
||||
|
||||
void logError(const std::string& error);
|
||||
|
||||
private:
|
||||
bool sendHandshakeMessage();
|
||||
@ -83,8 +84,9 @@ namespace ix
|
||||
/// Invoke the traffic tracker callback
|
||||
static void invokeTrafficTrackerCallback(size_t size, bool incoming);
|
||||
|
||||
/// Invoke the authenticated callback
|
||||
/// Invoke lifecycle callbacks
|
||||
void invokeAuthenticatedCallback();
|
||||
void invokeErrorCallback(const std::string& errorMsg);
|
||||
|
||||
///
|
||||
/// Member variables
|
||||
@ -108,8 +110,9 @@ namespace ix
|
||||
/// Traffic tracker callback
|
||||
static OnTrafficTrackerCallback _onTrafficTrackerCallback;
|
||||
|
||||
/// Callback invoked when we are authenticated
|
||||
/// Callbacks
|
||||
AuthenticatedCallback _authenticatedCallback;
|
||||
ErrorCallback _errorCallback;
|
||||
|
||||
/// Subscription callbacks, only one per channel
|
||||
std::unordered_map<std::string, SubscriptionCallback> _cbs;
|
||||
|
3
examples/satori_publisher/events.jsonl
Normal file
3
examples/satori_publisher/events.jsonl
Normal file
@ -0,0 +1,3 @@
|
||||
{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Foo"}
|
||||
{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Bar"}
|
||||
{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Baz"}
|
@ -56,7 +56,7 @@ int main(int argc, char* argv[])
|
||||
std::ifstream f(path);
|
||||
if (!f.is_open())
|
||||
{
|
||||
std::cerr << "error while opening file" << std::endl;
|
||||
std::cerr << "Error while opening file: " << path << std::endl;
|
||||
}
|
||||
|
||||
while (getline(f, line))
|
||||
@ -70,16 +70,23 @@ int main(int argc, char* argv[])
|
||||
|
||||
if (f.bad())
|
||||
{
|
||||
std::cerr << "error while reading file" << std::endl;
|
||||
std::cerr << "Error while opening file: " << path << std::endl;
|
||||
}
|
||||
|
||||
done = true;
|
||||
}
|
||||
);
|
||||
satoriConnection.setErrorCallback(
|
||||
[&done](const std::string& errMsg)
|
||||
{
|
||||
std::cerr << "Satori Error received: " << errMsg << std::endl;
|
||||
done = true;
|
||||
}
|
||||
);
|
||||
|
||||
while (!done)
|
||||
{
|
||||
msleep(1000);
|
||||
msleep(10);
|
||||
}
|
||||
|
||||
std::cout << incomingBytes << std::endl;
|
||||
|
Reference in New Issue
Block a user