fix /filter api - prevent error on exception conditions

This commit is contained in:
Nuwan 2023-12-14 11:34:42 +05:30
parent b0672a9cb3
commit 84481a4fec
2 changed files with 22 additions and 14 deletions

View File

@ -8,7 +8,7 @@ module JamRuby
me: { label: 'ME', min: -1, max: -1 }, me: { label: 'ME', min: -1, max: -1 },
unknown: { label: 'UNKNOWN', min: -2, max: -2 } unknown: { label: 'UNKNOWN', min: -2, max: -2 }
}; };
def self.filter(user, remote_ip, params) def self.filter(user, remote_ip, params)
#debugger #debugger
latency_good = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_good]) latency_good = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_good])
@ -17,8 +17,12 @@ module JamRuby
offset = [params[:offset].to_i, 0].max offset = [params[:offset].to_i, 0].max
limit = [params[:limit].to_i, 20].max limit = [params[:limit].to_i, 20].max
filter_params = {} filter_params = {}
filter_params.merge!(from_location: params[:from_location] ? '1' : '0') if params[:from_location].present? && params[:from_location].to_s == 'true'
filter_params.merge!(from_location: "1")
else
filter_params.merge!(from_location: "0")
end
genres = params[:genres] genres = params[:genres]
filter_params.merge!(genres: genres) if genres filter_params.merge!(genres: genres) if genres
@ -68,14 +72,14 @@ module JamRuby
[search, latency_data, nextOffset] [search, latency_data, nextOffset]
rescue => exception rescue => exception
logger.debug("Latency exception: #{exception.message}") #logger.debug("Latency exception: #{exception.message}")
Bugsnag.notify(exception) do |report| Bugsnag.notify(exception) do |report|
report.severity = "error" report.severity = "error"
report.add_tab(:latency, { report.add_tab(:latency, {
params: params, params: params,
user_id: user.id, user_id: user.id,
name: user.name, name: user.name,
url: filter_latency_url, url: latency_url,
}) })
end end
raise exception raise exception
@ -83,9 +87,7 @@ module JamRuby
end end
def self.users_latency_data(user_obj, remote_ip, latency_good, latency_fair, latency_high, filter_opts, offset, limit) def self.users_latency_data(user_obj, remote_ip, latency_good, latency_fair, latency_high, filter_opts, offset, limit)
filter_latency_url = "#{APP_CONFIG.latency_data_host}/search_users" uri = URI(latency_url)
uri = URI(filter_latency_url)
begin begin
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if APP_CONFIG.latency_data_host.start_with?("https://") http.use_ssl = true if APP_CONFIG.latency_data_host.start_with?("https://")
@ -138,22 +140,29 @@ module JamRuby
return { data: latency_data, next: nextOffset } return { data: latency_data, next: nextOffset }
else else
logger.debug("Latency response failed: #{response}")
Bugsnag.notify("LatencyResponseFailed") do |report| Bugsnag.notify("LatencyResponseFailed") do |report|
report.severity = "faliure" report.severity = "faliure"
report.add_tab(:latency, { report.add_tab(:latency, {
user_id: user_obj.id, user_id: user_obj.id,
name: user_obj.name, name: user_obj.name,
params: params, params: req_params,
url: filter_latency_url, url: latency_url,
code: response.code, code: response.code,
body: response.body, body: response.body,
}) })
end end
Rails.logger.debug("Latency response failed: #{response.code} #{response.body}")
raise Exception.new("#{response.code}: #{response.body}")
end end
rescue => exception rescue => exception
raise exception raise exception
end end
end end
private
def self.latency_url
"#{APP_CONFIG.latency_data_host}/client#"
end
end end
end end

View File

@ -181,10 +181,9 @@ class ApiSearchController < ApiController
def filter def filter
begin begin
@search, @latency_data, @nextOffset = JamRuby::MusicianFilter.filter(current_user, request.remote_ip, params) @search, @latency_data, @nextOffset = JamRuby::MusicianFilter.filter(current_user, request.remote_ip, params)
Rails.logger.debug("=====SEARCH : #{@search.results.inspect}")
Rails.logger.debug("=====LATENCY : #{@latency_data}")
respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/filter' respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/filter'
rescue rescue => ex
Rails.logger.debug("=====LATENCY EXCEPTION : #{ex.message}")
render json: {}, status: 500 render json: {}, status: 500
end end
end end