Vince's CSV Parser
Loading...
Searching...
No Matches
csv_error.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "common.hpp"
6#include <cstring>
7#include <memory>
8#include <string>
9#include <system_error>
10#include <utility>
11
12namespace csv {
23 enum class CSVConversionError {
25 None = 0,
26
29
32
35
38 };
39
40 namespace internals {
41 typedef const char* csv_error_message;
42
43 static CONSTEXPR_VALUE_14 csv_error_message CSV_CONVERSION_ERROR_MESSAGES[] = {
44 "",
45 "Not a number.",
46 "Overflow error.",
47 "Attempted to convert a floating point value to an integral type.",
48 "Negative numbers cannot be converted to unsigned types."
49 };
50 }
51
53 inline const char* csv_conversion_error_message(CSVConversionError error) noexcept {
54 const size_t index = static_cast<size_t>(error);
55 return index < (sizeof(internals::CSV_CONVERSION_ERROR_MESSAGES) / sizeof(internals::CSV_CONVERSION_ERROR_MESSAGES[0]))
56 ? internals::CSV_CONVERSION_ERROR_MESSAGES[index]
57 : internals::CSV_CONVERSION_ERROR_MESSAGES[static_cast<size_t>(CSVConversionError::NotANumber)];
58 }
59
86
94 struct CSVError {
96 std::error_code system;
99
100 CSVError() = default;
102 CSVError(CSVErrorCode value, csv::string_view message) : code(value) {
103 this->set_context(message);
104 }
105 CSVError(const CSVError& other)
106 : code(other.code), system(other.system), context(other.context) {
107 if (other.owned_context_) this->set_context(other.context);
108 }
109 CSVError(CSVError&& other) noexcept
110 : code(other.code), system(other.system), context(other.context),
111 owned_context_(std::move(other.owned_context_)) {
112 other.clear();
113 }
114 CSVError& operator=(const CSVError& other) {
115 if (this != &other) {
116 CSVError copy(other);
117 *this = std::move(copy);
118 }
119 return *this;
120 }
121 CSVError& operator=(CSVError&& other) noexcept {
122 if (this != &other) {
123 code = other.code;
124 system = other.system;
125 context = other.context;
126 owned_context_ = std::move(other.owned_context_);
127 other.clear();
128 }
129 return *this;
130 }
131
133 static CSVError from_static(CSVErrorCode value, csv::string_view message) noexcept {
134 CSVError error;
135 error.code = value;
136 error.context = message;
137 return error;
138 }
139
142 std::unique_ptr<char[]> buffer;
143 if (!message.empty()) {
144 buffer.reset(new char[message.size()]);
145 std::memcpy(buffer.get(), message.data(), message.size());
146 }
147 context = message.empty() ? csv::string_view()
148 : csv::string_view(buffer.get(), message.size());
149 owned_context_ = std::move(buffer);
150 }
151 explicit operator bool() const noexcept { return code != CSVErrorCode::None; }
152 void clear() noexcept {
153 code = CSVErrorCode::None;
154 system.clear();
156 owned_context_.reset();
157 }
158 std::string message() const {
159 const std::string text = context.empty() ? std::string() : std::string(context);
160 return system ? text + ": " + system.message() : text;
161 }
162
163 private:
164 // Keep success and borrowed static diagnostics allocation-free.
165 std::unique_ptr<char[]> owned_context_;
166 };
167
168 namespace internals {
169 inline bool fail(CSVError& error, CSVErrorCode code, csv::string_view message) {
170 error = CSVError(code, message);
171 return false;
172 }
173
174 // Use only for literals or other text with static lifetime. Runtime
175 // pointers (notably exception::what()) must use the owning fail().
176 inline bool fail_static(CSVError& error, CSVErrorCode code, csv::string_view message) noexcept {
177 error = CSVError::from_static(code, message);
178 return false;
179 }
180 }
181}
A standalone header file containing shared code.
The all encompassing namespace.
CSVConversionError
Non-throwing CSVField conversion result.
Definition csv_error.hpp:23
@ FloatToInt
A floating point field was requested as an integral type.
@ Overflow
The parsed value does not fit in the requested target type.
@ NegativeToUnsigned
A negative value was requested as an unsigned type.
@ None
Conversion succeeded.
@ NotANumber
The field is not compatible with the requested target type.
const char * csv_conversion_error_message(CSVConversionError error) noexcept
Return a stable human-readable description for a CSVConversionError.
Definition csv_error.hpp:53
CSVErrorCode
Operational failures reported through CSVError.
Definition csv_error.hpp:61
@ IndexOutOfBounds
A row/column position exceeds the visible schema or physical record width.
@ InvalidArgument
Invalid operation input, such as a null stream or zero batch size.
@ RowTooLarge
A complete record could not fit within the configured read window.
@ WriteFailed
Writing, flushing, or closing the output failed.
@ None
Success, including end-of-stream without a row.
@ ReadFailed
The input stream reported an I/O failure rather than ordinary EOF.
@ KeyColumnProtected
Removing the column that supplies the frame's stored keys is prohibited.
@ UnsupportedEncoding
A UTF-16/UTF-32 BOM was found; transcode the input to UTF-8 first.
@ KeyNotFound
A requested row key is absent from the frame's key index.
@ MappingFailed
A subsequent input-file region could not be memory-mapped; see CSVError::system.
@ RowLengthMismatch
A record's field count violates the configured variable-column policy.
@ SizeLimitExceeded
Combined preserved string-view key bytes exceed std::string::max_size().
@ MissingKeyColumn
Keyed construction has an empty key-column name or a name absent from the schema.
@ InvalidProxy
A proxy is unbound or a column proxy was invalidated by structural mutation.
@ OpenFailed
A native file could not be opened, including initial input mapping.
@ InvalidFormat
Conflicting parsing characters or an unresolved delimiter query.
@ DuplicateKey
A key conflicts with an existing row under the requested insertion/duplicate policy.
@ ColumnNotFound
A requested name is absent from the visible column schema.
@ UnkeyedFrame
A key-based operation was requested on a positional-only frame.
@ ReadOnlyProxy
Mutation was requested through a cell, row, or column without edit permission.
@ InvalidChunkSize
Configured read-window size is outside the supported byte limits.
@ UnsupportedOperation
The operation cannot be performed for this frame, such as inserting into a custom-key frame.
@ ConversionFailed
A DataFrame field/key conversion failed; context describes the cause.
std::string_view string_view
The string_view class used by this library.
Definition common.hpp:182
A diagnostic whose text remains valid for the lifetime of the error.
Definition csv_error.hpp:94
csv::string_view context
Diagnostic text; use set_context() to replace it with an owned copy.
Definition csv_error.hpp:98
static CSVError from_static(CSVErrorCode value, csv::string_view message) noexcept
Borrow text that must remain valid for the program's lifetime.
void set_context(csv::string_view message)
Replace the context with an owned copy; accepts views into this error.
CSVError(CSVErrorCode value, csv::string_view message)
Copy runtime text, including temporary strings and exception messages.