I try to connect soap via savon, I get an error:
(a:InternalServiceFault) Object reference not set to an instance of an object.
Extracted source (around line #85):
def raise_soap_and_http_errors!
raise soap_fault if soap_fault?
raise http_error if http_error?
end
here is my code:
client = Savon.client(wsdl: 'http://moghim24.ir:8080/Moghim24Scripts/Moghim24Services.svc?wsdl')
response = client.call(:open_tempfllist) do
message fd: '96/01/01'.to_s, ld: '97/01/01'.to_s, cust: '1005'.to_s, pass: '233344'.to_s
end
render :json => response.body
the error happens at this raise soap_fault if soap_fault?
Related
I am using appsignal gem to track if there is an error processing in my app.
This case i do call external API using faraday.
def truck_information(req_params)
response = #conn.post('truck/info') do |req|
req.headers['Content-Type'] = 'application/json'
req.body = req_params
end
return JSON.parse(response.body) if response_successful?(response)
response_error(response)
end
def response_successful?(response)
response.status == 200
end
def response_error(response)
err = NctError, "Code: #{response.status}, response: #{response.body}"
Appsignal.set_error(err)
raise NctError, I18n.t('error_messages.ppob.server_error')
end
my truck_information is used to call external api. and if success i will parse it to json. but if error i will call response_error method parser to create custom class error (NctError) and i want to send to appsignal to show the error without breaking the application process.
But when i was tested it, it doesn't send to appsignal. How to do send error to appsignal, even if it doesn't crash a request? because i need to track the error.
Thank you
You could try Appsignal.send_error
Appsignal.send_error(err)
If above doesn't work either, then set_error and send_error may only work with Exception:
def response_error(response)
raise NctError, I18n.t('error_messages.ppob.server_error')
rescue => e
Appsignal.send_error(e) do |transaction|
transaction.params = { code: response.status, response: response.body }
end
end
In a Rails project, I have following error:
undefined local variable or method ` response' for #<Deliveries::CheckJobService:0x00007fce8548dd60> Did you mean? response
And here is the code:
delivery_status = response['status']
I don't see the error
I try new things, and the error is even weirder:
def call
return false if #order.stuart_job_id.nil?
response = stuart_check_job
if response.nil?
#order.update(delivery_status: 'sth went wrong')
else
delivery_status = 2
delivered_at = 4
#order.update(delivery_status: delivery_status, delivered_at: delivered_at)
end
return true
end
This is the error:
undefined local variable or method ` 2' for #<Deliveries::CheckJobService:0x00007fce8490fc40>
I don't see any single quotation
I'm missing something here, but for some reason my begin then rescue Ruby code isn't capturing this error:
#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = Unprocessable Entity.>
This is my code:
begin
ShopifyAPI::CarrierService.create(with some arguments)
rescue StandardError => e
pp e
end
It doesn't ever capture it. In my rescue section I've tried the above but also:
rescue Exception => e
rescue ActiveResource::Errors => e
All with no luck. Where did I go astray?
Thanks
EDIT:
This is the full error, it not really anymore info, but here goes:
#<ShopifyAPI::CarrierService:0x0000000357a0a0
#attributes=
{"name"=>"XXXX",
"callback_url"=>
"https://XX-XX-XX-XX.c9users.io/receive_rate_request",
"format"=>"json",
"service_discovery"=>"true",
"carrier_service_type"=>"api"},
#errors=
#<ActiveResource::Errors:0x00000003578930
#base=#<ShopifyAPI::CarrierService:0x0000000357a0a0 ...>,
#messages={:base=>["you already have XXX set up for this shop"]}>,
#persisted=false,
#prefix_options={},
#remote_errors=
#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = Unprocessable Entity.>,
#validation_context=nil>
That's it!
Because it is not raising an exception, If you want to raise the exception when the response is false, you may have to use create with bang
begin
ShopifyAPI::CarrierService.create!(with some arguments)
rescue StandardError => e
pp e
end
According to the ActiveResource code (lib/active_resource/base.rb):
# <tt>404</tt> is just one of the HTTP error response codes that Active Resource will handle with its own exception. The
# following HTTP response codes will also result in these exceptions:
#
# * 200..399 - Valid response. No exceptions, other than these redirects:
# * 301, 302, 303, 307 - ActiveResource::Redirection
# * 400 - ActiveResource::BadRequest
# * 401 - ActiveResource::UnauthorizedAccess
# * 403 - ActiveResource::ForbiddenAccess
# * 404 - ActiveResource::ResourceNotFound
...
# * 422 - ActiveResource::ResourceInvalid (rescued by save as validation errors)
So it indicates that 422's are rescued by save on validation, which happens when .create is fired, and are bubbled up as validation errors instead.
Looking at lib/active_resource/validations.rb, you can see the ResourceInvalid exception is gobbled:
# Validate a resource and save (POST) it to the remote web service.
# If any local validations fail - the save (POST) will not be attempted.
def save_with_validation(options={})
perform_validation = options[:validate] != false
# clear the remote validations so they don't interfere with the local
# ones. Otherwise we get an endless loop and can never change the
# fields so as to make the resource valid.
#remote_errors = nil
if perform_validation && valid? || !perform_validation
save_without_validation
true
else
false
end
rescue ResourceInvalid => error
# cache the remote errors because every call to <tt>valid?</tt> clears
# all errors. We must keep a copy to add these back after local
# validations.
#remote_errors = error
load_remote_errors(#remote_errors, true)
false
end
So I wonder if it's logging that an exception happened, but is not actually raising an exception because it turns it in to a return false. It does say "local validations" in the comment, but sets remote_errors, so it's not perfectly clear where this code path is executed.
How do I catch this exception?
begin
data = Base64.strict_decode64(data) # decode data
...
rescue ArgumentError => e
logger.severe "Could not decrypt data: #{e}, #{data}"
Log
ArgumentError (invalid base64):
config/application.rb:32:in `decrypt'
I realized that I modified config/application, which requires restarting the server. Which is odd, because the error page showed the updated source code and the line number pointing to begin!
I found I did not have access to logger either! This is the finished product:
begin
data = Base64.strict_decode64(data) # decode data
...
rescue ArgumentError => e
Rails.logger.warn "Could not decrypt data: #{e}, #{data}"
text = ""
end
I'm trying to force an Net::SFTP::StatusException error in my spec and then validate that my code traps it.
Code:
def process_SFTP(username, password, csvfile)
begin
mySftpWrapper.new
mySftpWrapper.process_CSV_file(csvfile)
rescue RuntimeError => other_problem
logger.info(other_problem.message)
rescue Net::SFTP::StatusException => sftp_problem
logger.info(sftp_problem.message)
end
end
RSpec:
let(:wrapper) { mySftpWrapper.new }
before(:all) do
#wrapper_mock = double('mySftpWrapper')
mySftpWrapper.stub(:new).and_return(#wrapper_mock)
#logger_mock = double('ActiveSupport::BufferedLogger')
end
it "should trap a Net::SFTP::StatusException" do
sftp_response = Object.new
def sftp_response.code; code = 2 end
def sftp_response.message; message = "no such file" end
# Force exception here
#wrapper_mock.stub(:process_SFTP).with("username", "password", "nonexistentfile").and_raise(Net::SFTP::StatusException.new(sftp_response))
# Verify that logger.info received the forced error
#logger_mock.stub(:info).should_receive(/no such file/)
wrapper.process_SFTP("date", "username", "password", "nonexistentfile")
end
However, the statement I use to generate the Net::SFTP::StatusException doesn't throw the correct exception ("Net::SFTP::StatusException"). Instead it throws #<Net::SFTP::StatusException: Net::SFTP::StatusException>
How do I force the correct StatusException?
Any ideas are much appreciated!
As it turns out, the spec example is throwing exactly the correct Exception. The problem is that the Net::SFTP::StatusException is subclassed from RuntimeError:
net-sftp-2.0.5/lib/net/sftp/errors.rb:
module Net; module SFTP
# The base exception class for the SFTP system.
class Exception < RuntimeError; end
# A exception class for reporting a non-success result of an operation.
class StatusException < Net::SFTP::Exception
and the Net::SFTP::StatusException was getting thrown, but it was getting trapped by the RuntimeError clause before reaching the StatusException clause. Exactly what a spec is for!