Stream: beginners

Topic: ✔ Reading file and handling errors


view this post on Zulip Seth Workman (Dec 07 2023 at 21:42):

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 {} *

view this post on Zulip Seth Workman (Dec 07 2023 at 21:43):

It looks like something like this would work

        FileReadErr _ _ -> Stderr.line "Error"
        FileReadUtf8Err _ _ -> Stderr.line "Error"

view this post on Zulip Seth Workman (Dec 07 2023 at 21:44):

Is there a better way to handle these errors?

view this post on Zulip Luke Boswell (Dec 07 2023 at 21:45):

Your very close, just when you pattern match on the error yags in handleErr you are not including the tag payloads.

view this post on Zulip Luke Boswell (Dec 07 2023 at 21:47):

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.

view this post on Zulip Luke Boswell (Dec 07 2023 at 21:55):

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.

view this post on Zulip Seth Workman (Dec 07 2023 at 21:56):

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

view this post on Zulip Notification Bot (Dec 07 2023 at 21:56):

Seth Workman has marked this topic as resolved.

view this post on Zulip Ryan Bates (Dec 07 2023 at 21:59):

@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