#handling errors in async code

1 messages · Page 1 of 1 (latest)

spare slate
#

I am not very sure how to give context here so... well here is the code

fn resendWaitingFor(self: Api) void {
  1     var group = Io.Group.init;
  2
  3     var slice = self.waiting_for.slice();
  4     for (0..slice.len) |i| group.async(self.io, Api.rawRequest, .{ self, slice.get(i) });
  5
  6     group.await(self.io) catch {};
  7 }
  8
  9 fn rawRequest(
 10     self: Api,
 11     request: Request,
 12 ) !void {
 13     const io = self.io;
 14     var request_buf: [256]u8 = undefined;
 15     var request_writer: Io.Writer = .fixed(&request_buf);
 16     try request.write(&request_writer);
 17
 18     try self.stream.socket.send(
 19         io,
 20         &self.stream.socket.address,
 21         request_writer.toArrayList().items,
 22     );
 23 }

Which gives me the following error

/home/.local/share/zigup/0.17.0-dev.956+2dca73595/files/lib/std/Io.zig:1265:42: error: expected type 'error{Canceled}!void', found '@typeInfo(@typeInfo(@TypeOf(Api.rawRequest)).@"fn".return_type.?).error_union.error_set!void'
                _ = @as(Cancelable!void, @call(.auto, function, args_casted.*)) catch {};

How am I supposed to handle that?

coral thicket
#

rawRequest should return error{Canceled}!void
if it sees other errors, you should catch them and return error.Canceled or deal with them inside rawRequest

spare slate
#

(trying to figure out how to correct the ˋ)

#

its that simple huh

#

doesnt that break the idea of not needing to worry if the function is or isnt async?

#

like... I feel there should be a way to deal with that in the calling side

coral thicket
#

I supposed there could an adapter that convert the errors automatically

#

but for now, you have to write said adapter yourself

spare slate
#

since we dont have inline lambdas as well to just have -> fn() catch error.Cancelled

#

oh well, okay then, thanks

coral thicket
#

np