I'm confused about how to handle errors here as I'm getting a compiler error
run =
part1 <-
Path.fromStr "day1_input.txt"
|> File.readUtf8
|> Task.await
Stdout.line "\(part1)"
handleErr = \err ->
when err is
FileReadErr -> Stderr.line "Error"
FileReadUtf8Err -> Stderr.line "Error"
main =
run |> Task.onErr handleErr
19│ |> Task.onErr handleErr
^^^^^^^^^
This handleErr value is a:
[
FileReadErr,
FileReadUtf8Err,
] -> Task {} *
But |> needs its 2nd argument to be:
[
FileReadErr Path.Path InternalFile.ReadErr,
FileReadUtf8Err Path.Path [BadUtf8 Utf8ByteProblem Nat]*,
] -> Task {} *
It looks like something like this would work
FileReadErr _ _ -> Stderr.line "Error"
FileReadUtf8Err _ _ -> Stderr.line "Error"
Is there a better way to handle these errors?
Your very close, just when you pattern match on the error yags in handleErr you are not including the tag payloads.
If you havent seen it the https://www.roc-lang.org/examples/Tasks/README.html example might help, but I'm guessing from your code that you have as your using that same pattern.
I highly recommend adding a type annotation in situations like this, it can help debug the issue and by pinning the types at that point the compiler can help check your understanding. You can always blow it away once your done with it and have working code.
I think I'm starting to get it a bit more now, thanks! I'll read that over again and see what else I can pick up
Seth Workman has marked this topic as resolved.
@Seth Workman you can also import local files directly which doesn't require error handling. The compiler handles it.
app ...
imports [
"day1_input.txt" as input : Str,
...
]
Last updated: Jul 06 2025 at 12:14 UTC