# Logstash Ruby plugin code block not raising Exception

**URL:** <https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376>\
**Category:** Logstash\
**Created:** [December 10, 2021, 1:58am UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376 "2021-12-10T01:58:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_K](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colin_k/32/78869_2.png) [@Colin\_K](https://discuss.elastic.co/u/Colin_K)\
**Post date:** [December 10, 2021, 1:58am UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376/1 "2021-12-10T01:58:55Z")

</div>

I have a ruby block. The "happy" path works where I see the expected map output. When I provide an invalid scheme for an Array with a log line and an element that does not contain an object with field, I do not throw the exception.

For example an input log line that contains `body = [{"field" => {"key1": => "value1","key2": => "value2"}]`  
returns mapped as expected. But an input line missing field  
`body = [{"notField" => "other_value"}`  
does not show any exception raised in logstash logs or output.

```auto
json{
source =>['body']  
target => "some"
}

    ruby {
       code => "
           if (event.get('some').kind_of?(Array))           
             if (!event.get('some')[0].nil?)
               if (!event.get('some')[0]['field'].nil?)
                  mapped = event.get('some').map {
                    |h|
                      { 
                       'some_value' => h['field']['key1']
                       'another_value' => h['field']['key2'']
                     }
                  }; 
                  event.set('mapped',mapped)
                else 
                  raise Exception.new 'Non-empty Array type without expected schema:'+ event.get('some').to_s
                end
              end  
            end
        "
    }

```

---

<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:** [December 10, 2021, 2:18am UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376/2 "2021-12-10T02:18:56Z")

</div>

There are syntax errors in your ruby code that will cause logstash to crash. If I fix them

```
input { generator { count => 1 lines => [''] } }
filter {
    #mutate { add_field => { "body" => '[{"field": {"key1": "value1", "key2": "value2"}}]' } }
    mutate { add_field => { "body" => '[{"field": null }]' } }
    json{ source => "body" target => "some" }
    ruby {
        code => "
            if (event.get('some').kind_of?(Array))
                if (!event.get('some')[0].nil?)
                    if (!event.get('some')[0]['field'].nil?)
                        mapped = event.get('some').map { |h| { 'some_value' => h['field']['key1'], 'another_value' => h['field']['key2'] } };
                        event.set('mapped',mapped)
                    else
                        raise Exception.new 'Non-empty Array type without expected schema:'+ event.get('some').to_s
                    end
                end
            end
        "
    }
}
output { stdout { codec => rubydebug { metadata => false } } }

```

then I do get an error logged

> [ERROR][logstash.filters.ruby][main][ae9d0609338ac65d4f73d4911a57e44fce57dd39876d923c3802769de68bf55a] Ruby exception occurred: Non-empty Array type without expected schema:[{"field"=\>nil}]

---

<div class="post-metadata">

**Author:** ![Colin\_K](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colin_k/32/78869_2.png) [@Colin\_K](https://discuss.elastic.co/u/Colin_K)\
**Post date:** [December 11, 2021, 10:06am UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376/3 "2021-12-11T10:06:26Z")

</div>

> [@Colin\_K](#):
>
> ```auto
> if (event.get('some').kind_of?(Array))           
> if (!event.get('some')[0].nil?)
> if (!event.get('some')[0]['field'].nil?)
> mapped = event.get('some').map {
> |h|
> { 
> 'some_value' => h['field']['key1']
> 'another_value' => h['field']['key2'']
> }
> }; 
> event.set('mapped',mapped)
> else 
> raise Exception.new 'Non-empty Array type without expected schema:'+ event.get('some').to_s
> end
> end  
> end
> 
> ```

Perhaps I have an issue transcribing the code here but I'm not getting a compilation error and the logstash filter is running.

Are you showing that it's required to place the map block on a single line? Can you describe in english what the Syntax Error is?

---

<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:** [December 11, 2021, 4:55pm UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376/4 "2021-12-11T16:55:30Z")

</div>

> [@Colin\_K](#):
>
> ```auto
> { 
> 'some_value' => h['field']['key1']
> 'another_value' => h['field']['key2'']
> }
> 
> ```

ruby requires a comma between the two entries in the hash, and you have an unmatched quote in 'key2''

Newlines within the code block are a matter of personal preference.

---

<div class="post-metadata">

**Author:** ![Colin\_K](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colin_k/32/78869_2.png) [@Colin\_K](https://discuss.elastic.co/u/Colin_K)\
**Post date:** [December 21, 2021, 6:57pm UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376/5 "2021-12-21T18:57:35Z")

</div>

I agree that the comma is required. I missed that when transcribing from production code. However the issue is the exception Non-empty Array type without expected schema, and NOT a ruby syntax error.

[ERROR][logstash.filters.ruby][main][ae9d0609338ac65d4f73d4911a57e44fce57dd39876d923c3802769de68bf55a] Ruby exception occurred: Non-empty Array type without expected schema:[{"field"=\>nil}]

---

<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:** [January 18, 2022, 6:57pm UTC](https://discuss.elastic.co/t/logstash-ruby-plugin-code-block-not-raising-exception/291376/6 "2022-01-18T18:57:55Z")

</div>

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