Harden handling of Nextcloud errors

A status of 500+ in ocs.meta indicates that something went so wrong,
that we shouldn't rely on the ocs.data being filled with what we usually
expect to be there.
This commit is contained in:
Jan Sandbrink
2025-11-20 10:56:30 +01:00
parent 78f16799c3
commit 65e618a353
4 changed files with 14 additions and 10 deletions
@@ -78,6 +78,17 @@ module Storages
Failure(error.with(payload: "No origin user ID or user token found. Cannot execute query without user context."))
end
end
# Validates the OCS Meta Statuscode for fatal errors (i.e. unexpected server-side errors). Client-side errors,
# such as a 404 File Not Found do not cause an error.
# @return [Dry::Result]
def fail_on_ocs_error(json, error)
if json.dig(:ocs, :meta, :statuscode) < 500
Success(json)
else
Failure(error.with(code: :error))
end
end
end
end
end
@@ -55,7 +55,7 @@ module Storages
case response
in { status: 200..299 }
Success(response.json(symbolize_keys: true))
fail_on_ocs_error(response.json(symbolize_keys: true), error)
in { status: 404 }
Failure(error.with(code: :not_found))
in { status: 401 }
@@ -57,7 +57,7 @@ module Storages
case response
in { status: 200..299 }
verify_successful_response(response.json(symbolize_keys: true), error)
fail_on_ocs_error(response.json(symbolize_keys: true), error)
in { status: 404 }
Failure(error.with(code: :not_found))
in { status: 401 }
@@ -67,14 +67,6 @@ module Storages
end
end
def verify_successful_response(json, error)
if json.dig(:ocs, :meta, :status) == "ok"
Success(json)
else
Failure(error.with(code: :error))
end
end
def create_storage_file_infos(parsed_json)
parsed_json.dig(:ocs, :data)&.map do |(key, value)|
if value[:statuscode] == 200
+1
View File
@@ -141,6 +141,7 @@ module StorageServerHelpers
folder1_xml_response = build(:webdav_data_folder)
folder1_fileinfo_response = {
ocs: {
meta: { statuscode: 100 },
data: {
status: "OK",
statuscode: 200,