# Channels to return values in

**URL:** <https://forum.dfinity.org/t/channels-to-return-values-in/20437>\
**Category:** CDK\
**Tags:** Discussing, community-consideration\
**Created:** [June 4, 2023, 1:35pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437 "2023-06-04T13:35:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 4, 2023, 1:35pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/1 "2023-06-04T13:35:14Z")

</div>

From [Issues · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/issues/4017:)

We often have a situation when we need two shared methods to accomplish one action:

```auto
public shared func f(/*args*/) {
  // f code
};
public shared func g() {
  // g code
};

```

`f` initializes the action to be done and `g` accomplishes it.

It would be good to rewrite it as:

```auto
public shared func f(/*args*/) {
  // f code
  g();
};
public shared func g() {
  // g code
};

```

to have only `f` call in the “normal” (no errors) situation. But this way, we won’t know whether the error happened in `f` or in `g`.

To remove this trouble, I propose _channels_:

```auto
public shared func f(/*args*/) {
  // f code
  channel_write("f succeeded");
  g();
};
public shared func g() {
  // g code
};

```

The API caller would be able to read the channel to determine, whether the error was in `f` or in `g`.

I leave for community discussion whether channel values be typed (and of which types), Motoko syntax support, and implementation. I will also post this to the forum.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 4, 2023, 1:38pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/2 "2023-06-04T13:38:24Z")

</div>

A possible syntax is

```auto
public shared func f(/*args*/) : Channel<Nat> {
  // f code
  channel_write(22);
  g();
};

```

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 4, 2023, 1:50pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/3 "2023-06-04T13:50:35Z")

</div>

I realized that what I proposed has a name: It is a generator function.

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 5, 2023, 6:36am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/4 "2023-06-05T06:36:09Z")

</div>

> [@qwertytrewq](#):
>
> But this way, we won’t know whether the error happened in `f` or in `g`.

Isn’t this a positive thing? As someone using this interface I don’t care about which function of the implementation returned an error. I just want to handle the error, and if you have different error causes, then give me an enum so I can match on that

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 5, 2023, 6:50am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/5 "2023-06-05T06:50:30Z")

</div>

@Vivienne How can I return the enum if my execution flow is interrupted? The whole problem is that the current architecture does not allow me to return a value that classifies the error.

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 5, 2023, 6:55am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/6 "2023-06-05T06:55:04Z")

</div>

If you have a result return type on both functions(I’m not sure about Motoko syntax, but in Rust that would be e.g. `Result<(), MyErrorEnum>` you can forward g’s error as an error by f

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 5, 2023, 7:09am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/7 "2023-06-05T07:09:41Z")

</div>

That does not work for “system” errors like canister calls queue overflow. But to be useful, error handling needs to work in all cases.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [June 5, 2023, 9:59am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/8 "2023-06-05T09:59:48Z")

</div>

I can’t see how an extra mechanism could automatically take care of errors originating from the platform. The “obvious” way to keep errors from different sources apart is by tagging them distinctly (i.e. as a variant type) and return them from the function as a `Result<>`. For system methods you’ll have to catch the error (in the queue overflow case) and convert it to your error variant. @paulyoung has come up with a neat pattern that allows one to automatically widen the error type with new causes: [Motoko Bootcamp V1 2022 | Motoko Variant types - YouTube](https://youtu.be/GFenqSGhj7I?t=1948) I cannot recommend it enough!

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 5, 2023, 11:09am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/9 "2023-06-05T11:09:41Z")

</div>

So,

```auto
function m() : async () {
  try {
    f();
  }
  catch (e) {
    return #err1;
  }
  try {
    g();
  }
  catch (e) {
    return #err2;
  }
};

```

Does the above reliably detect all errors distinctly?

I think it does: If entrance to `m` fails, it return that error. If anything fails in `f` or `g` (including system errors), it returns `#err1` or `#err2`.

Right?

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 5, 2023, 8:26pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/10 "2023-06-05T20:26:01Z")

</div>

> [@qwertytrewq](#):
>
> types

You would need to at least change m to return an ‘async {#err1; #err2}’,

Also ‘f’ and ‘g’ are implicilty _one-way_ methods, with no result, since they have no return type at all. This means that the caller will send the message without waiting for any response.

Perhaps you meant ‘async ()’ for both, and wanted to await both calls?

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 5, 2023, 9:08pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/11 "2023-06-05T21:08:42Z")

</div>

> [@claudio](#):
>
> Perhaps you meant ‘async ()’ for both, and wanted to await both calls?

Yes. Await first `f` then `g`.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [June 6, 2023, 8:34am UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/12 "2023-06-06T08:34:09Z")

</div>

> [@qwertytrewq](#):
>
> returns `#err1` or `#err2`

Also you might want to pass some of the information in the caught `e` to the `#err1` or `#err2`. Variants can carry (here shareable) payload.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 6, 2023, 12:52pm UTC](https://forum.dfinity.org/t/channels-to-return-values-in/20437/13 "2023-06-06T12:52:55Z")

</div>

Unfortunately, this try…catch won’t work:

[https://forum.dfinity.org/t/when-does-the-catch-block-of-try-catch-run-in-motoko/12464/2:](https://forum.dfinity.org/t/when-does-the-catch-block-of-try-catch-run-in-motoko/12464/2:)

> Note that message sends can, in rare cases, trap synchronously, before the message body is even queued and executed, which falls into the case of the previous paragraph.

Not working a call inside `try` is indistinguishable from failure of calling the entire my shared method.

So, my “channels” idea still makes sense.

Therefore, I still need two calls from frontend (like invoice and payment), unfortunately.
