Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Metrics/ClassLength:
- 'packages/forest_admin_agent/lib/forest_admin_agent/routes/action/actions.rb'
- 'packages/forest_admin_agent/lib/forest_admin_agent/services/smart_action_checker.rb'
- 'packages/forest_admin_agent/lib/forest_admin_agent/routes/resources/related/update_related.rb'
- 'packages/forest_admin_agent/lib/forest_admin_agent/routes/resources/update_field.rb'
- 'packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/frontend_validation_utils.rb'
- 'packages/forest_admin_agent/lib/forest_admin_agent/utils/query_string_parser.rb'
- 'packages/forest_admin_datasource_active_record/lib/forest_admin_datasource_active_record/utils/query.rb'
Expand Down
4 changes: 2 additions & 2 deletions packages/forest_admin_agent/lib/forest_admin_agent.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'forest_admin_agent/version'
require_relative 'forest_admin_agent/http/Exceptions/business_error'
require 'forest_admin_datasource_toolkit'
require 'zeitwerk'

loader = Zeitwerk::Loader.for_gem
Expand All @@ -8,6 +8,6 @@
loader.setup

module ForestAdminAgent
class Error < StandardError; end
class Error < ForestAdminDatasourceToolkit::Exceptions::BusinessError; end
# Your code goes here...
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def start(rendering_id)

def verify_code_and_generate_token(params)
unless params['state']
raise ForestAdminAgent::Http::Exceptions::BadRequestError,
raise ForestAdminDatasourceToolkit::Exceptions::BadRequestError,
ForestAdminAgent::Utils::ErrorMessages::INVALID_STATE_MISSING
end

