Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Added error handling for storage files API endpoint

Andreas Pfohld5d87a5589c9
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index 944ff5b2b61..c7a09b209d4 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -28,14 +28,30 @@
module API::V3::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
+ helpers do
+ def handle_files_error(files)
+ case files.result
+ when :not_found
+ raise API::Errors::NotFound.new
+ when :not_authorized
+ raise API::Errors::Unauthorized.new
+ else
+ raise API::Errors::InternalError.new
+ end
+ end
+ end
+
resources :files do
get do
- files_query = ::API::V3::Utilities::StorageRequests
- .new(storage: @storage)
- .files_query(user: current_user)
+ files = ::API::V3::Utilities::StorageRequests
+ .new(storage: @storage)
+ .files_query(user: current_user)
+ .call
+ .on_failure { |r| handle_files_error(r) }
+ .result
::API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
- files_query.call,
+ files,
self_link: api_v3_paths.storage_files(@storage.id),
current_user:
)
diff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
index 5f18441fa1f..fecf679a745 100644
--- a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
+++ b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
@@ -21,14 +21,10 @@ module API::V3::Utilities::StorageInteraction
}
)
- if %w[401 403].include?(response.code)
- ServiceResult.failure(result: :not_authorized)
- else
- ServiceResult.success(result: response.body)
- end
+ response.is_a?(Net::HTTPSuccess) ? ServiceResult.success(result: response.body) : error(response)
end
- parse_response(result)
+ storage_files(result)
end
private
@@ -50,13 +46,24 @@ module API::V3::Utilities::StorageInteraction
end.to_xml
end
- def parse_response(response)
- return response unless response.success
+ def error(response)
+ case response
+ when Net::HTTPNotFound
+ ServiceResult.failure(result: :not_found)
+ when Net::HTTPUnauthorized
+ ServiceResult.failure(result: :not_authorized)
+ else
+ ServiceResult.failure(result: :error)
+ end
+ end
- Nokogiri::XML(response.result)
- .xpath('//d:response')
- .drop(1) # drop current directory
- .map { |file_element| storage_file(file_element) }
+ def storage_files(response)
+ response.map do |xml|
+ Nokogiri::XML(xml)
+ .xpath('//d:response')
+ .drop(1) # drop current directory
+ .map { |file_element| storage_file(file_element) }
+ end
end
def storage_file(file_element)