# Boilerplate Reduction

**URL:** <https://ziggit.dev/t/boilerplate-reduction/18610>\
**Category:** Help\
**Tags:** language\
**Created:** [November 27, 2026, 6:04am UTC](https://ziggit.dev/t/boilerplate-reduction/18610 "2026-11-27T06:04:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik.marinov](https://ziggit.dev/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nik.marinov](https://ziggit.dev/u/nik.marinov)\
**Post date:** [November 27, 2026, 6:04am UTC](https://ziggit.dev/t/boilerplate-reduction/18610/1 "2026-11-27T06:04:17Z")

</div>

Hi all,

I keep running into a recurring issue with boilerplate and I’m wondering if there is a way to reduce it.

I’m using an atomic Task queue and multi-threading which makes it so that I can’t use “try” in my functions since they all have to have a “void” return type, but that makes error handling very boilerplate heavy.

Here’s an example:

```zig
try group.concurrent(io, Server.listen, .{ io, q, "127.0.0.1", 8000 });

```

elsewhere in the code:

```zig
pub fn listen(io: Io, q: *AQ(Task), addr: []const u8, port: u16) void {
    const address = std.Io.net.IpAddress.parse(addr, port) catch |e| {
        std.log.err("{}", .{e});
        return;
    };

    var server = address.listen(io, .{ .mode = .stream, .reuse_address = true }) catch |e| {
        std.log.err("{}", .{e});
        return;
    };

    while (true) {
        const conn = server.accept(io) catch |e| {
          std.log.err("{}", .{e});
          return;
      };
        // conn.deinit() has to be called by the handler thread

        const task = Task{
            .handleConn = .{
                .call = handleConn,
                .args = .{ .io = io, .conn = conn },
            },
        };

        q.enqueue(task) catch |e| {
            switch (e) {
                error.QueueFull => {
                    std.debug.print("{} queue full\n", .{std.Thread.getCurrentId()});
                    try io.sleep(.fromMilliseconds(150), .awake);
                    continue;
                },
                error.QueueClosing => {
                    std.debug.print("tasker {} q is closing\n", .{std.Thread.getCurrentId()});
                    return;
                },
                else => {
                    std.log.err("{}", .{e});
                    return;
                },
            }
        };
    }
}

```

Is there a way to not have to repeat this pattern over and over?

```zig
const x = functionCall() catch |e| {
    std.log.err("{}", .{e});
    return;
};

```

I tried using an inline function which would be nice, but the code assumes I’m trying to return a “void” instead of return from the outer function so it doesn’t actually work. Here’s the pattern.

```zig
pub inline fn err(caller: []const u8, e: anyerror) void {
    std.log.err("{s}: {}", .{ caller, e });
}

```

Then the previous code becomes:

```zig
pub fn listen(io: Io, q: *AQ(Task), addr: []const u8, port: u16) void {
    const address = std.Io.net.IpAddress.parse(addr, port) catch |e| err(e);
    var server = address.listen(io, .{ .mode = .stream, .reuse_address = true }) catch |e| err(e);

    while (true) {
        const conn = server.accept(io) catch |e| err(e);
        // conn.deinit() has to be called by the handler thread

        const task = Task{
            .handleConn = .{
                .call = handleConn,
                .args = .{ .io = io, .conn = conn },
            },
        };

        q.enqueue(task) catch |e| {
            switch (e) {
                error.QueueFull => {
                    std.debug.print("{} queue full\n", .{std.Thread.getCurrentId()});
                    try io.sleep(.fromMilliseconds(150), .awake);
                    continue;
                },
                error.QueueClosing => {
                    std.debug.print("tasker {} q is closing\n", .{std.Thread.getCurrentId()});
                    return;
                },
                else => err(e),
            }
        };
    }
}

```

Is there some other pattern that I’m not aware of or thinking of?  
Thanks in advance,  
Nik

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [November 27, 2026, 6:20am UTC](https://ziggit.dev/t/boilerplate-reduction/18610/2 "2026-11-27T06:20:32Z")

</div>

you can make a wrapper that handles errors so that you can return errors from your function, this is similar to what `std.Io.Select` does:

```zig
fn dispatchOrSomething(io: Io, group: *Io.Group, queue: *AQ(Task), f: anytype, args: std.meta.ArgsTuple(@TypeOf(f))) error{ConcurrencyUnavailable}!void {
    const wrapper = struct {
        fn f(q: *AQ(Task), args: std.meta.ArgsTuple(@TypeOf(f)) void {
            const task = @call(f, args) catch |e| {
                std.log.err("{t}", .{e});
                return;
            };
            q.enqueue(task) catch |e| ....;
        }
    };
    try group.concurrent(io, wrapper.f, .{queue, args});
}

```
