Vince's CSV Parser
Loading...
Searching...
No Matches
csv_exceptions.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <set>
8#include <stdexcept>
9#include <string>
10#include <system_error>
11
12#include "common.hpp"
13#include "csv_error.hpp"
14
15namespace csv {
16 namespace internals {
17 CONSTEXPR_VALUE_14 char ERROR_CANNOT_OPEN_FILE[] = "Cannot open file ";
18 CONSTEXPR_VALUE_14 char ERROR_FAILED_OPEN_WRITE[] = "Failed to open file for writing: ";
19 CONSTEXPR_VALUE_14 char ERROR_STREAM_READ_FAILURE[] = "StreamParser read failure";
20 CONSTEXPR_VALUE_14 char ERROR_UNSUPPORTED_ENCODING_SUFFIX[] =
21 " encoded CSV input is not supported directly. Please transcode to UTF-8 before parsing.";
22 CONSTEXPR_VALUE_14 char ERROR_UNSUPPORTED_UTF16[] =
23 "UTF-16 encoded CSV input is not supported directly. Please transcode to UTF-8 before parsing.";
24 CONSTEXPR_VALUE_14 char ERROR_UNSUPPORTED_UTF32[] =
25 "UTF-32 encoded CSV input is not supported directly. Please transcode to UTF-8 before parsing.";
26 CONSTEXPR_VALUE_14 char ERROR_ROW_TOO_SHORT[] = "Line too short ";
27 CONSTEXPR_VALUE_14 char ERROR_ROW_TOO_LONG[] = "Line too long ";
28 CONSTEXPR_VALUE_14 char ERROR_ROW_LARGER_THAN_CHUNK_PREFIX[] =
29 "End of file not reached and no more records parsed. "
30 "This likely indicates a CSV row larger than the chunk size of ";
31 CONSTEXPR_VALUE_14 char ERROR_ROW_LARGER_THAN_CHUNK_SUFFIX[] =
32 " bytes. Use CSVFormat::chunk_size() to increase the chunk size.";
33 CONSTEXPR_VALUE_14 char ERROR_COLUMN_NOT_FOUND[] = "Column not found: ";
34 CONSTEXPR_VALUE_14 char ERROR_COLUMN_INDEX_OUT_OF_BOUNDS[] = "Column index out of bounds.";
35 CONSTEXPR_VALUE_14 char ERROR_COLUMN_INDEX_OUT_OF_RANGE[] = "Column index out of range.";
36 CONSTEXPR_VALUE_14 char CSV_ERROR_INDEX_OUT_OF_BOUNDS[] = "Index out of bounds.";
37 CONSTEXPR_VALUE_14 char ERROR_CANNOT_EDIT_CONST_DF_CELL[] = "Cannot edit a const DataFrame cell.";
38 CONSTEXPR_VALUE_14 char ERROR_CANNOT_ERASE_CONST_DF_ROW[] = "Cannot erase a const DataFrame row.";
39 CONSTEXPR_VALUE_14 char ERROR_COLUMN_APPLY_STATE_COUNT[] =
40 "column_parallel_apply() requires one state object per column.";
41 CONSTEXPR_VALUE_14 char ERROR_COLUMN_APPLY_SUBSET_STATE_COUNT[] =
42 "column_parallel_apply() subset overload requires one state object per selected column.";
43 CONSTEXPR_VALUE_14 char ERROR_COLUMN_APPLY_INVALID_INDEX[] =
44 "column_parallel_apply() subset overload received an invalid column index.";
45 CONSTEXPR_VALUE_14 char ERROR_KEY_COLUMN_EMPTY[] = "Key column cannot be empty.";
46 CONSTEXPR_VALUE_14 char ERROR_KEY_COLUMN_NOT_FOUND[] = "Key column not found: ";
47 CONSTEXPR_VALUE_14 char ERROR_KEY_COLUMN_VALUE[] = "Error retrieving key column value: ";
48 CONSTEXPR_VALUE_14 char ERROR_DUPLICATE_KEY[] = "Duplicate key encountered.";
49 CONSTEXPR_VALUE_14 char ERROR_UNKEYED_DATA_FRAME[] =
50 "This DataFrame was created without a key column.";
51 CONSTEXPR_VALUE_14 char ERROR_KEY_NOT_FOUND[] = "Key not found.";
52 CONSTEXPR_VALUE_14 char ERROR_CHUNK_PARALLEL_APPLY_ZERO[] =
53 "chunk_parallel_apply() requires a non-zero chunk size.";
54 CONSTEXPR_VALUE_14 char ERROR_READER_NULL_STREAM[] = "CSVReader requires a non-null stream";
55 CONSTEXPR_VALUE_14 char ERROR_MULTIPLE_DELIMITERS[] =
56 "There is more than one possible delimiter.";
57 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_FLOOR_PREFIX[] = "Chunk size must be at least ";
58 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_FLOOR_MIDDLE[] = " bytes (500KB). Provided: ";
59 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_CEILING_PREFIX[] = "Chunk size must fit in uint32_t. Maximum: ";
60 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_CEILING_MIDDLE[] = ". Provided: ";
61 CONSTEXPR_VALUE_14 char ERROR_CHAR_OVERLAP_PREFIX[] =
62 "There should be no overlap between the quote character, "
63 "the set of possible delimiters "
64 "and the set of whitespace characters. Offending characters: ";
65
66 inline std::string make_prefixed_message(const char* prefix, csv::string_view value) {
67 return std::string(prefix) + std::string(value);
68 }
69
70 inline std::string make_unsupported_encoding_message(const char* encoding) {
71 return std::string(encoding) + ERROR_UNSUPPORTED_ENCODING_SUFFIX;
72 }
73
74 inline std::string make_chunk_size_error(size_t floor, size_t provided) {
75 return std::string(ERROR_CHUNK_SIZE_FLOOR_PREFIX)
76 + std::to_string(floor)
77 + ERROR_CHUNK_SIZE_FLOOR_MIDDLE
78 + std::to_string(provided);
79 }
80
81 inline std::string make_chunk_size_ceiling_error(size_t ceiling, size_t provided) {
82 return std::string(ERROR_CHUNK_SIZE_CEILING_PREFIX)
83 + std::to_string(ceiling)
84 + ERROR_CHUNK_SIZE_CEILING_MIDDLE
85 + std::to_string(provided);
86 }
87
88 inline std::string make_row_larger_than_chunk_message(size_t chunk_size) {
89 return std::string(ERROR_ROW_LARGER_THAN_CHUNK_PREFIX)
90 + std::to_string(chunk_size)
91 + ERROR_ROW_LARGER_THAN_CHUNK_SUFFIX;
92 }
93
94 inline std::string make_mmap_failure_message(
95 const std::string& filename,
96 size_t offset,
97 size_t length
98 ) {
99 return "Memory mapping failed during CSV parsing: file='" + filename
100 + "' offset=" + std::to_string(offset)
101 + " length=" + std::to_string(length);
102 }
103
104 inline std::string make_char_overlap_error(const std::set<char>& offenders) {
105 std::string err_msg = ERROR_CHAR_OVERLAP_PREFIX;
106
107 size_t i = 0;
108 for (std::set<char>::const_iterator it = offenders.begin(); it != offenders.end(); ++it, ++i) {
109 err_msg += "'";
110 err_msg += *it;
111 err_msg += "'";
112
113 if (i + 1 < offenders.size()) {
114 err_msg += ", ";
115 }
116 }
117
118 err_msg += '.';
119 return err_msg;
120 }
121
122#if CSV_EXCEPTIONS_ENABLED
124 inline void throw_error(const CSVError& error) {
125 switch (error.code) {
127 return;
129 throw std::system_error(error.system, std::string(error.context));
132 throw std::invalid_argument(std::string(error.context));
134 throw std::length_error(std::string(error.context));
135 default:
136 throw std::runtime_error(std::string(error.context));
137 }
138 }
139 [[noreturn]] inline void throw_cannot_open_file(csv::string_view filename) {
140 throw std::runtime_error(make_prefixed_message(ERROR_CANNOT_OPEN_FILE, filename));
141 }
142
143 [[noreturn]] inline void throw_failed_open_for_writing(const std::string& filename) {
144 throw std::runtime_error(std::string(ERROR_FAILED_OPEN_WRITE) + filename);
145 }
146
147 [[noreturn]] inline void throw_unsupported_encoding(const char* encoding) {
148 throw std::runtime_error(make_unsupported_encoding_message(encoding));
149 }
150
151 [[noreturn]] inline void throw_stream_read_failure() {
152 throw std::runtime_error(ERROR_STREAM_READ_FAILURE);
153 }
154
155 [[noreturn]] inline void throw_mmap_failure(
156 const std::error_code& error,
157 const std::string& filename,
158 size_t offset,
159 size_t length
160 ) {
161 throw std::system_error(error, make_mmap_failure_message(filename, offset, length));
162 }
163
164 [[noreturn]] inline void throw_row_too_large_for_chunk(size_t chunk_size) {
165 throw std::runtime_error(make_row_larger_than_chunk_message(chunk_size));
166 }
167
168 [[noreturn]] inline void throw_line_too_short(csv::string_view raw_row) {
169 throw std::runtime_error(make_prefixed_message(ERROR_ROW_TOO_SHORT, raw_row));
170 }
171
172 [[noreturn]] inline void throw_line_too_long(csv::string_view raw_row) {
173 throw std::runtime_error(make_prefixed_message(ERROR_ROW_TOO_LONG, raw_row));
174 }
175
176 [[noreturn]] inline void throw_column_not_found(csv::string_view column) {
177 throw std::runtime_error(make_prefixed_message(ERROR_COLUMN_NOT_FOUND, column));
178 }
179
180 [[noreturn]] inline void throw_column_not_found_out_of_range(csv::string_view column) {
181 throw std::out_of_range(make_prefixed_message(ERROR_COLUMN_NOT_FOUND, column));
182 }
183
184 [[noreturn]] inline void throw_column_index_out_of_bounds() {
185 throw std::out_of_range(ERROR_COLUMN_INDEX_OUT_OF_BOUNDS);
186 }
187
188 [[noreturn]] inline void throw_column_index_out_of_range() {
189 throw std::out_of_range(ERROR_COLUMN_INDEX_OUT_OF_RANGE);
190 }
191#endif
192 }
193}
A standalone header file containing shared code.
Explicit error reporting shared by CSV reading, writing and DataFrames.
void throw_error(const CSVError &error)
Translate explicit library failures while preserving legacy exception categories.
The all encompassing namespace.
@ InvalidArgument
Invalid operation input, such as a null stream or zero batch size.
@ None
Success, including end-of-stream without a row.
@ MappingFailed
A subsequent input-file region could not be memory-mapped; see CSVError::system.
@ SizeLimitExceeded
Combined preserved string-view key bytes exceed std::string::max_size().
@ InvalidChunkSize
Configured read-window size is outside the supported byte limits.
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