Skip to content

Commit

Permalink
failure_category
Browse files Browse the repository at this point in the history
  • Loading branch information
log0div0 committed Jan 23, 2022
1 parent c474ad3 commit 6395eb2
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 58 deletions.
15 changes: 15 additions & 0 deletions src/testo/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ std::ostream& operator<<(std::ostream& stream, const std::exception& error) {
backtrace(stream, error);
return stream;
}

std::string GetFailureCategory(const std::exception& error) {
// exception with category should be on the bottom of the stack
try {
std::rethrow_if_nested(error);
const ExceptionWithCategory* exception = dynamic_cast<const ExceptionWithCategory*>(&error);
if (exception) {
return exception->failure_category;
} else {
return {};
}
} catch (const std::exception& error) {
return GetFailureCategory(error);
}
}
9 changes: 9 additions & 0 deletions src/testo/Exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ struct TestFailedException: Exception {
TestFailedException(): Exception("At least one of the tests failed") {}
};

struct ExceptionWithCategory: Exception {
ExceptionWithCategory(const std::string& message, const std::string& failure_category_):
Exception(message), failure_category(failure_category_) {}

std::string failure_category;
};

struct ContinueError: std::runtime_error {
using std::runtime_error::runtime_error;
};

std::ostream& operator<<(std::ostream& stream, const std::exception& error);

