Skip to content

Commit 3d6c6e3

Browse files
committed
feat: improve error handling in Application and FeatureFactory with specific exceptions
1 parent d2dd8fb commit 3d6c6e3

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

cucumber_cpp/library/Application.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,7 @@ namespace cucumber_cpp::library
143143

144144
runCommand = cli.add_subcommand("run")->parse_complete_callback([this]
145145
{
146-
try
147-
{
148-
RunFeatures();
149-
}
150-
catch (std::exception& error)
151-
{
152-
std::cerr << error.what();
153-
}
146+
RunFeatures();
154147
});
155148

156149
runCommand->add_option("-t,--tag", options.tags, "Cucumber tag expression");
@@ -187,6 +180,12 @@ namespace cucumber_cpp::library
187180
std::cout << error.what() << std::endl;
188181
return GetExitCode(engine::Result::failed);
189182
}
183+
catch (const UnsupportedAsteriskError& error)
184+
{
185+
std::cout << "UnsupportedAsteriskError error:\n\n";
186+
std::cout << error.what() << std::endl;
187+
return GetExitCode(engine::Result::failed);
188+
}
190189
catch (const cucumber_expression::Error& error)
191190
{
192191
std::cout << "Cucumber Expression error:\n\n";
@@ -199,6 +198,11 @@ namespace cucumber_cpp::library
199198
std::cout << error.what() << std::endl;
200199
return GetExitCode(engine::Result::failed);
201200
}
201+
catch (...)
202+
{
203+
std::cout << "Unknown error";
204+
return GetExitCode(engine::Result::failed);
205+
}
202206

203207
return GetExitCode();
204208
}

cucumber_cpp/library/InternalError.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ namespace cucumber_cpp::library
1616

1717
const std::source_location sourceLocation;
1818
};
19+
20+
struct UnsupportedAsteriskError : std::runtime_error
21+
{
22+
UnsupportedAsteriskError(const std::string& str)
23+
: std::runtime_error{ str }
24+
{}
25+
};
1926
}
2027

2128
#endif

cucumber_cpp/library/engine/FeatureFactory.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "cucumber/messages/scenario.hpp"
1414
#include "cucumber/messages/step.hpp"
1515
#include "cucumber/messages/tag.hpp"
16+
#include "cucumber_cpp/library/InternalError.hpp"
1617
#include "cucumber_cpp/library/StepRegistry.hpp"
1718
#include "cucumber_cpp/library/TagExpression.hpp"
1819
#include "cucumber_cpp/library/engine/FeatureInfo.hpp"
@@ -24,12 +25,14 @@
2425
#include <algorithm>
2526
#include <cstddef>
2627
#include <filesystem>
28+
#include <format>
2729
#include <functional>
2830
#include <map>
2931
#include <memory>
3032
#include <optional>
3133
#include <ranges>
3234
#include <set>
35+
#include <stdexcept>
3336
#include <string>
3437
#include <string_view>
3538
#include <utility>
@@ -149,7 +152,15 @@ namespace cucumber_cpp::library::engine
149152
{
150153
auto table = TableFactory(pickleStep.argument);
151154

152-
scenarioInfo.Children().push_back(featureTreeFactory.CreateStepInfo(stepTypeLut.at(*pickleStep.type), pickleStep.text, scenarioInfo, step.location.line, step.location.column.value_or(0), std::move(table)));
155+
try
156+
{
157+
scenarioInfo.Children().push_back(featureTreeFactory.CreateStepInfo(stepTypeLut.at(*pickleStep.type), pickleStep.text, scenarioInfo, step.location.line, step.location.column.value_or(0), std::move(table)));
158+
}
159+
catch (const std::out_of_range& e)
160+
{
161+
162+
throw UnsupportedAsteriskError{ std::format("{}:{}: * steps are not supported", scenarioInfo.FeatureInfo().Path().c_str(), step.location.line) };
163+
}
153164
}
154165

155166
void ConstructSteps(const FeatureTreeFactory& featureTreeFactory, ScenarioInfo& scenarioInfo, const FlatAst& flatAst, const std::vector<cucumber::messages::pickle_step>& pickleSteps)

0 commit comments

Comments
 (0)