Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit b9204b5

Browse files
authored
fix(services): service instance create/update errors (cloudfoundry#1736)
- Fixes an issue where updating a user-provided service instance to have the same name as an existing service instance caused a HTTP 500 Internal Server Error - Makes message validation errors more consistent with other endpoints - they now fail with HTTP 422 Unprocessable Entity - Improves testing of error details [#173416145](https://www.pivotaltracker.com/story/show/173416145)
1 parent 5dd3863 commit b9204b5

4 files changed

Lines changed: 195 additions & 43 deletions

File tree

app/actions/service_instance_update_user_provided.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module VCAP::CloudController
44
class ServiceInstanceUpdateUserProvided
55
include ServiceInstanceCreateMixin
66

7-
class InvalidUserProvidedServiceInstance < ::StandardError
7+
class UnprocessableUpdate < ::StandardError
88
end
99

1010
def initialize(service_event_repository)
@@ -37,7 +37,7 @@ def update(service_instance, message)
3737
attr_reader :service_event_repository
3838

3939
def error!(message)
40-
raise InvalidUserProvidedServiceInstance.new(message)
40+
raise UnprocessableUpdate.new(message)
4141
end
4242
end
4343
end

app/controllers/v3/service_instances_controller.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,18 @@ def create_managed(message, space:)
229229

230230
def update_user_provided(service_instance)
231231
message = ServiceInstanceUpdateUserProvidedMessage.new(hashed_params[:body])
232-
bad_request!(message.errors.full_messages) unless message.valid?
232+
unprocessable!(message.errors.full_messages) unless message.valid?
233233

234234
service_event_repository = VCAP::CloudController::Repositories::ServiceEventRepository::WithUserActor.new(user_audit_info)
235235
service_instance = ServiceInstanceUpdateUserProvided.new(service_event_repository).update(service_instance, message)
236236
render status: :ok, json: Presenters::V3::ServiceInstancePresenter.new(service_instance)
237+
rescue ServiceInstanceUpdateUserProvided::UnprocessableUpdate => api_err
238+
unprocessable!(api_err.message)
237239
end
238240

239241
def update_managed(service_instance)
240242
message = ServiceInstanceUpdateManagedMessage.new(hashed_params[:body])
241-
bad_request!(message.errors.full_messages) unless message.valid?
243+
unprocessable!(message.errors.full_messages) unless message.valid?
242244

243245
if message.service_plan_guid
244246
service_plan = ServicePlan.first(guid: message.service_plan_guid)
@@ -315,15 +317,15 @@ def can_write_space?(space)
315317

316318
def build_create_message(params)
317319
generic_message = ServiceInstanceCreateMessage.new(params)
318-
invalid_param!(generic_message.errors.full_messages) unless generic_message.valid?
320+
unprocessable!(generic_message.errors.full_messages) unless generic_message.valid?
319321

320322
specific_message = if generic_message.type == 'managed'
321323
ServiceInstanceCreateManagedMessage.new(params)
322324
else
323325
ServiceInstanceCreateUserProvidedMessage.new(params)
324326
end
325327

326-
invalid_param!(specific_message.errors.full_messages) unless specific_message.valid?
328+
unprocessable!(specific_message.errors.full_messages) unless specific_message.valid?
327329
specific_message
328330
end
329331

0 commit comments

Comments
 (0)