10000 exceptions-to-values.6 : Collect reasons for failure · java-to-kotlin/code@15cf851 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15cf851

Browse files
Duncan McGregordmcg
authored andcommitted
exceptions-to-values.6 : Collect reasons for failure
1 parent 496c4c6 commit 15cf851

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/main/java/travelator/marketing/HighValueCustomersMain.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import kotlin.system.exitProcess
77
fun main() {
88
System.`in`.reader().use { reader ->
99
System.out.writer().use { writer ->
10-
val errorLines = mutableListOf<String>()
10+
val errorLines = mutableListOf<ParseFailure>()
1111
val reportLines = reader
1212
.asLineSequence()
1313
.toHighValueCustomerReport {
@@ -16,7 +16,9 @@ fun main() {
1616
if (errorLines.isNotEmpty()) {
1717
System.err.writer().use { error ->
1818
error.appendLine("Lines with errors")
19-
errorLines.asSequence().writeTo(error)
19+
errorLines.asSequence().map { parseFailure ->
20+
"${parseFailure::class.simpleName} in ${parseFailure.line}"
21+
}.writeTo(error)
2022
}
2123
exitProcess(-1)
2224
} else {

src/main/java/travelator/marketing/HighValueCustomersReport.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import com.natpryce.Success
66
import com.natpryce.recover
77

88
fun Sequence<String>.toHighValueCustomerReport(
9-
onErrorLine: (String) -> Unit = {}
9+
onErrorLine: (ParseFailure) -> Unit = {}
1010
): Sequence<String> {
1111
val valuableCustomers = this
1212
.withoutHeader()
1313
.map { line ->
1414
line.toCustomerData().recover {
15-
onErrorLine(line)
15+
onErrorLine(it)
1616
null
1717
}
1818
}

src/test/java/travelator/marketing/HighValueCustomersReportTests.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ class HighValueCustomersReportTests {
5454
"1\tFred\tFlintstone\t11\t1000.00",
5555
)
5656

57-
val errorCollector = mutableListOf<String>()
57+
val errorCollector = mutableListOf<ParseFailure>()
5858
val result = lines
5959
.asSequence()
6060
.constrainOnce()
61-
.toHighValueCustomerReport { badLine -> // <1>
61+
.toHighValueCustomerReport { badLine ->
6262
errorCollector += badLine
6363
}
6464
.toList()
65+
assertEquals(
66+
listOf(NotEnoughFieldsFailure("INVALID LINE")),
67+
errorCollector
68+
)
6569

6670
assertEquals(
6771
listOf(
@@ -71,10 +75,6 @@ class HighValueCustomersReportTests {
7175
),
7276
result
7377
)
74-
assertEquals(
75-
listOf("INVALID LINE"),
76-
errorCollector
77-
)
7878
}
7979

8080

0 commit comments

Comments
 (0)
0