DocWire SDK
DocWire SDK: Deterministic, auditable, and secure data processing in C++20. On-premise by default. No compromises on computational speed.
Loading...
Searching...
No Matches
handling_errors_and_warnings.cpp

This example presents how to catch exceptions to handle critical errors and how to use transformer to process non-critical errors (warnings) flowing through the pipeline

#define DOCWIRE_ENABLE_SHORT_MACRO_NAMES
#include "docwire.h"
#include "error_tags.h"
#include "error.h"
#include "make_error.h"
#include "contains_type.h"
#include <filesystem>
#include <iostream>
#include <sstream>
void retry()
{
// Simulated retry logic for demonstration purposes.
}
void demonstrate_error_handling()
{
using namespace docwire;
// Snippet 1: categorized and tagged errors
try
{
throw make_error(errors::network_failure{});
}
catch (const docwire::errors::base& e)
{
retry();
}
// Snippet 2: embedded source location
try
{
throw make_error("Demonstration error");
}
catch (const docwire::errors::base& e)
{
std::cerr << e.location.file_name() << ":" << e.location.line() << std::endl;
}
// Snippet 3: secure context retrieval
try
{
throw make_error(std::filesystem::path{"/secret/file.txt"});
}
{
std::cerr << docwire::errors::diagnostic_message(e) << std::endl;
// or
if (auto* impl_ptr = dynamic_cast<docwire::errors::impl<std::filesystem::path>*>(&e))
{
auto fn = impl_ptr->context;
(void)fn; // avoid unused variable warning
}
}
}
int main(int argc, char* argv[])
{
using namespace docwire;
std::stringstream out_stream;
try
{
std::filesystem::path("data_processing_definition.doc") |
[](message_ptr msg, const message_callbacks& emit_message)
{
if (msg->is<std::exception_ptr>())
{
std::clog << "[WARNING] " <<
errors::diagnostic_message(msg->get<std::exception_ptr>()) <<
std::endl;
}
return emit_message(std::move(msg));
} |
out_stream;
}
catch (const std::exception& e)
{
std::cerr << "[ERROR] " << errors::diagnostic_message(e) << std::endl;
return 1;
}
demonstrate_error_handling();
return 0;
}
A composite parser handling various office document formats.
Exports data to plain text format.
std::string diagnostic_message(const std::exception &e)
Generates a diagnostic message for the given nested exceptions chain.
bool contains_type(const std::exception &e)
Checks if the given nested exceptions chain contains a specific type of context.
The main namespace for the DocWire SDK.
Definition ai_elements.h:19
Base class for all exceptions in the SDK.
Definition error.h:76
source_location location
The source location where the exception was thrown.
Definition error.h:78
Implementation of the error class for a variadic number of context items.
Definition error.h:168
Network failure error tag.
Definition error_tags.h:112
Callback bundle used by pipeline elements to emit messages.
Definition message.h:92