Skip to content

Commit

Permalink
add uid to rest api error messages. #690
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 4, 2021
1 parent 73942d5 commit 9ecbcba
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 122 deletions.
8 changes: 4 additions & 4 deletions app/controllers/client_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def create
serialized_json,
status: :created
else
Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors),
# Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors, uid: @client_prefix.uid),
status: :unprocessable_entity
end
end
Expand All @@ -157,8 +157,8 @@ def destroy
Rails.logger.warn message
head :no_content
else
Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors),
# Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors, uid: @client_prefix.uid),
status: :unprocessable_entity
end
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def create
render json: ClientSerializer.new(@client, options).serialized_json,
status: :created
else
Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors),
# Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors, uid: @client.uid),
status: :unprocessable_entity
end
end
Expand All @@ -190,8 +190,8 @@ def update
render json: ClientSerializer.new(@client, options).serialized_json,
status: :ok
else
Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors),
# Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors, uid: @client.uid),
status: :unprocessable_entity
end
end
Expand All @@ -213,8 +213,8 @@ def destroy
end
head :no_content
else
Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors),
# Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors, uid: @client.uid),
status: :unprocessable_entity
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/concerns/error_serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ErrorSerializable
extend ActiveSupport::Concern

included do
def serialize_errors(errors)
def serialize_errors(errors, options = {})
return nil if errors.nil?

arr =
Expand All @@ -15,9 +15,10 @@ def serialize_errors(errors)
sum <<
{
source: source,
uid: options[:uid],
title:
title.is_a?(String) ? title.sub(/^./, &:upcase) : title.to_s,
}
}.compact
end

sum
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def validate
status: :ok
else
logger.info @doi.errors.messages
render json: serialize_errors(@doi.errors.messages), status: :ok
render json: serialize_errors(@doi.errors.messages, uid: @doi.uid), status: :ok
end
end

Expand Down Expand Up @@ -568,8 +568,8 @@ def create
status: :created,
location: @doi
else
logger.error @doi.errors.inspect
render json: serialize_errors(@doi.errors),
# logger.error @doi.errors.inspect
render json: serialize_errors(@doi.errors, uid: @doi.uid),
include: @include,
status: :unprocessable_entity
end
Expand Down Expand Up @@ -625,8 +625,8 @@ def update
render json: DataciteDoiSerializer.new(@doi, options).serialized_json,
status: exists ? :ok : :created
else
logger.error @doi.errors.messages
render json: serialize_errors(@doi.errors.messages),
# logger.error @doi.errors.messages
render json: serialize_errors(@doi.errors.messages, uid: @doi.uid),
include: @include,
status: :unprocessable_entity
end
Expand All @@ -647,8 +647,8 @@ def undo
render json: DataciteDoiSerializer.new(@doi, options).serialized_json,
status: :ok
else
logger.error @doi.errors.messages
render json: serialize_errors(@doi.errors.messages),
# logger.error @doi.errors.messages
render json: serialize_errors(@doi.errors.messages, uid: @doi.uid),
include: @include,
status: :unprocessable_entity
end
Expand All @@ -665,7 +665,7 @@ def destroy
head :no_content
else
logger.error @doi.errors.inspect
render json: serialize_errors(@doi.errors),
render json: serialize_errors(@doi.errors, uid: @doi.uid),
status: :unprocessable_entity
end
else
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def create
serialized_json,
status: :created
else
Rails.logger.error @provider_prefix.errors.inspect
# Rails.logger.error @provider_prefix.errors.inspect
render json: serialize_errors(@provider_prefix.errors),
status: :unprocessable_entity
end
Expand All @@ -157,8 +157,8 @@ def destroy
Rails.logger.warn message
head :no_content
else
Rails.logger.error @provider_prefix.errors.inspect
render json: serialize_errors(@provider_prefix.errors),
# Rails.logger.error @provider_prefix.errors.inspect
render json: serialize_errors(@provider_prefix.errors, uid: @provider_prefix.uid),
status: :unprocessable_entity
end
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ def create
render json: ProviderSerializer.new(@provider, options).serialized_json,
status: :ok
else
Rails.logger.error @provider.errors.inspect
render json: serialize_errors(@provider.errors),
# Rails.logger.error @provider.errors.inspect
render json: serialize_errors(@provider.errors, uid: @provider.uid),
status: :unprocessable_entity
end
end
Expand All @@ -268,8 +268,8 @@ def update
render json: ProviderSerializer.new(@provider, options).serialized_json,
status: :ok
else
Rails.logger.error @provider.errors.inspect
render json: serialize_errors(@provider.errors),
# Rails.logger.error @provider.errors.inspect
render json: serialize_errors(@provider.errors, uid: @provider.uid),
status: :unprocessable_entity
end
end
Expand All @@ -291,8 +291,8 @@ def destroy
end
head :no_content
else
Rails.logger.error @provider.errors.inspect
render json: serialize_errors(@provider.errors),
# Rails.logger.error @provider.errors.inspect
render json: serialize_errors(@provider.errors, uid: @provider.uid),
status: :unprocessable_entity
end
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def create
render json: RepositorySerializer.new(@client, options).serialized_json,
status: :created
else
Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors),
# Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors, uid: @client.uid),
status: :unprocessable_entity
end
end
Expand All @@ -231,8 +231,8 @@ def update
render json: RepositorySerializer.new(@client, options).serialized_json,
status: :ok
else
Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors),
# Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors, uid: @client.uid),
status: :unprocessable_entity
end
end
Expand All @@ -252,8 +252,8 @@ def destroy
@client.send_delete_email unless Rails.env.test?
head :no_content
else
Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors),
# Rails.logger.error @client.errors.inspect
render json: serialize_errors(@client.errors, uid: @client.uid),
status: :unprocessable_entity
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/repository_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def create
serialized_json,
status: :created
else
Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors),
# Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors, uid: @client_prefix.uid),
status: :unprocessable_entity
end
end
Expand All @@ -158,8 +158,8 @@ def destroy
Rails.logger.warn message
head :no_content
else
Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors),
# Rails.logger.error @client_prefix.errors.inspect
render json: serialize_errors(@client_prefix.errors, uid: @client_prefix.uid),
status: :unprocessable_entity
end
end
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/helpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def register_url
success = update(minted: Time.zone.now, updated: Time.zone.now)
end
unless Rails.env.test?

Rails.logger.debug "[Handle] URL for DOI " + doi + " updated to " +
url +
"."
Expand Down
16 changes: 9 additions & 7 deletions spec/requests/clients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"relationships": {
"provider": {
"data": { "type": "providers", "id": provider.symbol.downcase },
"data": { "type": "providers", "id": provider.uid },
},
},
},
Expand Down Expand Up @@ -147,7 +147,7 @@

relationships = json.dig("data", "relationships")
expect(relationships.dig("provider", "data", "id")).to eq(
provider.symbol.downcase,
provider.uid,
)

Client.import
Expand All @@ -173,7 +173,7 @@
},
"relationships": {
"provider": {
"data": { "type": "providers", "id": provider.symbol.downcase },
"data": { "type": "providers", "id": provider.uid },
},
},
},
Expand All @@ -191,8 +191,8 @@

expect(json["errors"]).to eq(
[
{ "source" => "system_email", "title" => "Can't be blank" },
{ "source" => "system_email", "title" => "Is invalid" },
{ "source" => "system_email", "title" => "Can't be blank", "uid" => provider.uid + ".imperial" },
{ "source" => "system_email", "title" => "Is invalid", "uid" => provider.uid + ".imperial" },
],
)
end
Expand Down Expand Up @@ -329,7 +329,7 @@
get "/prefixes/#{prefix.uid}"
expect(
json.dig("data", "relationships", "clients", "data").first.dig("id"),
).to eq(client.symbol.downcase)
).to eq(client.uid)

get "provider-prefixes?query=#{prefix.uid}"
expect(
Expand All @@ -356,6 +356,7 @@
expect(last_response.status).to eq(422)
expect(json["errors"].first).to eq(
"source" => "globus_uuid", "title" => "Abc is not a valid UUID",
"uid" => client.uid
)
end
end
Expand All @@ -371,7 +372,7 @@
end
let(:credentials) do
provider.encode_auth_param(
username: provider.symbol.downcase, password: "12345",
username: provider.uid, password: "12345",
)
end
let(:headers) do
Expand Down Expand Up @@ -412,6 +413,7 @@
expect(last_response.status).to eq(422)
expect(json["errors"].first).to eq(
"source" => "symbol", "title" => "Cannot be changed",
"uid" => client.uid + "m"
)
end
end
Expand Down
Loading

0 comments on commit 9ecbcba

Please sign in to comment.