how to set the message attribute of the exception variable in Ruby? - ruby

I have an Exception type variable in Ruby. How can I set the value of attribute "message" of the exception variable?
Is there any method to set the attribute (like rb_attr_set()..)?

I got the answer for above question myself. We can set the "message" attribute of Exception object as follows:
rb_iv_set(*objeException, "mesg", current);
whereas current in VALUE type variable in which ruby string is stored..

Related

How to access / address nested fields on Logstash

I am currently trying to convert a nested sub field that contains hexadecimal string to an int type field using Logstash filter
ruby{
code => 'event.set("[transactions][gasprice_int]", event.get("[transactions][gasPrice]").to_i(16))'
}
but it's returning the error
[ERROR][logstash.filters.ruby ][main][2342d24206691f4db46a60285e910d102a6310e78cf8af43c9a2f1a1d66f58a8] Ruby exception occurred: wrong number of arguments calling `to_i` (given 1, expected 0)
I also tried looping through json objects in transactions field using
transactions_num = event.get("[transactions]").size
transactions_num.times do |index|
event.set("[transactions][#{index}][gasprice_int]", event.get("[transactions][#{index}][gasPrice].to_i(16)"))
end
but this also returned an error of
[ERROR][logstash.filters.ruby ][main][99b05fdb6022cc15b0f97ba10cabb3e7c1a8fabb8e0c47d6660861badffdb28e] Ruby exception occurred: Invalid FieldReference: `[transactions][0][gasPrice].to_i(16)`
This method of conversion of hex-string to int type using a ruby filter worked when I wasn't dealing with nested fields, so can anyone please help me how to correctly address nested fields in this case?
btw,
this is the code that still works
ruby {
code => 'event.set("difficulty_int", event.get("difficulty").to_i(16))'
}
I think you have answered this one yourself in your final example - the to_i should not be inside the double quotes. So
...event.get("[transactions][#{index}][gasPrice].to_i(16)"))
should be
...event.get("[transactions][#{index}][gasPrice]").to_i(16))

Access variable of the returning object in Freemarker

I have an addNewRow() method that returns a object which gets displayed through the Freemarker template. There is a scenario where I need to check if one of the fields of the returning object has the value of -1. I tried the following syntax unsuccessfully.
<#if ${Object.field} ==-1>
<#if Object.field ==-1>
How can I access the object variable in this case?

chef attributes value not getting parsed in another attribute

I am setting attributes in default.rb as
default[:my_app] = {
:vol => "data02",
:commitlog => "/foo/bar/node[:vol]/commitlog",
}
But :vol value is not getting parsed in commitlog attribute and I am getting following error.
mError executing action `create` on resource 'directory[/foo/bar/node[:vol]/comitlog]'[0m
You're missing the String interpolation syntax, e.g. y = "The value of X is #{X}." You probably want:
default[:my_app] = {
:vol => "data02",
:commitlog => "/foo/bar/#{node[:vol]}/commitlog",
}
Also, keep in mind that if you make one attribute depend on the value of another, you might override node[:my_app][:vol] later and expect the value of node[:my_app][:commitlog] to change with it, and it may not. The attributes will be parsed together, potentially before your override affects the first one.
Even after I am using the interpolation syntax, and when I am using node[:my_app][:commitlog] in recipe it shows /foo/bar//commitlog

"Invalid use of Null" when using Str() with a Null Recordset field, but Str(Null) works fine

I'm banging my head against the wall on this one. I was looking at some old database reporting code written in VB6 and came across this line (the code is moving data from a "source" database into a reporting database):
rsTarget!VehYear = Trim(Str(rsSource!VehYear))
When rsSource!VehYear is Null, the above line generates an "Invalid use of Null" run-time error. If I break on the above line and type the following in the Immediate pane:
?rsSource!VehYear
It outputs Null. Fine, that makes sense. Next, I try to reproduce the error:
?Str(rsSource!VehYear)
I get an "Invalid use of Null" error.
However, if I type the following into the Immediate window:
?Str(Null)
I don't get an error. It simply outputs Null.
If I repeat the same experiment with Trim() instead of Str(), everything works fine. ?Trim(rsSource!VehYear) returns Null, as does ?Trim(Null). No run-time errors.
So, my question is, how can Str(rsSource!VehYear) possibly throw an "Invalid use of Null" error when Str(Null) does not, when I know that rsSource!VehYear is equal to Null?
Update: If I type the following in the Immediate window, it works as expected (no error occurs):
?Str(rsSource!VehYear.Value)
This outputs Null. Now, I know that rsSource!VehYear is actually an ADODB.Field instance, but Value is its default property, so Str should be operating on the Value property (which is Null). Even the error message ("Invalid use of Null") suggests that Str is receiving a Null parameter, but how can it treat Null differently in one case and not the other?
My only guess is the internal implementation of Str() is somehow failing to get the default property, and the "Invalid use of Null" error is happening for a different reason (something other than the parameter is causing the "Invalid use of Null", perhaps when it is trying to retrieve the default property from the Field object).
Does anyone have a more detailed, technical explanation for what is actually happening here?
In short:
?Str(rsSource!VehYear)
throws an "Invalid use of Null" error when rsSource!VehYear is Null, but
?Str(rsSource!VehYear.Value)
returns Null.
However, both Trim(rsSource!VehYear) and Trim(rsSource!VehYear.Value) return Null.
If you need a value other than a string, try using IsNull instead:
rsTarget!VehYear = IIf(IsNull(rsSource!VehYear), 0, rsSource!VehYear)
' Note 0 is the default value
The Str function will specifically check if a Null value is passed in and deal with it accordingly. When you pass in an object it attempts to convert the result of a default method to a String. The result of the default method is not passed into the Str method, but Field object is, so a check for the initial Null will fail. The Str function will continue to check the parameter type for datatypes that it supports when it realizes that it has an object, it will attempt to retrieve the default value. It doesn't re-attempt to deal with the default value as it did with the passed in argument, so the attempt to return a Null as a String will fail. It seems MS didn't expect a default value to be Null or any other invalid value for Str. For instance Str doesn't support an empty string either.
This was my workaround in the vb6-days:
rsTarget!VehYear = Trim(Str(rsSource!VehYear & ""))
the & "" will make sure there is allways at least an empty string to work with.
From memory, null database fields are Nothing (or possibly vbNull), which do not have the same rules applied to them as Null. You should just be able to do a quick check:
If (rsSource!VehYear Is Nothing) Then
' Null
Else
' Not null
End If

Object variable not set error in vb6

Private m_IncidentDateRange As New MCWebServiceCOMWrapper.DateRange
Public Property Get IncidentDateRange() As MCWebServiceCOMWrapper.DateRange
IncidentDateRange = m_IncidentDateRange
End Property
Public Property Let IncidentDateRange(ByRef vNewValue As MCWebServiceCOMWrapper.DateRange)
Set m_IncidentDateRange = vNewValue
End Property
Error comes up in Get method please help
I would check the documentation for the MCWebServiceCOMWrapper.DateRange object and see if you are required to initialized it with values before you can use it. (maybe set a beginning and end date).
Don't you need to use "Set" there?

Resources