Skip to content

Commit

Permalink
Merge pull request #4647 from pulibrary/4581-handle-patron-errors
Browse files Browse the repository at this point in the history
Catch when patron_hash is TrueClass
  • Loading branch information
sandbergja authored Dec 18, 2024
2 parents abddb2b + 0069df9 commit c6d2549
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/models/requests/alma_patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(uid:)
@uid = uid
end

def hash
def patron_hash
{
last_name:,
active_email:,
Expand Down
4 changes: 2 additions & 2 deletions app/models/requests/full_patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
module Requests
# FullPatron pulls all available data from both Alma and LDAP via Bibdata
class FullPatron
attr_reader :hash
attr_reader :patron_hash
def initialize(user: nil)
@hash = ::Bibdata.get_patron(user, ldap: true)
@patron_hash = ::Bibdata.get_patron(user, ldap: true)
end
end
end
4 changes: 2 additions & 2 deletions app/models/requests/patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def load_patron(user:)

def current_patron_hash(uid)
if alma_provider?
AlmaPatron.new(uid:).hash
AlmaPatron.new(uid:).patron_hash
else
FullPatron.new(user:).hash
FullPatron.new(user:).patron_hash
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/services/bibdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ def get_patron(user, ldap:)
build_api_patron(api_response:, user:)
rescue ServerError
Rails.logger.error('An error was encountered with the Patron Data Service.')
nil
{}
rescue PerSecondThresholdError => per_second_error
Rails.logger.error("The maximum number of HTTP requests per second for the Alma API has been exceeded.")
raise(per_second_error)
rescue ResourceNotFoundError
Rails.logger.error("404 Patron #{user.uid} cannot be found in the Patron Data Service.")
nil
{}
rescue ForbiddenError
Rails.logger.error("403 Not Authorized to Connect to Patron Data Service at #{api_base_uri}/patron/#{user.uid}")
nil
{}
rescue Faraday::ConnectionFailed
Rails.logger.error("Unable to connect to #{api_base_uri}")
nil
{}
rescue EmptyResponseError
Rails.logger.error("#{patron_uri} returned an empty patron response")
nil
{}
end

def holding_locations
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/account_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,23 @@
stub_request(:get, invalid_patron_record_uri)
.to_return(status: 404, body: '<html><title>Not Here</title><body></body></html>', headers: {})
patron = account_controller.send(:current_patron, invalid_user)
expect(patron).to be nil
expect(patron).to eq({})
end

it "returns a nil value when the application isn't authorized to access patron data" do
unauthorized_patron_record_uri = "#{Requests.config['bibdata_base']}/patron/#{unauthorized_user.uid}"
stub_request(:get, unauthorized_patron_record_uri)
.to_return(status: 403, body: '<html><title>Not Authorized</title><body></body></html>', headers: {})
patron = account_controller.send(:current_patron, unauthorized_user)
expect(patron).to be nil
expect(patron).to eq({})
end

it 'returns a nil value when the HTTP response to the API request has a 500 status code' do
valid_patron_record_uri = "#{Requests.config['bibdata_base']}/patron/#{valid_user.uid}"
stub_request(:get, valid_patron_record_uri)
.to_return(status: 500, body: 'Error', headers: {})
patron = account_controller.send(:current_patron, valid_user)
expect(patron).to be nil
expect(patron).to eq({})
end
end
end
21 changes: 21 additions & 0 deletions spec/features/requests/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
let(:temp_item_id) { '4815239' }
let(:temp_id_mfhd) { '5018096' }
let(:mutiple_items) { '9979171923506421' }
let(:the_senses) { '9951680203506421' }

let(:transaction_url) { "https://lib-illiad.princeton.edu/ILLiadWebPlatform/transaction" }
let(:transaction_note_url) { "https://lib-illiad.princeton.edu/ILLiadWebPlatform/transaction/1093806/notes" }
Expand Down Expand Up @@ -1441,5 +1442,25 @@
expect(confirm_email.subject).to eq("In Process Request")
end
end

context 'when the bibdata patron request fails' do
let(:user) { FactoryBot.create(:user) }
let(:first_item) { the_senses }

before do
stub_availability_by_holding_id(bib_id: '9951680203506421', holding_id: '22480938160006421')
stub_catalog_raw(bib_id: '9951680203506421')
stub_single_holding_location('annex$noncirc')
stub_request(:get, "#{Requests.config[:bibdata_base]}/patron/#{user.uid}?ldap=true")
.to_return(status: 500)
login_as user
end

it 'displays an error when the patron data can not be retreived' do
visit 'requests/9951680203506421?aeon=false&mfhd=22480938160006421'

expect(page).to have_content('A problem occurred looking up your library account.')
end
end
end
# rubocop:enable Metrics/BlockLength
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[
{
"barcode": "32101061062418",
"id": "23480938080006421",
"holding_id": "22480938160006421",
"copy_number": "1",
"status": "Available",
"status_label": "Item in place",
"status_source": "base_status",
"process_type": null,
"on_reserve": "N",
"item_type": "NoCirc",
"pickup_location_id": "annex",
"pickup_location_code": "annex",
"location": "annex$noncirc",
"label": "Forrestal Annex - Non-Circulating",
"description": "vol.6",
"enum_display": "vol.6",
"chron_display": "",
"in_temp_library": false
},
{
"barcode": "32101061062459",
"id": "23480938090006421",
"holding_id": "22480938160006421",
"copy_number": "1",
"status": "Available",
"status_label": "Item in place",
"status_source": "base_status",
"process_type": null,
"on_reserve": "N",
"item_type": "NoCirc",
"pickup_location_id": "annex",
"pickup_location_code": "annex",
"location": "annex$noncirc",
"label": "Forrestal Annex - Non-Circulating",
"description": "vol.5",
"enum_display": "vol.5",
"chron_display": "",
"in_temp_library": false
},
{
"barcode": "32101061062400",
"id": "23480938100006421",
"holding_id": "22480938160006421",
"copy_number": "1",
"status": "Available",
"status_label": "Item in place",
"status_source": "base_status",
"process_type": null,
"on_reserve": "N",
"item_type": "NoCirc",
"pickup_location_id": "annex",
"pickup_location_code": "annex",
"location": "annex$noncirc",
"label": "Forrestal Annex - Non-Circulating",
"description": "vol.4",
"enum_display": "vol.4",
"chron_display": "",
"in_temp_library": false
},
{
"barcode": "32101061062467",
"id": "23480938110006421",
"holding_id": "22480938160006421",
"copy_number": "1",
"status": "Available",
"status_label": "Item in place",
"status_source": "base_status",
"process_type": null,
"on_reserve": "N",
"item_type": "NoCirc",
"pickup_location_id": "annex",
"pickup_location_code": "annex",
"location": "annex$noncirc",
"label": "Forrestal Annex - Non-Circulating",
"description": "vol.3",
"enum_display": "vol.3",
"chron_display": "",
"in_temp_library": false
},
{
"barcode": "32101061062392",
"id": "23480938120006421",
"holding_id": "22480938160006421",
"copy_number": "1",
"status": "Available",
"status_label": "Item in place",
"status_source": "base_status",
"process_type": null,
"on_reserve": "N",
"item_type": "NoCirc",
"pickup_location_id": "annex",
"pickup_location_code": "annex",
"location": "annex$noncirc",
"label": "Forrestal Annex - Non-Circulating",
"description": "vol.2",
"enum_display": "vol.2",
"chron_display": "",
"in_temp_library": false
},
{
"barcode": "32101060781513",
"id": "23480938130006421",
"holding_id": "22480938160006421",
"copy_number": "1",
"status": "Available",
"status_label": "Item in place",
"status_source": "base_status",
"process_type": null,
"on_reserve": "N",
"item_type": "NoCirc",
"pickup_location_id": "annex",
"pickup_location_code": "annex",
"location": "annex$noncirc",
"label": "Forrestal Annex - Non-Circulating",
"description": "vol.1",
"enum_display": "vol.1",
"chron_display": "",
"in_temp_library": false
}
]
Loading

0 comments on commit c6d2549

Please sign in to comment.