Skip to content

Commit

Permalink
feat: added InternalError with SourceLocation (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
daantimmer authored Jan 26, 2024
1 parent 051764f commit 830ff6d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cucumber-cpp/Body.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef CUCUMBER_CPP_BODY_HPP
#define CUCUMBER_CPP_BODY_HPP

#include "cucumber-cpp/InternalError.hpp"
#include <cstddef>
#include <source_location>
#include <sstream>
#include <stdexcept>
#include <string>
Expand All @@ -12,20 +14,18 @@
namespace cucumber_cpp
{
template<class To>
inline To StringTo(const std::string& s)
inline To StringTo(const std::string& s, std::source_location sourceLocation = std::source_location::current())
{
std::istringstream stream{ s };
To to;
stream >> to;
if (stream.fail())
{
throw std::invalid_argument("Cannot convert parameter");
}
throw InternalError{ "Cannnot convert parameter \"" + s + "\"", sourceLocation };
return to;
}

template<>
inline std::string StringTo<std::string>(const std::string& s)
inline std::string StringTo<std::string>(const std::string& s, std::source_location /*sourceLocation*/)
{
return s;
}
Expand Down
1 change: 1 addition & 0 deletions cucumber-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(cucumber-cpp PRIVATE
Hooks.hpp
HookScopes.cpp
HookScopes.hpp
InternalError.hpp
OnTestPartResultEventListener.cpp
OnTestPartResultEventListener.hpp
Rtrim.cpp
Expand Down
20 changes: 20 additions & 0 deletions cucumber-cpp/InternalError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CUCUMBER_CPP_INTERNALERROR_HPP
#define CUCUMBER_CPP_INTERNALERROR_HPP

#include <source_location>
#include <stdexcept>

namespace cucumber_cpp
{
struct InternalError : std::runtime_error
{
InternalError(std::string str, std::source_location sourceLocation = std::source_location::current())
: std::runtime_error{ std::move(str) }
, sourceLocation{ sourceLocation }
{}

const std::source_location sourceLocation;
};
}

#endif
6 changes: 3 additions & 3 deletions cucumber-cpp/StepRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace cucumber_cpp
struct TableValue
{
template<class T>
T As() const;
T As(std::source_location sourceLocation = std::source_location::current()) const;

std::string value;
};
Expand Down Expand Up @@ -153,9 +153,9 @@ namespace cucumber_cpp
//////////////////////////

template<class T>
T TableValue::As() const
T TableValue::As(std::source_location sourceLocation) const
{
return StringTo<T>(value);
return StringTo<T>(value, sourceLocation);
}

template<class T>
Expand Down
6 changes: 6 additions & 0 deletions cucumber-cpp/StepRunner.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cucumber-cpp/StepRunner.hpp"
#include "cucumber-cpp/HookScopes.hpp"
#include "cucumber-cpp/InternalError.hpp"
#include "cucumber-cpp/OnTestPartResultEventListener.hpp"
#include "cucumber-cpp/Rtrim.hpp"
#include "cucumber-cpp/StepRegistry.hpp"
Expand Down Expand Up @@ -254,6 +255,11 @@ namespace cucumber_cpp
result = decltype(result)::pending;
ReportHandler().Error(e.message, e.sourceLocation.file_name(), e.sourceLocation.line(), e.sourceLocation.column());
}
catch (const InternalError& e)
{
result = decltype(result)::error;
ReportHandler().Error(e.what(), e.sourceLocation.file_name(), e.sourceLocation.line(), e.sourceLocation.column());
}
catch (const std::source_location& loc)
{
result = decltype(result)::error;
Expand Down

0 comments on commit 830ff6d

Please sign in to comment.