Expand All @@ -40,7 +40,7 @@ def verify_code_and_generate_token(params)
def get_rendering_id_from_state(state)
state = JSON.parse(state.tr("'", '"').gsub('=>', ':'))
unless state.key? 'renderingId'
raise ForestAdminAgent::Http::Exceptions::BadRequestError.new(
raise ForestAdminDatasourceToolkit::Exceptions::BadRequestError.new(
ForestAdminAgent::Utils::ErrorMessages::INVALID_STATE_RENDERING_ID,
details: { state: state }
)
Expand All @@ -49,7 +49,7 @@ def get_rendering_id_from_state(state)
begin
Integer(state['renderingId'])
rescue ArgumentError
raise ForestAdminAgent::Http::Exceptions::ValidationError.new(
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError.new(
ForestAdminAgent::Utils::ErrorMessages::INVALID_RENDERING_ID,
details: { renderingId: state['renderingId'] }
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative 'business_error'

module ForestAdminAgent
module Http
module Exceptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require_relative 'Exceptions/business_error'
require_relative 'Exceptions/http_exception'

module ForestAdminAgent
Expand Down Expand Up @@ -33,35 +32,30 @@ def self.translate(error)
# @param error [Exception] The error to get status for
# @return [Integer] The HTTP status code
def self.get_error_status(error)
if defined?(ForestAdminDatasourceToolkit::Exceptions::ValidationError) &&
of_type?(error, ForestAdminDatasourceToolkit::Exceptions::ValidationError)
return 400
end

error.status if error.respond_to?(:status) && error.status

case error
when Exceptions::ValidationError, Exceptions::BadRequestError
when ForestAdminDatasourceToolkit::Exceptions::ValidationError, ForestAdminDatasourceToolkit::Exceptions::BadRequestError
400
when Exceptions::UnauthorizedError
when ForestAdminDatasourceToolkit::Exceptions::UnauthorizedError
401
when Exceptions::ForbiddenError
when ForestAdminDatasourceToolkit::Exceptions::ForbiddenError
403
when Exceptions::NotFoundError
when ForestAdminDatasourceToolkit::Exceptions::NotFoundError
404
when Exceptions::ConflictError
when ForestAdminDatasourceToolkit::Exceptions::ConflictError
409
when Exceptions::UnprocessableError
when ForestAdminDatasourceToolkit::Exceptions::UnprocessableError
422
when Exceptions::TooManyRequestsError
when ForestAdminDatasourceToolkit::Exceptions::TooManyRequestsError
429
when Exceptions::InternalServerError
when ForestAdminDatasourceToolkit::Exceptions::InternalServerError
500
when Exceptions::BadGatewayError
when ForestAdminDatasourceToolkit::Exceptions::BadGatewayError
502
when Exceptions::ServiceUnavailableError
when ForestAdminDatasourceToolkit::Exceptions::ServiceUnavailableError
503
when Exceptions::BusinessError
when ForestAdminDatasourceToolkit::Exceptions::BusinessError
# default BusinessError → 422 (Unprocessable Entity)
422
else
Expand All @@ -82,7 +76,7 @@ def self.get_error_message(error)
end

is_known_error = error.is_a?(Exceptions::HttpException) ||
error.is_a?(Exceptions::BusinessError) ||
error.is_a?(ForestAdminDatasourceToolkit::Exceptions::BusinessError) ||
(defined?(ForestAdminDatasourceToolkit::Exceptions::ForestException) &&
error.is_a?(ForestAdminDatasourceToolkit::Exceptions::ForestException))

Expand All @@ -95,7 +89,7 @@ def self.get_error_message(error)
# @param error [Exception] The error to get data from
# @return [Hash, nil] The error metadata or nil
def self.get_error_data(error)
return error.details if error.is_a?(Exceptions::BusinessError) &&
return error.details if error.is_a?(ForestAdminDatasourceToolkit::Exceptions::BusinessError) &&
error.respond_to?(:details) &&
!error.details.empty?

Expand All @@ -107,9 +101,9 @@ def self.get_error_data(error)
# @return [Proc, nil] A proc that generates custom headers
def self.get_custom_headers(error)
case error
when Exceptions::NotFoundError
when ForestAdminDatasourceToolkit::Exceptions::NotFoundError
{ 'x-error-type' => 'object-not-found' }
when Exceptions::TooManyRequestsError
when ForestAdminDatasourceToolkit::Exceptions::TooManyRequestsError
{ 'Retry-After' => error.retry_after.to_s }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module ForestAdminAgent
module Http
class ForestAdminApiRequester
include ForestAdminAgent::Http::Exceptions
include ForestAdminDatasourceToolkit::Exceptions

def initialize
Expand All @@ -30,7 +29,7 @@ def post(url, params = nil)

def handle_response_error(error)
# Re-raise if it's already a BusinessError
raise error if error.is_a?(ForestAdminAgent::Http::Exceptions::BusinessError)
raise error if error.is_a?(BusinessError)
raise error if error.is_a?(ForestException)

if error.response[:message]&.include?('certificate')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def handle_hook_request(args = {})
private

def middleware_custom_action_approval_request_data(args)
raise Http::Exceptions::UnprocessableError if args.dig(:params, :data, :attributes, :requester_id)
raise ForestAdminDatasourceToolkit::Exceptions::UnprocessableError if args.dig(:params, :data, :attributes,
:requester_id)

if (signed_request = args.dig(:params, :data, :attributes, :signed_approval_request))
args[:params][:data][:attributes][:signed_approval_request] = decode_signed_approval_request(signed_request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def parse_query_segment(collection, args, permissions, caller)
return unless args[:params][:segmentQuery]

unless args[:params][:connectionName]
raise ForestAdminAgent::Http::Exceptions::UnprocessableError, 'Missing native query connection attribute'
raise ForestAdminDatasourceToolkit::Exceptions::UnprocessableError,
'Missing native query connection attribute'
end

QueryValidator.valid?(args[:params][:segmentQuery])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Resources
class NativeQuery < AbstractAuthenticatedRoute
include ForestAdminAgent::Builder
include ForestAdminAgent::Utils
include ForestAdminAgent::Http::Exceptions
include ForestAdminDatasourceToolkit::Exceptions
include ForestAdminDatasourceToolkit::Components::Charts
include ForestAdminAgent::Routes::QueryHandler
Expand All @@ -31,7 +30,8 @@ def handle_request(args = {})

QueryValidator.valid?(query)
unless args[:params][:connectionName]
raise ForestAdminAgent::Http::Exceptions::UnprocessableError, 'Missing native query connection attribute'
raise ForestAdminDatasourceToolkit::Exceptions::UnprocessableError,
'Missing native query connection attribute'
end

@permissions.can_chart?(args[:params])
Expand Down Expand Up @@ -62,7 +62,7 @@ def type=(type)
end

def raise_error(result, key_names)
raise BadRequestError,
raise ForestAdminDatasourceToolkit::Exceptions::BadRequestError,
"The result columns must be named #{key_names} instead of '#{result.keys.join("', '")}'"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def handle_request(args = {})

records = @collection.list(@caller, filter, projection)

raise Http::Exceptions::NotFoundError, 'Record does not exists' unless records.size.positive?
unless records.size.positive?
raise ForestAdminDatasourceToolkit::Exceptions::NotFoundError,
'Record does not exists'
end

{
name: args[:params]['collection_name'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,26 @@ def handle_request(args = {})

def parse_index(index_param)
index = Integer(index_param)
raise Http::Exceptions::ValidationError, 'Index must be non-negative' if index.negative?
if index.negative?
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError,
'Index must be non-negative'
end

index
rescue ArgumentError
raise Http::Exceptions::ValidationError, "Invalid index: #{index_param}"
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError, "Invalid index: #{index_param}"
end

def validate_array_field!(field_schema, field_name)
FieldValidator.validate(@collection, field_name)
return if field_schema.column_type.is_a?(Array)

raise Http::Exceptions::ValidationError,
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError,
"Field '#{field_name}' is not an array (type: #{field_schema.column_type})"
rescue ForestAdminDatasourceToolkit::Exceptions::ValidationError => e
raise Http::Exceptions::NotFoundError, e.message if e.message.include?('not found')
raise ForestAdminDatasourceToolkit::Exceptions::NotFoundError, e.message if e.message.include?('not found')

raise Http::Exceptions::ValidationError, e.message
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError, e.message
end

def fetch_record(primary_key_values)
Expand All @@ -95,20 +98,20 @@ def fetch_record(primary_key_values)
)
records = @collection.list(@caller, filter, ProjectionFactory.all(@collection))

raise Http::Exceptions::NotFoundError, 'Record not found' unless records&.any?
raise ForestAdminDatasourceToolkit::Exceptions::NotFoundError, 'Record not found' unless records&.any?

records[0]
end

def validate_array_value!(array, field_name, array_index)
unless array.is_a?(Array)
raise Http::Exceptions::UnprocessableError,
raise ForestAdminDatasourceToolkit::Exceptions::UnprocessableError,
"Field '#{field_name}' value is not an array (got: #{array.class})"
end

return unless array_index >= array.length

raise Http::Exceptions::ValidationError,
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError,
"Index #{array_index} out of bounds for array of length #{array.length}"
end

Expand All @@ -128,7 +131,8 @@ def coerce_value(value, column_type)
begin
return Float(value)
rescue ArgumentError
raise Http::Exceptions::ValidationError, "Cannot coerce '#{value}' to Number - wrong type"
raise ForestAdminDatasourceToolkit::Exceptions::ValidationError,
"Cannot coerce '#{value}' to Number - wrong type"
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Routes
module Security
class Authentication < AbstractRoute
include ForestAdminAgent::Builder
include ForestAdminAgent::Http::Exceptions
include ForestAdminDatasourceToolkit::Exceptions

def setup_routes
add_route(
Expand Down
Loading
Loading