-
Notifications
You must be signed in to change notification settings - Fork 1
fix: replace all generic errors to specific HTTP errors #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4 new issues
This is from Qlty Cloud, the successor to Code Climate Quality. Learn more. |
| class HttpError < StandardError | ||
| attr_reader :status, :user_message, :custom_headers, :meta, :cause, :name | ||
|
|
||
| def initialize(error, status, user_message = nil, _meta = nil, custom_headers_proc = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
c39ace3 to
ee032a0
Compare
packages/forest_admin_agent/lib/forest_admin_agent/http/error_handling.rb
Show resolved
Hide resolved
packages/forest_admin_agent/lib/forest_admin_agent/http/error_translator.rb
Show resolved
Hide resolved
|
Diff Coverage: The code coverage on the diff in this pull request is 83.0%. Total Coverage: This PR will decrease coverage by 0.18%. File Coverage Changes
🛟 Help
This is from Qlty Cloud, the successor to Code Climate Quality. Learn more. |
| def get_error_message(error) | ||
| return error.user_message if error.is_a?(ForestAdminAgent::Http::Exceptions::HttpError) | ||
|
|
||
| if error.class.respond_to?(:ancestors) && error.class.ancestors.include?(ForestAdminAgent::Http::Exceptions::HttpException) | ||
| return error.message | ||
| end | ||
|
|
||
| if (customizer = ForestAdminAgent::Facades::Container.cache(:customize_error_message)) | ||
| return error.message if error.is_a?(ForestAdminAgent::Http::Exceptions::BusinessError) | ||
|
|
||
| if defined?(ForestAdminAgent::Facades) && | ||
| (customizer = ForestAdminAgent::Facades::Container.cache(:customize_error_message)) | ||
| message = eval(customizer).call(error) | ||
| return message if message | ||
| end | ||
|
|
||
| return error.message if error.is_a?(ForestAdminDatasourceToolkit::Exceptions::ValidationError) | ||
|
|
||
| 'Unexpected error' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def self.translate(error) # rubocop:disable Metrics/MethodLength | ||
| # Already an HttpError or HttpException - no translation needed | ||
| return error if error.is_a?(Exceptions::HttpError) | ||
| return error if error.respond_to?(:status) && error.status | ||
|
|
||
| # Translate DatasourceToolkit ValidationError to Agent BadRequestError | ||
| if defined?(ForestAdminDatasourceToolkit::Exceptions::ValidationError) && | ||
| error.is_a?(ForestAdminDatasourceToolkit::Exceptions::ValidationError) | ||
| error = Exceptions::BadRequestError.new(error.message) | ||
| end | ||
|
|
||
| # Translate BusinessError to HttpError with appropriate status code | ||
| case error | ||
| when Exceptions::BadRequestError, Exceptions::ValidationError | ||
| ERROR_400.new(error) | ||
| when Exceptions::ForbiddenError | ||
| ERROR_403.new(error) | ||
| when Exceptions::NotFoundError | ||
| ERROR_404.new(error) | ||
| when Exceptions::ConflictError | ||
| ERROR_409.new(error) | ||
| when Exceptions::UnprocessableError | ||
| ERROR_422.new(error) | ||
| when Exceptions::TooManyRequestsError | ||
| ERROR_429.new(error) | ||
| when Exceptions::InternalServerError | ||
| ERROR_500.new(error) | ||
| when Exceptions::BadGatewayError | ||
| ERROR_502.new(error) | ||
| when Exceptions::ServiceUnavailableError | ||
| ERROR_503.new(error) | ||
| else | ||
| # Unknown error type - return as-is, will be handled as 500 | ||
| error | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
closed in favor of #197 |
Definition of Done
General
Security