Skip to content

Commit e7993a8

Browse files
committed
Provide a more informative error message for invalid column names in the header
1 parent a7837ec commit e7993a8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/init_parsing.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ function process_header_and_schema_and_finish_row_skip!(
9797
elseif !Parsers.ok(code)
9898
throw(HeaderParsingError("Error parsing header for column $i at $(lines_skipped_total+1):$(pos) (row:pos)."))
9999
else
100-
push!(parsing_ctx.header, Symbol(strip(Parsers.getstring(row_bytes, val, options.e))))
100+
identifier_s = strip(Parsers.getstring(row_bytes, val, options.e))
101+
try
102+
push!(parsing_ctx.header, Symbol(identifier_s))
103+
catch
104+
throw(HeaderParsingError("Error parsing header for column $i ('$(identifier_s)') at " *
105+
"$(lines_skipped_total+1):$pos (row:pos): presence of invalid non text bytes in the CSV snippet"))
106+
end
101107
end
102108
pos += tlen
103109
end
@@ -166,7 +172,13 @@ function process_header_and_schema_and_finish_row_skip!(
166172
elseif !Parsers.ok(code)
167173
throw(HeaderParsingError("Error parsing header for column $i at $(lines_skipped_total+1):$pos (row:pos)."))
168174
else
169-
push!(parsing_ctx.header, Symbol(strip(Parsers.getstring(row_bytes, val, options.e))))
175+
identifier_s = strip(Parsers.getstring(row_bytes, val, options.e))
176+
try
177+
push!(parsing_ctx.header, Symbol(identifier_s))
178+
catch
179+
throw(HeaderParsingError("Error parsing header for column $i ('$(identifier_s)') at " *
180+
"$(lines_skipped_total+1):$pos (row:pos): presence of invalid non text bytes in the CSV snippet"))
181+
end
170182
end
171183
pos += tlen
172184
i += 1

test/exception_handling.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ end
228228
header=true,
229229
)
230230

231+
@test_throws ChunkedCSV.HeaderParsingError("Error parsing header for column 1 ('a\0') at 1:1 (row:pos): presence of invalid non text bytes in the CSV snippet") parse_file(IOBuffer("""
232+
a\0,b
233+
1,2
234+
"""),
235+
[Int,Int],
236+
header=true,
237+
)
238+
231239
@test_throws ArgumentError("Provided header and schema names don't match. In schema, not in header: [:q]. In header, not in schema: [:a, :b, :c]") parse_file(IOBuffer("""
232240
a,b,c
233241
1,2,3

0 commit comments

Comments
 (0)