Added socket write verification to bolt buffer.

Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D199
This commit is contained in:
Matej Ferencevic 2017-04-07 09:52:19 +02:00
parent 72f7b64c8f
commit 22dfe61fe1
3 changed files with 41 additions and 7 deletions

View File

@ -0,0 +1,11 @@
#pragma once
#include "utils/exceptions/basic_exception.hpp"
namespace communication::bolt {
class BoltException : public BasicException {
public:
using BasicException::BasicException;
};
}

View File

@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include "communication/bolt/v1/bolt_exception.hpp"
#include "communication/bolt/v1/constants.hpp"
#include "logging/loggable.hpp"
#include "utils/bswap.hpp"
@ -112,7 +113,8 @@ class ChunkedEncoderBuffer : public Loggable {
if (size_ == 0) return;
// Flush the whole buffer.
socket_.Write(buffer_.data(), size_);
bool written = socket_.Write(buffer_.data(), size_);
if (!written) throw BoltException("Socket write failed!");
logger.trace("Flushed {} bytes.", size_);
// Cleanup.

View File

@ -2,6 +2,7 @@
#include <string>
#include "communication/bolt/v1/bolt_exception.hpp"
#include "communication/bolt/v1/messaging/codes.hpp"
#include "communication/bolt/v1/state.hpp"
#include "logging/default.hpp"
@ -14,6 +15,17 @@ struct Query {
std::string statement;
};
template <typename Session>
void StateExecutorFailure(Session &session, Logger &logger,
const std::map<std::string, TypedValue> &metadata) {
try {
session.encoder_.MessageFailure(metadata);
} catch (const BoltException &e) {
logger.debug("MessageFailure failed because: {}", e.what());
session.Close();
}
}
/**
* TODO (mferencevic): finish & document
*/
@ -41,7 +53,8 @@ State StateExecutorRun(Session &session) {
if (!is_successfully_executed) {
db_accessor->abort();
session.encoder_.MessageFailure(
StateExecutorFailure<Session>(
session, logger,
{{"code", "Memgraph.QueryExecutionFail"},
{"message",
"Query execution has failed (probably there is no "
@ -57,23 +70,31 @@ State StateExecutorRun(Session &session) {
// !! QUERY ENGINE -> RUN METHOD -> EXCEPTION HANDLING !!
} catch (const query::SyntaxException &e) {
db_accessor->abort();
session.encoder_.MessageFailure(
StateExecutorFailure<Session>(
session, logger,
{{"code", "Memgraph.SyntaxException"}, {"message", "Syntax error"}});
return ERROR;
} catch (const query::QueryEngineException &e) {
db_accessor->abort();
session.encoder_.MessageFailure(
StateExecutorFailure<Session>(
session, logger,
{{"code", "Memgraph.QueryEngineException"},
{"message", "Query engine was unable to execute the query"}});
return ERROR;
} catch (const StacktraceException &e) {
db_accessor->abort();
session.encoder_.MessageFailure({{"code", "Memgraph.StacktraceException"},
StateExecutorFailure<Session>(session, logger,
{{"code", "Memgraph.StacktraceException"},
{"message", "Unknown exception"}});
return ERROR;
} catch (const BoltException &e) {
db_accessor->abort();
logger.debug("Failed because: {}", e.what());
session.Close();
} catch (std::exception &e) {
db_accessor->abort();
session.encoder_.MessageFailure(
StateExecutorFailure<Session>(
session, logger,
{{"code", "Memgraph.Exception"}, {"message", "Unknown exception"}});
return ERROR;
}