-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #696 from coyote-team/661-creating-two-resources-w…
…ith-identical-uris-triggers-http-500 661 creating two resources with identical uris triggers http 500
- Loading branch information
Showing
5 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
# Used to rescue form errors that would otherwise raise an exception and display the standard rails 500 error page | ||
# Needed to rescue from resources controller create action for identical/invalid source URIs and Canonical IDs | ||
# Can add additional rescue_from statements as needed for other controllers/forms | ||
module FormErrors | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
rescue_from ActiveRecord::RecordNotUnique, with: :not_unique_response | ||
rescue_from URI::InvalidURIError, with: :invalid_uri_response | ||
end | ||
|
||
private | ||
|
||
def not_unique_response(e) | ||
response = check_unique_error(e) | ||
resource.errors.add(:base, response) | ||
logger.warn "Unable to create resource due to '#{e.message}'" | ||
render :new, status: :unprocessable_entity | ||
end | ||
|
||
def check_unique_error(e) | ||
if e.message.include?("canonical_id") | ||
"The Canonical ID is already in use for this organization." | ||
elsif e.message.include?("source_uri") | ||
"The Source URI is already in use for this organization." | ||
end | ||
end | ||
|
||
def invalid_uri_response(e) | ||
response = "The Source URI is invalid." | ||
resource.errors.add(:base, response) | ||
logger.warn "Unable to create resource due to '#{e.message}'" | ||
render :new, status: :unprocessable_entity | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters