Added default diagram generation error for empty diagrams (#246)
This commit is contained in:
@@ -95,11 +95,17 @@ void generate_diagram_select_generator(const std::string &od,
|
||||
using diagram_generator =
|
||||
typename diagram_generator_t<DiagramConfig, GeneratorTag>::type;
|
||||
|
||||
std::stringstream buffer;
|
||||
buffer << diagram_generator(
|
||||
dynamic_cast<DiagramConfig &>(*diagram), *model);
|
||||
|
||||
// Only open the file after the diagram has been generated successfully
|
||||
// in order not to overwrite previous diagram in case of failure
|
||||
auto path = std::filesystem::path{od} /
|
||||
fmt::format("{}.{}", name, GeneratorTag::extension);
|
||||
std::ofstream ofs;
|
||||
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
|
||||
ofs << diagram_generator(dynamic_cast<DiagramConfig &>(*diagram), *model);
|
||||
ofs << buffer.str();
|
||||
|
||||
ofs.close();
|
||||
|
||||
@@ -258,11 +264,12 @@ void generate_diagrams(const std::vector<std::string> &diagram_names,
|
||||
if (indicator)
|
||||
indicator->complete(name);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
catch (const std::exception &e) {
|
||||
if (indicator)
|
||||
indicator->fail(name);
|
||||
|
||||
LOG_ERROR(e.what());
|
||||
LOG_ERROR(
|
||||
"ERROR: Failed to generate diagram {}: {}", name, e.what());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -107,11 +107,19 @@ std::ostream &operator<<(
|
||||
template <typename C, typename D>
|
||||
void generator<C, D>::generate(std::ostream &ostr) const
|
||||
{
|
||||
const auto &config = generators::generator<C, D>::config();
|
||||
const auto &model = generators::generator<C, D>::model();
|
||||
|
||||
if (!config.allow_empty_diagrams() && model.is_empty()) {
|
||||
throw clanguml::error::empty_diagram_error{
|
||||
"Diagram configuration resulted in empty diagram."};
|
||||
}
|
||||
|
||||
nlohmann::json j;
|
||||
j["name"] = generators::generator<C, D>::model().name();
|
||||
j["diagram_type"] = to_string(generators::generator<C, D>::model().type());
|
||||
if (generators::generator<C, D>::config().title) {
|
||||
j["title"] = generators::generator<C, D>::config().title();
|
||||
j["name"] = model.name();
|
||||
j["diagram_type"] = to_string(model.type());
|
||||
if (config.title) {
|
||||
j["title"] = config.title();
|
||||
}
|
||||
|
||||
generate_diagram(j);
|
||||
|
||||
@@ -250,6 +250,13 @@ template <typename C, typename D>
|
||||
void generator<C, D>::generate(std::ostream &ostr) const
|
||||
{
|
||||
const auto &config = generators::generator<C, D>::config();
|
||||
const auto &model = generators::generator<C, D>::model();
|
||||
|
||||
if (!config.allow_empty_diagrams() && model.is_empty() &&
|
||||
config.mermaid().before.empty() && config.mermaid().after.empty()) {
|
||||
throw clanguml::error::empty_diagram_error{
|
||||
"Diagram configuration resulted in empty diagram."};
|
||||
}
|
||||
|
||||
update_context();
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "common/model/diagram_filter.h"
|
||||
#include "common/model/relationship.h"
|
||||
#include "config/config.h"
|
||||
#include "error.h"
|
||||
#include "util/error.h"
|
||||
#include "util/util.h"
|
||||
#include "version.h"
|
||||
@@ -265,9 +266,16 @@ template <typename C, typename D>
|
||||
void generator<C, D>::generate(std::ostream &ostr) const
|
||||
{
|
||||
const auto &config = generators::generator<C, D>::config();
|
||||
const auto &model = generators::generator<C, D>::model();
|
||||
|
||||
update_context();
|
||||
|
||||
if (!config.allow_empty_diagrams() && model.is_empty() &&
|
||||
config.puml().before.empty() && config.puml().after.empty()) {
|
||||
throw clanguml::error::empty_diagram_error{
|
||||
"Diagram configuration resulted in empty diagram."};
|
||||
}
|
||||
|
||||
ostr << "@startuml" << '\n';
|
||||
|
||||
generate_title(ostr);
|
||||
|
||||
@@ -164,6 +164,13 @@ public:
|
||||
*/
|
||||
virtual inja::json context() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check whether the diagram is empty
|
||||
*
|
||||
* @return True, if diagram is empty
|
||||
*/
|
||||
virtual bool is_empty() const = 0;
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::unique_ptr<diagram_filter> filter_;
|
||||
|
||||
@@ -69,6 +69,13 @@ public:
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check whether the element view is empty
|
||||
*
|
||||
* @return True, if the view does not contain any elements
|
||||
*/
|
||||
bool is_empty() const { return elements_.empty(); }
|
||||
|
||||
private:
|
||||
reference_vector<T> elements_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user