Error if channel is NOT empty

I want to throw an error if a channel is not empty. I know i could pass the channel into a process that always errors, but i was hoping there was a little bit simpler of a method. For example, something similar to

Channel
  .fromPath("xyz/*")
  .ifEmpty {
      log.error("ERROR...")
      System.exit(2)
  }

but with the inverse behavior

try this, it is not pretty, but works

The map part is needed otherwise the notEmpty error will execute with Empty channel input.

test_channel
        .ifEmpty {
            log.error("this is empty")
        }
        .first()
        .map { it ->
            if (it != null) {
                log.error("this is not empty")
            }
        }

If a channel is empty, the following operator won’t do anything so simply add a map on the end of your channel.

params.input = false
workflow {
    ch_in = params.input ? Channel.fromList([1, 2, 3]) : Channel.empty()
    ch_in.map{ error "Error: Channel is not empty" }
}
1 Like

thanks, that works great!