Commit d620ea7643e40d7edce9007dac8387929c90554a
1 parent
e073b436
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
treating exception msg to client
Showing
3 changed files
with
38 additions
and
9 deletions
Show diff stats
lib/noosfero/api/helpers.rb
| 1 | +require 'grape' | |
| 2 | + | |
| 1 | 3 | module Noosfero; |
| 2 | 4 | |
| 3 | 5 | module API |
| ... | ... | @@ -296,7 +298,12 @@ |
| 296 | 298 | end |
| 297 | 299 | end |
| 298 | 300 | if d[:provider] == 'serpro' |
| 299 | - raise ArgumentError, "Environment api_captcha_settings verify_uri not defined" if d[:verify_uri].nil? | |
| 301 | + #raise ArgumentError, "Environment api_captcha_settings verify_uri not defined" if d[:verify_uri].nil? | |
| 302 | + if d[:verify_uri].nil? | |
| 303 | + msg="Environment api_captcha_settings verify_uri not defined" | |
| 304 | + log msg | |
| 305 | + return client_message(_('Captcha validation error'), msg) | |
| 306 | + end | |
| 300 | 307 | return verify_serpro_captcha(d[:serpro_client_id], params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], d[:verify_uri]) |
| 301 | 308 | end |
| 302 | 309 | raise ArgumentError, "Environment api_captcha_settings provider not defined" |
| ... | ... | @@ -366,15 +373,37 @@ |
| 366 | 373 | begin |
| 367 | 374 | body = http.request(request).body |
| 368 | 375 | rescue Exception => e |
| 369 | - logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | |
| 370 | - logger.error e | |
| 371 | - return _("Serpro captcha error: #{e.message}") | |
| 376 | + log_exception(e) | |
| 377 | + return client_message(_('Internal captcha validation error'),"Serpro captcha error: #{e.message}") | |
| 372 | 378 | end |
| 373 | 379 | return _("Wrong captcha text, please try again") if body == 0 |
| 374 | 380 | return _("Token not found") if body == 2 |
| 375 | 381 | body == '1' ? true : body |
| 376 | 382 | end |
| 377 | 383 | |
| 384 | + | |
| 385 | + # custom_message[:prepend2log] -> Prepend2log gives more details to the application log | |
| 386 | + def log_exception(e, prepend_message2log=nil) | |
| 387 | + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | |
| 388 | + logger.formatter = GrapeLogging::Formatters::Default.new | |
| 389 | + e.message = "#{prepend_message2log} e.message" if prepend_message2log.present? | |
| 390 | + puts e.message | |
| 391 | + logger.error e | |
| 392 | + end | |
| 393 | + | |
| 394 | + # message[:user_message] -> Displays the message directly to user | |
| 395 | + # message[:console_message] -> Displays the message to the javascript console | |
| 396 | + def client_message(user_message, console_message) | |
| 397 | + message = {single_message: true, user_message: user_message, console_message: console_message} | |
| 398 | + message.to_json if message.present? | |
| 399 | + end | |
| 400 | + | |
| 401 | + def log(message) | |
| 402 | + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) | |
| 403 | + logger.formatter = GrapeLogging::Formatters::Default.new | |
| 404 | + logger.error message | |
| 405 | + end | |
| 406 | + | |
| 378 | 407 | end |
| 379 | 408 | end |
| 380 | 409 | end | ... | ... |
test/unit/api/helpers_test.rb
| ... | ... | @@ -247,9 +247,8 @@ class APIHelpersTest < ActiveSupport::TestCase |
| 247 | 247 | params = {} |
| 248 | 248 | params[:txtToken_captcha_serpro_gov_br] = '4324343' |
| 249 | 249 | params[:captcha_text] = '4324343' |
| 250 | - logger = Logger.new(File.join(Rails.root, 'log', 'test_api.log')) | |
| 251 | - stubs(:logger).returns(logger) | |
| 252 | - assert_equal test_captcha('127.0.0.1', params, environment), 'Serpro captcha error: getaddrinfo: Name or service not known' | |
| 250 | + r = test_captcha('127.0.0.1', params, environment) | |
| 251 | + assert_equal 'Serpro captcha error: getaddrinfo: Name or service not known', JSON.parse(r)['console_message'] | |
| 253 | 252 | end |
| 254 | 253 | |
| 255 | 254 | ... | ... |
test/unit/api/session_test.rb
| ... | ... | @@ -39,7 +39,7 @@ class SessionTest < ActiveSupport::TestCase |
| 39 | 39 | assert_equal 400, last_response.status |
| 40 | 40 | end |
| 41 | 41 | |
| 42 | - should 'detected error, Name or service not known, for Serpro Captcha communication' do | |
| 42 | + should 'detected error, Name or service not known, for Serpro captcha communication' do | |
| 43 | 43 | environment = Environment.default |
| 44 | 44 | environment.api_captcha_settings = { |
| 45 | 45 | enabled: true, |
| ... | ... | @@ -51,7 +51,8 @@ class SessionTest < ActiveSupport::TestCase |
| 51 | 51 | params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com", |
| 52 | 52 | :txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'} |
| 53 | 53 | post "/api/v1/register?#{params.to_query}" |
| 54 | - assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", JSON.parse(last_response.body)["message"] | |
| 54 | + message = JSON.parse(last_response.body)['message'] | |
| 55 | + assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", JSON.parse(message)['console_message'] | |
| 55 | 56 | end |
| 56 | 57 | |
| 57 | 58 | ... | ... |