Skip to content

Commit ebe259b

Browse files
committed
Improve error handling
1 parent 4255a8f commit ebe259b

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/ui/WorkflowTree.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.ui.Modifier
2020
import androidx.compose.ui.graphics.Color
2121
import androidx.compose.ui.unit.dp
2222
import com.squareup.workflow1.traceviewer.model.Node
23+
import com.squareup.workflow1.traceviewer.util.ParseResult
2324
import com.squareup.workflow1.traceviewer.util.parseTrace
2425
import io.github.vinceglb.filekit.PlatformFile
2526

@@ -33,17 +34,31 @@ public fun RenderDiagram(
3334
frameInd: Int,
3435
onFileParse: (List<Node>) -> Unit,
3536
onNodeSelect: (Node) -> Unit,
37+
modifier: Modifier = Modifier
3638
) {
3739
var frames by remember { mutableStateOf<List<Node>>(emptyList()) }
38-
var isLoading by remember { mutableStateOf(true) }
40+
var isLoading by remember(traceFile) { mutableStateOf(true) }
41+
var error by remember(traceFile) { mutableStateOf<Throwable?>(null) }
3942

4043
LaunchedEffect(traceFile) {
41-
isLoading = true
42-
frames = parseTrace(traceFile)
43-
onFileParse(frames)
44+
val parseResult = parseTrace(traceFile)
45+
46+
if (parseResult is ParseResult.Failure) {
47+
error = parseResult.error
48+
return@LaunchedEffect
49+
}
50+
51+
val parsedFrames = (parseResult as ParseResult.Success).trace ?: emptyList()
52+
frames = parsedFrames
53+
onFileParse(parsedFrames)
4454
isLoading = false
4555
}
4656

57+
if (error != null) {
58+
Text("Error parsing file: ${error?.message}")
59+
return
60+
}
61+
4762
if (!isLoading) {
4863
DrawTree(frames[frameInd], onNodeSelect)
4964
}
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
package com.squareup.workflow1.traceviewer.util
22

33
import com.squareup.moshi.JsonAdapter
4-
import com.squareup.moshi.JsonDataException
54
import com.squareup.moshi.Moshi
65
import com.squareup.moshi.Types
76
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
87
import com.squareup.workflow1.traceviewer.model.Node
98
import io.github.vinceglb.filekit.PlatformFile
109
import io.github.vinceglb.filekit.readString
11-
import java.io.IOException
1210

1311
/**
14-
* Parses a given file's JSON String into [Node] with Moshi adapters. Moshi automatically throws JsonDataException
15-
* and IOException
16-
* @throws JsonDataException malformed JSON data or an error reading.
17-
* @throws IOException JSON is correct, but mismatch between class and JSON structure.
12+
* Parses a given file's JSON String into [Node] with Moshi adapters.
13+
*
14+
* @return A [ParseResult] representing result of parsing, either an error related to the
15+
* format of the JSON, or a success and a parsed trace.
1816
*/
1917
public suspend fun parseTrace(
2018
file: PlatformFile,
21-
): List<Node> {
22-
val jsonString = file.readString()
23-
24-
val moshi = Moshi.Builder()
25-
.add(KotlinJsonAdapterFactory())
26-
.build()
27-
28-
val workflowList = Types.newParameterizedType(List::class.java, Node::class.java)
29-
val workflowAdapter: JsonAdapter<List<Node>> = moshi.adapter(workflowList)
30-
val trace = workflowAdapter.fromJson(jsonString)
31-
if (trace == null) {
32-
throw JsonDataException("Could not parse JSON")
19+
): ParseResult {
20+
return try {
21+
val jsonString = file.readString()
22+
val moshi = Moshi.Builder()
23+
.add(KotlinJsonAdapterFactory())
24+
.build()
25+
val workflowList = Types.newParameterizedType(List::class.java, Node::class.java)
26+
val workflowAdapter: JsonAdapter<List<Node>> = moshi.adapter(workflowList)
27+
val trace = workflowAdapter.fromJson(jsonString)
28+
ParseResult.Success(trace)
29+
} catch (e: Exception) {
30+
ParseResult.Failure(e)
3331
}
34-
return trace
32+
}
33+
34+
sealed interface ParseResult {
35+
class Success(val trace: List<Node>?) : ParseResult
36+
class Failure(val error: Throwable) : ParseResult
3537
}

0 commit comments

Comments
 (0)