Skip to content

Commit 7ff50d7

Browse files
authored
Add LineParseException (#18)
Sometimes, when parsing a line with some sort of unexpected content, is hard to track down the line where this occurred, specially on large files. This pull request adds the LineParseException, which encapsulates all sort of errors which might occur when trying to parse any line. If necessary, it will be possible to access the line which generated the error.
1 parent e19935e commit 7ff50d7

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ plugins {
2424
apply(plugin = "com.novoda.bintray-release")
2525

2626
group = "br.com.guiabolso"
27-
version = "0.4.0"
27+
version = "0.5.0"
2828

2929
repositories {
3030
mavenCentral()

src/main/kotlin/br/com/guiabolso/fixedlengthfilehandler/FixedLengthFileParser.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ public open class FixedLengthFileParser<T>(
102102
@PublishedApi
103103
internal var recordMappings: MutableList<RecordMapping> = recordMappings.toMutableList()
104104

105+
@Suppress("TooGenericExceptionCaught")
105106
public fun buildSequence(): Sequence<T> {
106107
return fileStream.bufferedReader().lineSequence().map {
107108
currentLine = it
108-
recordMapperFor(it).recordBuilder(this)
109+
110+
try {
111+
recordMapperFor(it).recordBuilder(this)
112+
} catch (exception: Exception) {
113+
throw LineParseException(currentLine, exception)
114+
}
109115
}
110116
}
111117

@@ -171,3 +177,8 @@ public class MultiFixedLengthFileParser<T>(
171177
public class NoRecordMappingException(
172178
public val line: String
173179
) : RuntimeException("There are no valid record mappers for line $line")
180+
181+
public class LineParseException(
182+
public val line: String,
183+
public override val cause: Exception
184+
) : RuntimeException("Failed to parse line", cause)

src/test/kotlin/br/com/guiabolso/fixedlengthfilehandler/FixedLengthFileParserTest.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class FixedLengthFileParserTest : ShouldSpec() {
203203
cccc
204204
""".trimmedInputStream()
205205

206-
shouldThrow<NoRecordMappingException> {
206+
shouldThrow<LineParseException> {
207207
multiFixedLengthFileParser<String>(stream) {
208208
withRecord({ it.contains("b") }) {
209209
field<String>(0, 4)
@@ -286,6 +286,22 @@ class FixedLengthFileParserTest : ShouldSpec() {
286286
MyRecord("FirstStr", "SecondStr")
287287
)
288288
}
289+
290+
should("Throw ParseException if there was a line parsing problem") {
291+
data class Foo(val string: String, val date: LocalDate)
292+
293+
val stream = """
294+
aaaa2019-02-09
295+
bbbb2019-ER-10
296+
cccc2019-04-11
297+
""".trimmedInputStream()
298+
299+
shouldThrow<LineParseException> {
300+
fixedLengthFileParser<Foo>(stream) {
301+
Foo(field(0, 4), field(4, 13))
302+
}.toList()
303+
}
304+
}
289305
}
290306

291307
private fun String.trimmedInputStream(): InputStream = trimIndent().toByteArray().inputStream()

0 commit comments

Comments
 (0)