Skip to content

Commit

Permalink
refactor: action: use more appropriate new QueryEvaluationException
Browse files Browse the repository at this point in the history
  • Loading branch information
pflanze committed Jul 24, 2024
1 parent f9347b0 commit 4033336
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
20 changes: 19 additions & 1 deletion include/silo/query_engine/query_parse_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@

namespace silo {

class [[maybe_unused]] QueryParseException : public std::runtime_error {
class [[maybe_unused]] QueryException : public std::runtime_error {
protected:
explicit QueryException(const std::string& error_message);

public:
/// A short string describing the phase or similar of query
/// exception.
[[nodiscard]] virtual std::string_view duringString() const = 0;
};

class [[maybe_unused]] QueryParseException : public QueryException {
public:
[[maybe_unused]] explicit QueryParseException(const std::string& error_message);
[[nodiscard]] std::string_view duringString() const override;
};

class [[maybe_unused]] QueryEvaluationException : public QueryException {
public:
[[maybe_unused]] explicit QueryEvaluationException(const std::string& error_message);
[[nodiscard]] std::string_view duringString() const override;
};

} // namespace silo
4 changes: 2 additions & 2 deletions src/silo/query_engine/actions/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ QueryResult Action::executeAndOrder(
// query syntax but with its execution. But this will disappear
// from the code base again soon.
auto error = [&](const std::string_view& what) {
throw silo::QueryParseException(fmt::format(
"{} not supported for streaming endpoints when receiving more than {} rows, but got "
throw silo::QueryEvaluationException(fmt::format(
"{} not supported for streaming endpoints when returning more than {} rows, but got "
"{}+",
what,
MATERIALIZATION_CUTOFF,
Expand Down
21 changes: 19 additions & 2 deletions src/silo/query_engine/query_parse_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
#include <string>

namespace silo {
[[maybe_unused]] QueryParseException::QueryParseException(const std::string& error_message)

QueryException::QueryException(const std::string& error_message)
: std::runtime_error(error_message.c_str()) {}
} // namespace silo

[[maybe_unused]] QueryParseException::QueryParseException(const std::string& error_message)
: QueryException(error_message) {}

std::string_view QueryParseException::duringString() const {
return "parsing";
}

[[maybe_unused]] QueryEvaluationException::QueryEvaluationException(const std::string& error_message
)
: QueryException(error_message) {}

std::string_view QueryEvaluationException::duringString() const {
return "evaluation";
}

} // namespace silo
6 changes: 4 additions & 2 deletions src/silo_api/query_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ void QueryHandler::post(
);
}
}
} catch (const silo::QueryParseException& ex) {
} catch (const silo::QueryException& ex) {
response.setContentType("application/json");
SPDLOG_INFO("Query is invalid: " + query + " - exception: " + ex.what());
SPDLOG_INFO(
"Query is invalid: {} - exception during {}: {}", query, ex.duringString(), ex.what()
);
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
std::ostream& out_stream = response.send();
out_stream << nlohmann::json(ErrorResponse{.error = "Bad request", .message = ex.what()});
Expand Down

0 comments on commit 4033336

Please sign in to comment.