Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added the valid members to reporting query errors

Damian Legawiec2a10da8a8c6f
diff --git a/spree/api/app/controllers/spree/api/v3/admin/reporting_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/reporting_controller.rb
index ab69940e1c..d25c1579b5 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/reporting_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/reporting_controller.rb
@@ -82,8 +82,17 @@ module Spree
end
end
+ # An unknown member carries what it was and what was valid instead.
+ # Passing those through as `details` lets a client correct the query
+ # without parsing the sentence — the same list the schema endpoint
+ # would have given it.
def render_invalid_query(error)
- render_error(code: 'invalid_reporting_query', message: error.message, status: :unprocessable_content)
+ details = if error.is_a?(Spree::Reporting::UnknownMember)
+ { kind: error.kind, name: error.name.to_s, valid: error.valid.map(&:to_s) }
+ end
+
+ render_error(code: 'invalid_reporting_query', message: error.message,
+ status: :unprocessable_content, details: details)
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/reporting_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/reporting_controller_spec.rb
index d3b240b272..82a506c2ad 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/reporting_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/reporting_controller_spec.rb
@@ -113,6 +113,16 @@ RSpec.describe Spree::Api::V3::Admin::ReportingController, type: :controller do
expect(json_response['error']['code']).to eq('invalid_reporting_query')
expect(json_response['error']['message']).to include('net_revenue')
end
+ it 'names the valid members in the error details so a client can correct itself' do
+ post :query, params: { metrics: %w[revenue] }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ details = json_response['error']['details']
+ expect(details['kind']).to eq('metric')
+ expect(details['name']).to eq('revenue')
+ expect(details['valid']).to include('gross_revenue', 'net_revenue')
+ end
+
end
context 'without authentication' do
diff --git a/spree/core/app/models/spree/exports/report.rb b/spree/core/app/models/spree/exports/report.rb
index 7ab185c87b..b481381b00 100644
--- a/spree/core/app/models/spree/exports/report.rb
+++ b/spree/core/app/models/spree/exports/report.rb
@@ -64,11 +64,19 @@ module Spree
@reporting_query ||= Spree::Reporting::Query.new(store: store, params: query_params)
end
+ # The base class parses the column; this only has to insist the payload
+ # is an object, since a bare array or `null` is valid JSON that would
+ # otherwise crash rather than fail validation.
def query_params
- raw = search_params.is_a?(String) ? JSON.parse(search_params) : (search_params || {})
- raise Spree::Reporting::InvalidQuery, 'search_params must be an object' unless raw.is_a?(Hash)
+ raise Spree::Reporting::InvalidQuery, 'search_params must be an object' unless parsed_payload_is_object?
- raw['query'] || raw[:query] || {}
+ parsed_search_params[:query] || {}
+ end
+
+ def parsed_payload_is_object?
+ search_params.blank? || !search_params.is_a?(String) || JSON.parse(search_params).is_a?(Hash)
+ rescue JSON::ParserError
+ false
end
def authorize_members!(query)