std::string GetFailureCategory(const std::exception& error);
1 change: 1 addition & 0 deletions src/testo/IR/Test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct TestRun {

std::string failure_message;
std::string failure_stacktrace;
std::string failure_category;
};

}
7 changes: 4 additions & 3 deletions src/testo/NNClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ nlohmann::json NNClient::receive_response() {
nlohmann::json response = channel->recv();
std::string type = response.at("type");
if (type == ERROR_RESPONSE) {
std::string message = response.at("data").get<std::string>();
throw std::runtime_error(message);
std::string message = response.at("data");
std::string failure_category = response.at("failure_category");
throw ExceptionWithCategory(message, failure_category);
} else if (type == CONTINUE_ERROR_RESPONSE) {
std::string message = response.at("data").get<std::string>();
std::string message = response.at("data");
throw ContinueError(message);
}
return response;
Expand Down
5 changes: 4 additions & 1 deletion src/testo/ReportWriterNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ void ReportWriterNative::test_end() {
{"test_name", current_test_run->test->name()},
{"exec_status", to_string(current_test_run->exec_status)},
{"start_timestamp", to_string(current_test_run->start_timestamp)},
{"stop_timestamp", to_string(current_test_run->stop_timestamp)}
{"stop_timestamp", to_string(current_test_run->stop_timestamp)},
{"failure_message", current_test_run->failure_message},
{"failure_stacktrace", current_test_run->failure_stacktrace},
{"failure_category", current_test_run->failure_category},
};
file << current_test_run_meta.dump(2);
current_test_run = nullptr;
Expand Down
3 changes: 2 additions & 1 deletion src/testo/Reporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ void Reporter::test_passed() {
++current_test_run_index;
}

void Reporter::test_failed(const std::string& message, const std::string& stacktrace) {
void Reporter::test_failed(const std::string& message, const std::string& stacktrace, const std::string& failure_category) {
report_raw(fmt::format("{}", stacktrace), red, true);

current_test_run->failure_message = message;
current_test_run->failure_stacktrace = stacktrace;
current_test_run->failure_category = failure_category;
current_test_run->stop_timestamp = std::chrono::system_clock::now();
current_test_run->exec_status = IR::TestRun::ExecStatus::Failed;

Expand Down
2 changes: 1 addition & 1 deletion src/testo/Reporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Reporter {
void prepare_environment();
void run_test();
void test_passed();
void test_failed(const std::string& message, const std::string& stacktrace);
void test_failed(const std::string& message, const std::string& stacktrace, const std::string& failure_category);

void print_statistics();

Expand Down
4 changes: 3 additions & 1 deletion src/testo/VisitorInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ void VisitorInterpreter::visit_test(std::shared_ptr<IR::Test> test) {
ss << std::string(current_controller->ast_node->begin()) << ": note: the " << current_controller->type() << " " << current_controller->name() << " was declared here\n\n";
}

reporter.test_failed(error.what(), ss.str());
std::string failure_category = GetFailureCategory(error);

reporter.test_failed(error.what(), ss.str(), failure_category);

if (stop_on_fail) {
throw std::runtime_error("");
Expand Down
17 changes: 17 additions & 0 deletions src/testo_nn_server/Exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include "Exceptions.hpp"

std::string GetFailureCategory(const std::exception& error) {
// exception with category should be on the bottom of the stack
try {
std::rethrow_if_nested(error);
const ExceptionWithCategory* exception = dynamic_cast<const ExceptionWithCategory*>(&error);
if (exception) {
return exception->failure_category;
} else {
return {};
}
} catch (const std::exception& error) {
return GetFailureCategory(error);
}
}
21 changes: 21 additions & 0 deletions src/testo_nn_server/Exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#pragma once

#include <stdexcept>

struct ExceptionWithCategory: std::runtime_error {
ExceptionWithCategory(const std::string& message, const std::string& failure_category_):
std::runtime_error(message), failure_category(failure_category_) {}

std::string failure_category;
};

struct LogicError: ExceptionWithCategory {
LogicError(const std::string& message): ExceptionWithCategory(message, "logic_error") {}
};

struct ContinueError: std::runtime_error {
using std::runtime_error::runtime_error;
};

std::string GetFailureCategory(const std::exception& error);
8 changes: 3 additions & 5 deletions src/testo_nn_server/MessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ void MessageHandler::run() {
response = handle_request(request);
} catch (const std::system_error& error) {
throw;
} catch (const nn::ContinueError& continue_error) {
} catch (const ContinueError& continue_error) {
response = create_continue_error_response(continue_error.what());
} catch (const std::exception& error) {
if (request.count("image")) {
request["image"] = "omitted";
}
spdlog::error("Error while processing request \n{}:\n{}", request.dump(4), error.what());
response = create_error_response(error.what());
}
if (!response.empty()) {
channel->send(response);
response = create_error_response(error.what(), GetFailureCategory(error));
}
channel->send(response);
}
}

Expand Down
21 changes: 20 additions & 1 deletion src/testo_nn_server/js/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ Value ContextRef::eval(const std::string& script, bool compile_only) {
Value exception_val = get_exception();
if (exception_val.is_instance_of(get_global_object().get_property_str("ContinueError"))) {
std::string message = exception_val.get_property_str("message");
throw nn::ContinueError(message);
throw ContinueError(message);
} else if (exception_val.is_instance_of(get_global_object().get_property_str("ExceptionWithCategory"))) {
std::string message = exception_val.get_property_str("message");
std::string failure_category = exception_val.get_property_str("failure_category");
throw ExceptionWithCategory(message, failure_category);
} else {
std::string message;

Expand Down Expand Up @@ -136,6 +140,10 @@ Value ContextRef::new_continue_error(const std::string& message) {
return call_constructor(get_global_object().get_property_str("ContinueError"), {new_string(message)});
}

Value ContextRef::new_exception_with_category(const std::string& message, const std::string& failure_category) {
return call_constructor(get_global_object().get_property_str("ExceptionWithCategory"), {new_string(message), new_string(failure_category)});
}

void ContextRef::set_class_proto(JSClassID class_id, Value obj) {
JS_SetClassProto(handle, class_id, obj.release());
}
Expand Down Expand Up @@ -200,6 +208,17 @@ Context::Context(const stb::Image<stb::RGB>* image, ContextEnv* env): ContextRef
}
ContinueError.prototype = Object.create(Error.prototype);
function ExceptionWithCategory(message, failure_category) {
if (!new.target) {
return new ExceptionWithCategory(message. failure_category);
}
this.name = "ExceptionWithCategory";
this.message = message;
this.failure_category = failure_category;
}
ExceptionWithCategory.prototype = Object.create(Error.prototype);
)");
}

Expand Down
1 change: 1 addition & 0 deletions src/testo_nn_server/js/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct ContextRef {
Value new_object();
Value new_object_class(int class_id);
Value new_continue_error(const std::string& message);
Value new_exception_with_category(const std::string& message, const std::string& failure_category);

void set_class_proto(JSClassID class_id, Value obj);

Expand Down
53 changes: 24 additions & 29 deletions src/testo_nn_server/js/FunctionsAdapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,51 @@ typedef Value JSCPPFunction(ContextRef ctx, const ValueRef this_val, const std::
typedef Value JSCPPGetter(ContextRef ctx, ValueRef this_val);
typedef Value JSCPPSetter(ContextRef ctx, ValueRef this_val, ValueRef val);

template <JSCPPFunction F>
JSValue Func(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
template <typename Fn>
JSValue FuncWrapper(JSContext *ctx, Fn&& fn) {
try {
std::vector<ValueRef> args;
for (int i = 0; i < argc; ++i) {
args.push_back(ValueRef(argv[i], ctx));
}
Value result = F(ctx, ValueRef(this_val, ctx), args);
return result.release();
} catch (const nn::ContinueError& error) {
return fn();
} catch (const ContinueError& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_continue_error(error.what()));
return exception.release();
} catch (const ExceptionWithCategory& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_exception_with_category(error.what(), error.failure_category));
return exception.release();
} catch (const std::exception& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_string(error.what()));
return exception.release();
}
}

template <JSCPPFunction F>
JSValue Func(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
return FuncWrapper(ctx, [&] {
std::vector<ValueRef> args;
for (int i = 0; i < argc; ++i) {
args.push_back(ValueRef(argv[i], ctx));
}
Value result = F(ctx, ValueRef(this_val, ctx), args);
return result.release();
});
}

template <JSCPPGetter F>
JSValue Func(JSContext* ctx, JSValueConst this_val) {
try {
return FuncWrapper(ctx, [&] {
Value result = F(ctx, ValueRef(this_val, ctx));
return result.release();
} catch (const nn::ContinueError& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_continue_error(error.what()));
return exception.release();
} catch (const std::exception& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_string(error.what()));
return exception.release();
}
});
}

