NilClass# failed with TypeError: nil is not a symbol - ruby-on-rails-3.1

I am unable to send emails. i have already tried the following:
Converting type of handler from text to long text does'nt work as i am using postgres.
I have also tried restarting my workers and delayed jobs
and my delayed job is version is 3.0.1.
I am having problem on the line:
Notifier.delay.delivery_alert(u)
and my delivery_alert method is
def delivery_alert(user)
#user = user
#deliveries = #user.issues.includes(:copy => [:rentable]).current.by_last_status("to_be_delivered").map(&:copy).map(&:rentable)
#returns = #user.issues.includes(:copy => [:rentable]).current.by_last_status("marked_for_return").map(&:copy).map(&:rentable)
mail(:to => #user.email)
end
While on localhost i am getting the error:
[Worker(host:ubuntu pid:12169)] Class#delivery_alert failed with NoMethodError: undefined method `delivery_alert' for # - 5 failed attempts
for which i have added a patch in my lib folder
require 'yaml'
module Delayed
module Backend
module Base
def payload_object
YAML::ENGINE.yamler = 'psych'
#payload_object ||= YAML.load(self.handler)
rescue TypeError, LoadError, NameError, ArgumentError => e
raise DeserializationError,
"Job failed to load: #{e.message}. Handler: #{handler.inspect}"
end
end
end
class PerformableMailer
def perform
double = object.is_a?( String ) ? object.constantize : object
double.send(method_name, *args).deliver
end
end
end
by taking reference of
https://gist.github.com/oelmekki/2181381
but still my email are not going. Thanks in advance?

Related

Ruby why does rackup raise this undefined method error upon boot?

I have a class for writing recurring entries. The initializer verifies that the start date (#date_1) is after the end date (#date_2).
class Recurrent
include Format
attr_accessor :intitule, :categorie, :sens
attr_reader :mensualite, :date_1, :date_2
def initialize (user, date_1, date_2 = date_1.next_year)
#date_1 = date_1
#date_2 = date_2
#jour = Date.today
#user = user
#compte = #user.compte
# start date before end date ? :
inversees?
end
# cut for brievity
private
def inversees?
if #date_1 < #date_2
return true
else
raise StandardError.new("start date must be before end date.")
end
end
begin
inversees?(false)
rescue => error
puts error.message
end
end
when I start the server I get this error message :
[me#manjaro accountsapp]$ rackup
undefined method `inversees?' for Recurrent:Class
Although, when I provoke the error by entering wrong dates (and disabling the javascript test that I do before data is sent to the server) , I does work. Why does this error message appear?

Accessing the Receiving Object that Throws an Exception

While debugging a really strange issue with ActionMailer, I came to realize I didn't know how to access an object that was creating the exception. Not the exception, but the object itself.
begin
AppMailer.send_invoice(hostel_resident).deliver_later
flash[:success] = "Your invoice was sent successfully!"
rescue => msg
# display the system generated error message
flash[:error] = "#{msg}"
end
NoMethodError: undefined method `disposition_type' for #<Mail::UnstructuredField:0x009g71c2a68258>
This code works great to catch any exceptions and print the message.
However, how do I get ahold of the <Mail::UnstructuredField:0x009g71c2a68258> object? I'd like to be able to play around with this guy, read messages inside it, and just generally have access to it.
This has to be possible, but inspect doesn't help, cause is no use and backtrace just shows you where it happened. I need that object though, the receiver of the nonexistent method.
Thanks!
actionmailer (4.2.4)
mail (2.6.3)
Seems like you're using mail gem. This is a known issue which is already reported in the GitHub. See #851.
Try using different version of the gem, something in 2.6 series.
This seems to work, using receiver on NameError (which NoMethodError is a child of)
obj = Object.new
puts obj.to_s
begin
obj.do_something
rescue NoMethodError => e
puts e.message
puts e.receiver
end
# #<Object:0x007fa5ac84da88>
# undefined method `do_something' for #<Object:0x007fa5ac84da88>
# #<Object:0x007fa5ac84da88>
This seems to require ruby >= 2.3, to do this for < 2.3, AFAIK you have to do something like this (not tested in older rubies, but should work):
class MyNoMethodError < NoMethodError
attr_accessor :my_receiver
end
obj = Object.new
puts obj.to_s
begin
begin
obj.do_something
rescue NoMethodError => e
# rescue the exception and wrap it in the method that caused it, using `self` instead of `obj`
error = MyNoMethodError.new(e)
error.my_receiver = obj
raise error
end
rescue MyNoMethodError => c
puts c.inspect # custom exception stuff
puts c.cause.inspect # original exception stuff
puts c.my_receiver
end
# #<Object:0x007f884e846d58>
# #<MyNoMethodError: undefined method `do_something' for #<Object:0x007f884e846d58>>
# #<NoMethodError: undefined method `do_something' for #<Object:0x007f884e846d58>>
# #<Object:0x007f884e846d58>

Getting undefined error in mass text app

I'm creating a app that sends mass texts using a JSON file with the numbers and names. Every time I test load the app in IRB I get the error:
NameError: undefined local variable or method `data_from_file' for main:Object
from /home/qc/tep/Coding Stuff/Ruby/text app/main.rb:14:in `contacts_from_file'
I understand what the error means, but I don't understand why I'm getting the error, here's the source code:
require 'json'
def sanatize(numbers)
"+1" + number.gsub(/^1|\D/, "")
end
def numbers_from_file
file = open('numbers.json').read
JSON.parse(file)
end
def contacts_from_file
contacts= { }
data_from_file['feed']['entry'].each do |entry|
name = entry['gsx$name']['$t']
number = entry['gsx$number']['$t']
contacts[sanatize(number)] = name
end
contacts
end
def contact_numbers
contacts_from_file.keys
end
def contact_name
contacts_from_file[number]
end
And here's the JSON file:
{
'1**********' => 'Big Bird'
'1**********'} => 'Josh'
}
If anybody could help me and tell me why the data_from_file is "undefined" it would be extremely helpful, thank you ahead of time.
You never define data_from_file, you just try to read from it in the contects_from_file method.
Perhaps you meant numbers_from_file instead of data_from_file?

error_class=NoMethodError error="undefined method `bytesize' Fluentd

I have the below Fluentd plugin code:
require 'avro'
module Fluent
module TextFormatter
class Sample
end
class AvroFormatter < Formatter
Fluent::Plugin.register_formatter('avro', self)
config_param :schema_file, :string, :default => nil
config_param :schema_json, :string, :default => nil
def configure(conf)
super
if not (#schema_json.nil? ^ #schema_file.nil?) then
raise Fluent::ConfigError, 'schema_json or schema_file (but not both) is required'
end
if #schema_json.nil? then
#schema_json = File.read(#schema_file)
end
#schema = Avro::Schema.parse(#schema_json)
end
def format(tag, time, record)
handler = Sample.new()
end
end
end
end
And I need to instance the class "Sample" in the def "Format". The problem is that when I try to do a http POST against Fluentd the below error appears:
failed: error_class=NoMethodError error="undefined method `bytesize'
This error only appears when the class "Sample" is instanced. I'm new with ruby, and I don't know where is the problem. Should I create the class "Sample" in another file?
I think you're getting this error because code, that calls format expects string result, but instead it gets an instance of Sample class. Try to return some string instead.
You can also use this example here: http://docs.fluentd.org/articles/plugin-development#text-formatter-plugins.

Sinatra Error Handling in Ruby

I have a simple Sinatra rest and I am having trouble trapping an error. I also admit I am fairly new to Ruby and Sinatra.
When I raise and error in the post endpoint I want to report the incoming document. I need to either 1) handle the error within the post result (where I have access to #incoming) or 2) pass the incoming document to the error and report it there.
What is a better option, option 1 or option 2?
If I stay with option 1, how do I prevent error from picking up the error (as it seems to be doing now)
If I go to option 2, how do I pass incoming to error?
Below is a sample of my code:
post ('/result') do
begin
#incoming = JSON.parse(request.body.read)
//do something that causes an error
rescue
e = env['sinatra.error']
url = request.url
ip = request.ip
#actlogpassblock = { :message => e.message,
:path => url,
:ip => ip,
:timestamp => Time.new,
:type => "500",
:sub => "RES",
:payload => #incoming
}
action_log.insert(#actlogpassblock)
status 500
end
end
error do
status 500
e = env['sinatra.error']
url = request.url
ip = request.ip
backtrace = "Application error\n#{e}\n#{e.backtrace.join("\n")}"
#actlogpassblock = { :message => e.message,
:path => url,
:ip => ip,
:timestamp => Time.new,
:type => "500",
:backtrace => backtrace
}
action_log.insert(#actlogpassblock)
{:result => 'Ah Shucks! Something went wrong'}.to_json
end
If I stay with option 1, how do I prevent error from picking up the
error (as it seems to be doing now)
According to the docs:
The error handler is invoked any time an exception is raised from a
route block...
However, that only applies to uncaught exceptions. Try this:
require 'sinatra'
set :show_exceptions, false
get '/' do
begin
raise ZeroDivisionError
rescue ZeroDivisionError
"rescue clause"
end
end
error do
"sinatra error handler"
end
Then try this:
get '/' do
raise ZeroDivisionError
end
error do
"sinatra error handler"
end
Also, you can tailor the error handler to only catch certain exceptions thereby avoiding some exceptions, e.g.
error IndexError do ...
or
error MyCustomException do ...
or
error 400..510 do
But for the catch all version:
error do
you can't stop that from executing when an uncaught exception occurs in a route block...unless:
The error handlers will only be invoked, however, if both the Sinatra
:raise_errors and :show_exceptions configuration options have been set
to false...
:raise_errors defaults to true in the "test" environment and to false
on other environments.
:show_exceptions value defaults to true in the
"development" environment and to false on other environments
The author of Sintra has said: "This [behavior] is
intentional. The idea is that error blocks will hide the issue and you
usually don't want to do this in development mode.
https://github.com/sul-dlss/sdr-services-app/blob/master/Sinatra-error-handling.md
If I go to option 2, how do I pass incoming to error?
An #variable that is created inside a route block can be seen inside an error block. Try this:
require 'sinatra'
set :show_exceptions, false
get '/' do
#incoming = "hello world" #create #variable
raise ZeroDivisionError
end
error ZeroDivisionError do
#incoming #retrieve #variable
end
After entering the url http://localhost:4567 in your browser, you should see "hello world" on the returned web page.
The reason that works is because an #variable attaches itself to whatever object is self at the time the #variable is created; likewise when an #variable is summoned, it is retrieved from whatever object is self at that time. When Sinatra executes either the route block or the error block it sets self to the same object.
Option 2 seems nice because it separates the error handling code from the application code.

Resources