Repository navigation
Expose the location of each parse failure via rfl::Error::details() #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clockblocker
wants to merge
2
commits into
munich-data-engineering:main
Choose a base branch
from
clockblocker:structured-error-details
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+547
−58
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,36 @@ | ||
| #ifndef RFL_INTERNAL_ERROR_HPP_ | ||
| #define RFL_INTERNAL_ERROR_HPP_ | ||
|
|
||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| #include "cold.hpp" | ||
|
|
||
| namespace rfl::internal { | ||
|
|
||
| /// Describes a single failure contained in an Error: where it occurred and | ||
| /// what went wrong. | ||
| struct ErrorDetail { | ||
| /// A field name or an array index. | ||
| using PathSegment = std::variant<std::string, size_t>; | ||
|
|
||
| /// The location of the failure, starting from the root of the value being | ||
| /// read. Field names are spelled as they appear in the input, that is, after | ||
| /// processors such as rfl::SnakeCaseToCamelCase have been applied. An empty | ||
| /// path refers to the root value itself. | ||
| std::vector<PathSegment> path; | ||
|
|
||
| /// The message describing the failure, without the "Failed to parse field | ||
| /// '...': " prefixes that Error::what() contains for every level of nesting. | ||
| std::string message; | ||
|
|
||
| bool operator==(const ErrorDetail&) const = default; | ||
| }; | ||
|
|
||
| /// Defines the error class to be returned when something went wrong. | ||
| /// This is used throughout reflect-cpp for error handling. | ||
| class Error { | ||
|
|
@@ -20,12 +46,22 @@ class Error { | |
| /// @param _what The error message describing what went wrong (will be moved) | ||
| Error(std::string&& _what) : what_(std::move(_what)) {} | ||
|
|
||
| /// Constructs an error with a message and the individual failures it | ||
| /// summarizes. | ||
| /// @param _what The error message describing what went wrong | ||
| /// @param _details The individual failures, see details() | ||
| Error(std::string _what, std::vector<ErrorDetail> _details) | ||
| : what_(std::move(_what)), | ||
| details_(_details.empty() | ||
| ? nullptr | ||
| : new std::vector<ErrorDetail>(std::move(_details))) {} | ||
|
|
||
| /// Destructor. | ||
| ~Error() = default; | ||
|
|
||
| /// Copy constructor. | ||
| /// @param e The error to copy from | ||
| Error(const Error& e) = default; | ||
| Error(const Error& e) : what_(e.what_), details_(copy_details(e.details_)) {} | ||
|
|
||
| /// Move constructor. | ||
| /// @param e The error to move from | ||
|
|
@@ -34,7 +70,13 @@ class Error { | |
| /// Copy assignment operator. | ||
| /// @param _other The error to copy from | ||
| /// @return Reference to this error | ||
| Error& operator=(const Error& _other) = default; | ||
| Error& operator=(const Error& _other) { | ||
| if (this != &_other) { | ||
| what_ = _other.what_; | ||
| details_ = copy_details(_other.details_); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| /// Move assignment operator. | ||
| /// @param _other The error to move from | ||
|
|
@@ -47,9 +89,48 @@ class Error { | |
| /// from state | ||
| std::string what() && { return std::move(what_); } | ||
|
|
||
| /// Returns the individual failures summarized by what(), each with the | ||
| /// location at which it occurred. An error that was constructed from a | ||
| /// message only is reported as a single failure at the root. | ||
| std::vector<ErrorDetail> details() const& { | ||
| if (!details_) { | ||
| return {ErrorDetail{.path = {}, .message = what_}}; | ||
| } | ||
| return *details_; | ||
| } | ||
|
|
||
| /// Moves the individual failures out of the Error object and leaves it in a | ||
| /// moved from state. | ||
| std::vector<ErrorDetail> details() && { | ||
| if (!details_) { | ||
| return {ErrorDetail{.path = {}, .message = std::move(what_)}}; | ||
| } | ||
| return std::move(*details_); | ||
| } | ||
|
|
||
| private: | ||
| /// Errors are created and destroyed in many places, mostly as part of | ||
| /// rfl::Result. Deleting and copying the details out of line keeps that code | ||
| /// small, which matters for the performance of successful reads. | ||
| struct DeleteDetails { | ||
| RFL_COLD void operator()(std::vector<ErrorDetail>* _ptr) const { | ||
| delete _ptr; | ||
| } | ||
| }; | ||
|
|
||
| using DetailsPtr = std::unique_ptr<std::vector<ErrorDetail>, DeleteDetails>; | ||
|
|
||
| RFL_COLD static DetailsPtr copy_details(const DetailsPtr& _ptr) { | ||
| return _ptr ? DetailsPtr(new std::vector<ErrorDetail>(*_ptr)) : nullptr; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I mean...rfl::Box can handle that more elegantly. |
||
| } | ||
|
|
||
| private: | ||
| /// Documents what went wrong | ||
| std::string what_; | ||
|
|
||
| /// The individual failures summarized by what_. Null, unless what_ was | ||
| /// assembled from failures at nested locations. | ||
| DetailsPtr details_; | ||
| }; | ||
|
|
||
| } // namespace rfl::internal | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #ifndef RFL_INTERNAL_COLD_HPP_ | ||
| #define RFL_INTERNAL_COLD_HPP_ | ||
|
|
||
| /// Marks a function that is rarely called, such as one that only runs when | ||
| /// something went wrong. Such functions are kept out of the code of their | ||
| /// callers, so that they do not slow down the common case. | ||
| #if defined(_MSC_VER) | ||
| #define RFL_COLD __declspec(noinline) | ||
| #elif defined(__GNUC__) || defined(__clang__) | ||
| #define RFL_COLD __attribute__((noinline, cold)) | ||
| #else | ||
| #define RFL_COLD | ||
| #endif | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@clockblocker Could you use rfl::Box here? Clearly, the implicit assumption here is that DetailsPtr should not be NULL, so rfl::Box establishes that more clearly.
Moreover, if you use rfl::Box, you won't have to implement the copy operations by hand.