Skip to content

Commit

Permalink
surface SymphonyReader::RecordIncompleteError message to caller
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Aug 30, 2019
1 parent b1ad083 commit 64fbad2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Style/FormatString:
- 'app/models/symphony_reader.rb'
- 'config/initializers/okcomputer.rb'
- 'spec/controllers/marcxml_controller_spec.rb'
- 'spec/requests/metadata_refresh_spec.rb'
- 'spec/models/symphony_reader_spec.rb'

# Offense count: 1
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/marcxml_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
class MarcxmlController < ApplicationController #:nodoc:
before_action :set_marcxml_resource

rescue_from(SymphonyReader::RecordIncompleteError) do |e|
render status: 500, plain: e.message
end

def catkey
render plain: @marcxml.catkey
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/metadata_refresh_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
class MetadataRefreshController < ApplicationController
before_action :load_item

rescue_from(SymphonyReader::RecordIncompleteError) do |e|
render status: 500, plain: e.message
end

def refresh
status = RefreshMetadataAction.run(@item)
@item.save if status
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ObjectsController < ApplicationController
render status: 500, plain: e.message
end

rescue_from(SymphonyReader::RecordIncompleteError) do |e|
render status: 500, plain: e.message
end

# Register new objects in DOR
def create
Rails.logger.info(params.inspect)
Expand Down
28 changes: 26 additions & 2 deletions spec/controllers/marcxml_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,42 @@

describe 'GET marcxml' do
it 'retrieves MARCXML' do
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: resource.catkey }).to_return(body: '{}')
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: resource.catkey }).to_return(body: '{}', headers: { 'Content-Length': 2 })

get :marcxml, params: { catkey: '12345' }
expect(response.body).to start_with '<record'
end

context 'when incomplete response from Symphony' do
before do
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: resource.catkey }).to_return(body: '{}', headers: { 'Content-Length': 0 })
end

it 'returns a 500 error' do
get :marcxml, params: { catkey: '12345' }
expect(response.status).to eq(500)
expect(response.body).to eq('Incomplete response received from Symphony for 12345 - expected 0 bytes but got 2')
end
end
end

describe 'GET mods' do
it 'transforms the MARCXML into MODS' do
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: resource.catkey }).to_return(body: '{}')
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: resource.catkey }).to_return(body: '{}', headers: { 'Content-Length': 2 })
get :mods, params: { catkey: '12345' }
expect(response.body).to match(/mods/)
end

context 'when incomplete response from Symphony' do
before do
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: resource.catkey }).to_return(body: '{}', headers: { 'Content-Length': 0 })
end

it 'returns a 500 error' do
get :mods, params: { catkey: '12345' }
expect(response.status).to eq(500)
expect(response.body).to eq('Incomplete response received from Symphony for 12345 - expected 0 bytes but got 2')
end
end
end
end
12 changes: 11 additions & 1 deletion spec/controllers/objects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
render_views

context 'error handling' do
let(:errmsg) { 'my unique snowflake error message' }

it 'returns a 409 error with location header when an object already exists' do
allow(RegistrationService).to receive(:register_object).and_raise(Dor::DuplicateIdError.new('druid:existing123obj'))
post :create
Expand All @@ -34,9 +36,17 @@
end

it 'returns a 422 error when an object has a bad name' do
allow(RegistrationService).to receive(:register_object).and_raise(ArgumentError)
allow(RegistrationService).to receive(:register_object).and_raise(ArgumentError.new(errmsg))
post :create
expect(response.status).to eq(422)
expect(response.body).to eq(errmsg)
end

it 'returns a 500 error when the SymphonyReader gets an incomplete response' do
allow(RegistrationService).to receive(:register_object).and_raise(SymphonyReader::RecordIncompleteError.new(errmsg))
post :create
expect(response.status).to eq(500)
expect(response.body).to eq(errmsg)
end
end

Expand Down
33 changes: 26 additions & 7 deletions spec/requests/metadata_refresh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@

before do
allow(Dor).to receive(:find).and_return(object)
allow(RefreshMetadataAction).to receive(:run).and_return('<xml />')
allow(object).to receive(:save)
end

it 'updates the metadata and saves the changes' do
post '/v1/objects/druid:mk420bs7601/refresh_metadata',
headers: { 'X-Auth' => "Bearer #{jwt}" }
expect(response).to be_successful
expect(RefreshMetadataAction).to have_received(:run).with(object)
expect(object).to have_received(:save)
context 'when happy path' do
before do
allow(RefreshMetadataAction).to receive(:run).and_return('<xml />')
end

it 'updates the metadata and saves the changes' do
post '/v1/objects/druid:mk420bs7601/refresh_metadata',
headers: { 'X-Auth' => "Bearer #{jwt}" }
expect(response).to be_successful
expect(RefreshMetadataAction).to have_received(:run).with(object)
expect(object).to have_received(:save)
end
end

context 'when incomplete response from Symphony' do
before do
allow(object.identityMetadata).to receive(:otherId).and_return(['catkey:666'])
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: '666' }).to_return(body: '{}', headers: { 'Content-Length': 0 })
end

it 'returns a 500 error' do
post '/v1/objects/druid:mk420bs7601/refresh_metadata',
headers: { 'X-Auth' => "Bearer #{jwt}" }
expect(response.status).to eq(500)
expect(response.body).to eq('Incomplete response received from Symphony for 666 - expected 0 bytes but got 2')
end
end
end

0 comments on commit 64fbad2

Please sign in to comment.