template <JSCPPSetter F>
JSValue Func(JSContext* ctx, JSValueConst this_val, JSValueConst val) {
try {
return FuncWrapper(ctx, [&] {
Value result = F(ctx, ValueRef(this_val, ctx), ValueRef(val, ctx));
return result.release();
} catch (const nn::ContinueError& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_continue_error(error.what()));
return exception.release();
} catch (const std::exception& error) {
ContextRef ctx_ref(ctx);
Value exception = ctx_ref.throw_(ctx_ref.new_string(error.what()));
return exception.release();
}
});
}

template <JSCPPFunction func>
Expand Down
9 changes: 7 additions & 2 deletions src/testo_nn_server/js/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ int main(int argc, char** argv) {
auto val = js_ctx.eval(script);

return 0;
} catch (const nn::ContinueError& error) {
std::cerr << "C++ ContinueError!" << std::endl;
} catch (const ContinueError& error) {
std::cerr << "ContinueError!" << std::endl;
std::cerr << error.what() << std::endl;
} catch (const ExceptionWithCategory& error) {
std::cerr << "ExceptionWithCategory!" << std::endl;
std::cerr << error.what() << std::endl;
std::cerr << error.failure_category << std::endl;
} catch (const std::exception& error) {
std::cerr << "std::exception!" << std::endl;
std::cerr << error << std::endl;
}
}
11 changes: 1 addition & 10 deletions src/testo_nn_server/nn/Tensor.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@

#pragma once

#include <stdexcept>
#include <vector>
#include <algorithm>

#include "nlohmann/json.hpp"

#include "Point.hpp"

#include "../Exceptions.hpp"

namespace nn {

struct ContinueError: std::runtime_error {
using std::runtime_error::runtime_error;
};

struct LogicError: std::runtime_error {
using std::runtime_error::runtime_error;
};

template <typename Object>
struct Tensor {
std::vector<Object> objects;
Expand Down
7 changes: 3 additions & 4 deletions src/testo_nn_server_protocol/Messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,18 @@ inline nlohmann::json create_js_validate_response(bool success, const std::strin

// errors

inline nlohmann::json create_error_response(const std::string& message, const std::string& _stdout = {}) {
inline nlohmann::json create_error_response(const std::string& message, const std::string& failure_category) {
return {
{"type", ERROR_RESPONSE},
{"data", message},
{"stdout", _stdout},
{"failure_category", failure_category},
};
}

inline nlohmann::json create_continue_error_response(const std::string& message, const std::string& _stdout = {}) {
inline nlohmann::json create_continue_error_response(const std::string& message) {
return {
{"type", CONTINUE_ERROR_RESPONSE},
{"data", message},
{"stdout", _stdout},
};
}

Expand Down

0 comments on commit 6395eb2

Please sign in to comment.