Skip to content

Commit

Permalink
Merge pull request #144 from sanger/bugfix/exception-handling
Browse files Browse the repository at this point in the history
Fix exception handling
  • Loading branch information
pjvv authored May 4, 2020
2 parents 9039894 + 527c35d commit ede42ac
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/controllers/process_plates_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
class ProcessPlatesController < ApplicationController
require 'wrangler'
require 'lighthouse'

skip_before_filter :configure_api, except: [:create]

Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
config.admin_email = '[email protected]'

# https://github.com/sanger/wrangler
config.wrangler_url = 'http://example.com/tube_rack'
config.wrangler_url = 'http://127.0.0.1:5001/tube_rack'

# https://github.com/sanger/lighthouse
config.lighthouse_host = 'http://127.0.0.1:5000'
Expand Down
31 changes: 16 additions & 15 deletions lib/lighthouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ class Lighthouse
def self.call_api(barcodes)
responses = []

barcodes.each do |barcode|
url = URI.parse("#{Rails.application.config.lighthouse_host}/plates/new")
begin
barcodes.each do |barcode|
url = URI.parse("#{Rails.application.config.lighthouse_host}/plates/new")

Rails.logger.debug("Trying POST to '#{url}' with barcode '#{barcode}'")
Rails.logger.debug("Trying POST to '#{url}' with barcode '#{barcode}'")

req = Net::HTTP::Post.new(url.to_s, 'Content-Type' => 'application/json')
req.body = { barcode: barcode }.to_json
req = Net::HTTP::Post.new(url.to_s, 'Content-Type' => 'application/json')
req.body = { barcode: barcode }.to_json

res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end

responses << { code: res.code, body: res.body }
responses << { code: res.code, body: res.body }
end
rescue StandardError => e
Rails.logger.error(e)
nil
else
Rails.logger.info("Sent POST requests to lighthouse service: #{responses}")
end

Rails.logger.info("Sent POST requests to lighthouse service: #{responses}")

responses
rescue StandardError => e
Rails.logger.error(e)
nil
end
end
29 changes: 15 additions & 14 deletions lib/wrangler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ class Wrangler
def self.call_api(barcodes)
responses = []

barcodes.each do |barcode|
url = URI.parse("#{Rails.application.config.wrangler_url}/#{barcode}")
begin
barcodes.each do |barcode|
url = URI.parse("#{Rails.application.config.wrangler_url}/#{barcode}")

Rails.logger.debug("Trying GET to: #{url}")
Rails.logger.debug("Trying GET to: #{url}")

req = Net::HTTP::Get.new(url.to_s)
req = Net::HTTP::Get.new(url.to_s)

res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end

responses << { barcode: barcode, code: res.code }
responses << { barcode: barcode, code: res.code }
end
rescue StandardError => e
Rails.logger.error(e)
nil
else
Rails.logger.info("Sent GET requests to wrangler: #{responses}")
end

Rails.logger.info("Sent GET requests to wrangler: #{responses}")

responses
rescue StandardError => e
Rails.logger.error(e)
nil
end
end

0 comments on commit ede42ac

Please sign in to comment.