# Throw an error if channel is empty

**URL:** <https://community.seqera.io/t/throw-an-error-if-channel-is-empty/1022>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [July 26, 2024, 10:16pm UTC](https://community.seqera.io/t/throw-an-error-if-channel-is-empty/1022 "2024-07-26T22:16:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashton\_Teng](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/ashton_teng/32/664_2.png) [@Ashton\_Teng](https://community.seqera.io/u/Ashton_Teng)\
**Post date:** [July 26, 2024, 10:16pm UTC](https://community.seqera.io/t/throw-an-error-if-channel-is-empty/1022/1 "2024-07-26T22:16:53Z")

</div>

I have a list of prefixes and a list of files:  
valid\_prefixes = list: [prefixes…]  
files\_ch = tuple: [prefix, [files]…]  
I then want to join the two, and only the files with the valid prefixes remain:  
filtered\_files\_ch = tuple: [prefix, [files]…]

I want to add the important error checking, where if none of the files match the valid prefixes, I want to throw an error. i.e. if the filtered\_files\_ch is empty, throw and error and quit the pipeline.

How can I approach this? I understand that nextflow doesn’t know whether something will be empty at compile time.

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [July 27, 2024, 1:25am UTC](https://community.seqera.io/t/throw-an-error-if-channel-is-empty/1022/2 "2024-07-27T01:25:11Z")

</div>

Hello @Ashton_Teng

Welcome to the community forum 🙂

See the snippet below for quitting a pipeline if a channel is empty.

```auto
Channel
  .empty()
  .ifEmpty { error("The channel was empty. Quitting...") }
  .set { my_ch }

```

If it weren’t empty, it would be stored in the variable `my_ch`. The output should look like:

 ![CleanShot 2024-07-26 at 22.17.15@2x](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/8a198ca264a2ee7c0c06d0e82fe2d5a2a685d3ed.png)

Let’s think of a slightly more complex example. I have a channel that isn’t empty, but to some extent, I want to consider it empty. Let’s say there’s something missing that is important. See the example below:

```auto
Channel
  .of(['Name': 'Marcel'], ['Name': 'Ashton'])
  .filter { it.Name == 'Bob' }
  .ifEmpty { error("There is no name Bob in the channel") }
  .view { it['Name'] }

```

 ![CleanShot 2024-07-26 at 22.24.07@2x](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/9e49a0a55cfd520959870353d18493627910c613.png)

If instead I was looking for Marcel…

```auto
Channel
  .of(['Name': 'Marcel'], ['Name': 'Ashton'])
  .filter { it.Name == 'Marcel' }
  .ifEmpty { error("There is no name Bob in the channel") }
  .view { it['Name'] }

```

 ![CleanShot 2024-07-26 at 22.24.51@2x](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/0fde498ccb860a54ca544f797f675e4140c95eba.png)

As you can see, the `ifEmpty` operator is what you’re probably looking for 😉
