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
14namespace csv {
15 namespace internals {
16 CONSTEXPR_VALUE_14 char ERROR_CANNOT_OPEN_FILE[] = "Cannot open file ";
17 CONSTEXPR_VALUE_14 char ERROR_FAILED_OPEN_WRITE[] = "Failed to open file for writing: ";
18 CONSTEXPR_VALUE_14 char ERROR_STREAM_READ_FAILURE[] = "StreamParser read failure";
19 CONSTEXPR_VALUE_14 char ERROR_UNSUPPORTED_ENCODING_SUFFIX[] =
20 " encoded CSV input is not supported directly. Please transcode to UTF-8 before parsing.";
21 CONSTEXPR_VALUE_14 char ERROR_ROW_TOO_SHORT[] = "Line too short ";
22 CONSTEXPR_VALUE_14 char ERROR_ROW_TOO_LONG[] = "Line too long ";
23 CONSTEXPR_VALUE_14 char ERROR_ROW_LARGER_THAN_CHUNK_PREFIX[] =
24 "End of file not reached and no more records parsed. "
25 "This likely indicates a CSV row larger than the chunk size of ";
26 CONSTEXPR_VALUE_14 char ERROR_ROW_LARGER_THAN_CHUNK_SUFFIX[] =
27 " bytes. Use CSVFormat::chunk_size() to increase the chunk size.";
28 CONSTEXPR_VALUE_14 char ERROR_COLUMN_NOT_FOUND[] = "Column not found: ";
29 CONSTEXPR_VALUE_14 char ERROR_COLUMN_INDEX_OUT_OF_BOUNDS[] = "Column index out of bounds.";
30 CONSTEXPR_VALUE_14 char ERROR_COLUMN_INDEX_OUT_OF_RANGE[] = "Column index out of range.";
31 CONSTEXPR_VALUE_14 char CSV_ERROR_INDEX_OUT_OF_BOUNDS[] = "Index out of bounds.";
32 CONSTEXPR_VALUE_14 char ERROR_CANNOT_EDIT_CONST_DF_CELL[] = "Cannot edit a const DataFrame cell.";
33 CONSTEXPR_VALUE_14 char ERROR_CANNOT_ERASE_CONST_DF_ROW[] = "Cannot erase a const DataFrame row.";
34 CONSTEXPR_VALUE_14 char ERROR_COLUMN_APPLY_STATE_COUNT[] =
35 "column_parallel_apply() requires one state object per column.";
36 CONSTEXPR_VALUE_14 char ERROR_COLUMN_APPLY_SUBSET_STATE_COUNT[] =
37 "column_parallel_apply() subset overload requires one state object per selected column.";
38 CONSTEXPR_VALUE_14 char ERROR_COLUMN_APPLY_INVALID_INDEX[] =
39 "column_parallel_apply() subset overload received an invalid column index.";
40 CONSTEXPR_VALUE_14 char ERROR_KEY_COLUMN_EMPTY[] = "Key column cannot be empty.";
41 CONSTEXPR_VALUE_14 char ERROR_KEY_COLUMN_NOT_FOUND[] = "Key column not found: ";
42 CONSTEXPR_VALUE_14 char ERROR_KEY_COLUMN_VALUE[] = "Error retrieving key column value: ";
43 CONSTEXPR_VALUE_14 char ERROR_DUPLICATE_KEY[] = "Duplicate key encountered.";
44 CONSTEXPR_VALUE_14 char ERROR_UNKEYED_DATA_FRAME[] =
45 "This DataFrame was created without a key column.";
46 CONSTEXPR_VALUE_14 char ERROR_KEY_NOT_FOUND[] = "Key not found.";
47 CONSTEXPR_VALUE_14 char ERROR_CHUNK_PARALLEL_APPLY_ZERO[] =
48 "chunk_parallel_apply() requires a non-zero chunk size.";
49 CONSTEXPR_VALUE_14 char ERROR_READER_NULL_STREAM[] = "CSVReader requires a non-null stream";
50 CONSTEXPR_VALUE_14 char ERROR_MULTIPLE_DELIMITERS[] =
51 "There is more than one possible delimiter.";
52 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_FLOOR_PREFIX[] = "Chunk size must be at least ";
53 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_FLOOR_MIDDLE[] = " bytes (500KB). Provided: ";
54 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_CEILING_PREFIX[] = "Chunk size must fit in uint32_t. Maximum: ";
55 CONSTEXPR_VALUE_14 char ERROR_CHUNK_SIZE_CEILING_MIDDLE[] = ". Provided: ";
56 CONSTEXPR_VALUE_14 char ERROR_CHAR_OVERLAP_PREFIX[] =
57 "There should be no overlap between the quote character, "
58 "the set of possible delimiters "
59 "and the set of whitespace characters. Offending characters: ";
60
61 inline std::string make_prefixed_message(const char* prefix, csv::string_view value) {
62 return std::string(prefix) + std::string(value);
63 }
64
65 inline std::string make_unsupported_encoding_message(const char* encoding) {
66 return std::string(encoding) + ERROR_UNSUPPORTED_ENCODING_SUFFIX;
67 }
68
69 inline std::string make_chunk_size_error(size_t floor, size_t provided) {
70 return std::string(ERROR_CHUNK_SIZE_FLOOR_PREFIX)
71 + std::to_string(floor)
72 + ERROR_CHUNK_SIZE_FLOOR_MIDDLE
73 + std::to_string(provided);
74 }
75
76 inline std::string make_chunk_size_ceiling_error(size_t ceiling, size_t provided) {
77 return std::string(ERROR_CHUNK_SIZE_CEILING_PREFIX)
78 + std::to_string(ceiling)
79 + ERROR_CHUNK_SIZE_CEILING_MIDDLE
80 + std::to_string(provided);
81 }
82
83 inline std::string make_row_larger_than_chunk_message(size_t chunk_size) {
84 return std::string(ERROR_ROW_LARGER_THAN_CHUNK_PREFIX)
85 + std::to_string(chunk_size)
86 + ERROR_ROW_LARGER_THAN_CHUNK_SUFFIX;
87 }
88
89 inline std::string make_mmap_failure_message(
90 const std::string& filename,
91 size_t offset,
92 size_t length
93 ) {
94 return "Memory mapping failed during CSV parsing: file='" + filename
95 + "' offset=" + std::to_string(offset)
96 + " length=" + std::to_string(length);
97 }
98
99 inline std::string make_char_overlap_error(const std::set<char>& offenders) {
100 std::string err_msg = ERROR_CHAR_OVERLAP_PREFIX;
101
102 size_t i = 0;
103 for (std::set<char>::const_iterator it = offenders.begin(); it != offenders.end(); ++it, ++i) {
104 err_msg += "'";
105 err_msg += *it;
106 err_msg += "'";
107
108 if (i + 1 < offenders.size()) {
109 err_msg += ", ";
110 }
111 }
112
113 err_msg += '.';
114 return err_msg;
115 }
116
117 [[noreturn]] inline void throw_cannot_open_file(csv::string_view filename) {
118 throw std::runtime_error(make_prefixed_message(ERROR_CANNOT_OPEN_FILE, filename));
119 }
120
121 [[noreturn]] inline void throw_failed_open_for_writing(const std::string& filename) {
122 throw std::runtime_error(std::string(ERROR_FAILED_OPEN_WRITE) + filename);
123 }
124
125 [[noreturn]] inline void throw_unsupported_encoding(const char* encoding) {
126 throw std::runtime_error(make_unsupported_encoding_message(encoding));
127 }
128
129 [[noreturn]] inline void throw_stream_read_failure() {
130 throw std::runtime_error(ERROR_STREAM_READ_FAILURE);
131 }
132
133 [[noreturn]] inline void throw_mmap_failure(
134 const std::error_code& error,
135 const std::string& filename,
136 size_t offset,
137 size_t length
138 ) {
139 throw std::system_error(error, make_mmap_failure_message(filename, offset, length));
140 }
141
142 [[noreturn]] inline void throw_row_too_large_for_chunk(size_t chunk_size) {
143 throw std::runtime_error(make_row_larger_than_chunk_message(chunk_size));
144 }
145
146 [[noreturn]] inline void throw_line_too_short(csv::string_view raw_row) {
147 throw std::runtime_error(make_prefixed_message(ERROR_ROW_TOO_SHORT, raw_row));
148 }
149
150 [[noreturn]] inline void throw_line_too_long(csv::string_view raw_row) {
151 throw std::runtime_error(make_prefixed_message(ERROR_ROW_TOO_LONG, raw_row));
152 }
153
154 [[noreturn]] inline void throw_column_not_found(csv::string_view column) {
155 throw std::runtime_error(make_prefixed_message(ERROR_COLUMN_NOT_FOUND, column));
156 }
157
158 [[noreturn]] inline void throw_column_not_found_out_of_range(csv::string_view column) {
159 throw std::out_of_range(make_prefixed_message(ERROR_COLUMN_NOT_FOUND, column));
160 }
161
162 [[noreturn]] inline void throw_column_index_out_of_bounds() {
163 throw std::out_of_range(ERROR_COLUMN_INDEX_OUT_OF_BOUNDS);
164 }
165
166 [[noreturn]] inline void throw_column_index_out_of_range() {
167 throw std::out_of_range(ERROR_COLUMN_INDEX_OUT_OF_RANGE);
168 }
169 }
170}
A standalone header file containing shared code.
The all encompassing namespace.
std::string_view string_view
The string_view class used by this library.
Definition common.hpp:174