#pragma once
#include "transcribe-log.h"
#include "transcribe.h"
#include <exception>
#include <new>
namespace transcribe {
template <typename Fn> transcribe_status api_guard_status(const char * fn_name, Fn && body) {
try {
return body();
} catch (const std::bad_alloc &) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "%s: out of memory", fn_name);
return TRANSCRIBE_ERR_OOM;
} catch (const std::exception & e) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "%s: caught backend exception: %s", fn_name, e.what());
return TRANSCRIBE_ERR_BACKEND;
} catch (...) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "%s: caught unknown backend exception", fn_name);
return TRANSCRIBE_ERR_BACKEND;
}
}
template <typename T, typename Fn> T api_guard_value(const char * fn_name, T on_throw, Fn && body) {
try {
return body();
} catch (const std::exception & e) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "%s: caught backend exception: %s", fn_name, e.what());
return on_throw;
} catch (...) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_ERROR, "%s: caught unknown backend exception", fn_name);
return on_throw;
}
}
template <typename Fn> void api_guard_void(const char * fn_name, Fn && body) {
try {
body();
} catch (const std::exception & e) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_WARN, "%s: caught backend exception during teardown: %s", fn_name,
e.what());
} catch (...) {
transcribe::log_msg(TRANSCRIBE_LOG_LEVEL_WARN, "%s: caught unknown backend exception during teardown", fn_name);
}
}
}