raise new SavingError when any saving call fails

pull/14388/head
Gonzalo Riestra 6 years ago
parent 19bab9defa
commit d8433a37e0

@ -17,5 +17,12 @@ module CartoGearsApi
@errors = errors
end
end
# Thrown when data could not be saved due to an internal error
class SavingError < StandardError
def initialize
super("There was an error while saving the data")
end
end
end
end

@ -81,6 +81,7 @@ module CartoGearsApi
#
# @raise [Errors::RecordNotFound] if the user could not be found in the database
# @raise [Errors::ValidationFailed] if the password validation failed
# @raise [Errors::SavingError] if the password could not be saved because of an internal error
def change_password(user_id, new_password)
user = find_user(user_id)
@ -88,8 +89,11 @@ module CartoGearsApi
user.password_confirmation = new_password
raise CartoGearsApi::Errors::ValidationFailed.new(user.errors) unless user.errors.empty?
user.save(raise_on_failure: true)
user.update_in_central
user.save(raise_on_failure: true)
rescue CartoDB::CentralCommunicationFailure, Sequel::ValidationFailed
raise CartoGearsApi::Errors::SavingError.new
end
private

@ -95,7 +95,7 @@ describe CartoGearsApi::Users::UsersService do
expect {
service.change_password(@user.id, 'new_password')
}.to raise_error(Sequel::ValidationFailed, 'Saving error')
}.to raise_error(CartoGearsApi::Errors::SavingError)
end
it 'raises an exception if update_in_central operation fails' do
@ -103,7 +103,7 @@ describe CartoGearsApi::Users::UsersService do
expect {
service.change_password(@user.id, 'new_password')
}.to raise_error(CartoDB::CentralCommunicationFailure)
}.to raise_error(CartoGearsApi::Errors::SavingError)
end
end
end

Loading…
Cancel
Save