# Ruby exceptions logstash

**URL:** https://discuss.elastic.co/t/ruby-exceptions-logstash/276829
**Category:** Logstash
**Created:** [June 23, 2021, 5:27pm UTC](https://discuss.elastic.co/t/ruby-exceptions-logstash/276829 "2021-06-23T17:27:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Roberto\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roberto_b/32/77615_2.png) [@Roberto\_B](https://discuss.elastic.co/u/Roberto_B)
#### Post date: [June 23, 2021, 5:27pm UTC](https://discuss.elastic.co/t/ruby-exceptions-logstash/276829/1 "2021-06-23T17:27:46Z")

</div>

Hi all,  
i'm in trouble with Ruby exceptions i get 2 types of exceptions:

```auto
[ERROR][logstash.filters.ruby][windows-patching][7da18e5397d31e1f8d249dc2ef273bcbcc32d26e064f064c3138ef1bcbc3c2a0] Ruby exception occurred:
 undefined method `scan' for nil:NilClass
[ERROR][logstash.filters.ruby][windows-patching][e0a2523135310fc00c1030f2da81c2f3b28e517f8b2c21c12101e9d813e448ce] Ruby exception occurred: undefined method `[]' for nil:NilClass

```

I've this kind of ruby filter

```auto
filter{
	ruby {
  code => "
	if event.get('Patches available').nil?
		event.remove('[Patches available]')
	end
  "
	}
}

filter{
	ruby {
  code => "
	if event.get('Patches installed').nil?
		event.remove('[Patches installed]')
	end
  "
	}
}

#Questo modulo standardizza il campo Installable Errata e Applicable Errata togliendo "" [] \ lo splitta e produce un array di stringhe
filter {
	mutate {
    gsub => [
	"Patches available", "[\[\]]", "",
	"Patches available", "\"", ""
	]
	}
	ruby { code => 'event.set("Patches available", event.get("Patches available").scan(/\(\w+\)/))' }
 }

filter {
	mutate {
    gsub => [
	"Patches installed", "[\[\]]", "",
	"Patches installed", "\"", ""
	]
	}
	ruby { code => 'event.set("Patches installed", event.get("Patches installed").scan(/\(\w+\)/))' }
}

filter{
	ruby {
  code => "
	if event.get('Patches available').nil?
		event.set('N_Patches_available', 0)
		event.remove('[Patches available]')
	else
		event.set('N_Patches_available', (event.get('Patches available').length))
	end
  "
	}
}

filter{
	ruby {
  code => "
	if event.get('Patches installed').nil?
		event.set('N_Patches_installed', 0)
		event.remove('[Patches installed]')
	else
		event.set('N_Patches_installed', (event.get('Patches installed').length))
	end
  "
	}
}

```

Maybe the problem is due to the fact that in some case one of the field is empty so "scan" cannnot scan any data?

I would like to totaly remove the field if it is nil and\or to avoid exceptions. Do you know of to do that? Someting like a " if not event.nil? " ?! I'm not a Ruby expert.

KR

Roberto

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [June 23, 2021, 6:19pm UTC](https://discuss.elastic.co/t/ruby-exceptions-logstash/276829/2 "2021-06-23T18:19:24Z")

</div>

Your first two ruby filters potentially delete the field [Patches available], but after running the mutate you unconditionally call ruby code which runs .scan on its value. That is going to result in that "undefined method `scan' for nil:NilClass" exception.

You could try wrapping the mutate and 3rd ruby filter in

```
if [Patches available] {
    ...
}
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 21, 2021, 6:19pm UTC](https://discuss.elastic.co/t/ruby-exceptions-logstash/276829/3 "2021-07-21T18:19:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
