Skip to content

Commit

Permalink
Still looking for a place to push the generation of the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ahx committed Oct 27, 2023
1 parent 26e0baa commit 1d7e0a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
23 changes: 5 additions & 18 deletions lib/openapi_first/request_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative 'error_response'
require_relative 'request_body_validator'
require_relative 'string_keyed_hash'
require_relative 'request_validation_error_message'
require 'openapi_parameters'

module OpenapiFirst
Expand All @@ -20,7 +21,9 @@ def self.fail!(status = 400, location = nil, title: nil, validation_result: nil)
throw FAIL, {
status:,
location:,
title: title || validation_result&.message || Rack::Utils::HTTP_STATUS_CODES[status],
title: RequestValidationErrorMessage.build(
title || validation_result&.message || Rack::Utils::HTTP_STATUS_CODES[status], location
),
validation_result:
}
end
Expand All @@ -38,8 +41,7 @@ def call(env)

error = validate_request(operation, env)
if error
location, title = error.values_at(:location, :title)
raise RequestInvalidError, error_message(title, location) if @raise
raise RequestInvalidError, error[:title] if @raise

return error_response(error).render
end
Expand All @@ -48,21 +50,6 @@ def call(env)

private

def error_message(title, location)
return title unless location

"#{TOPICS.fetch(location)} #{title}"
end

TOPICS = {
body: 'Request body invalid:',
query: 'Query parameter invalid:',
header: 'Header parameter invalid:',
path: 'Path segment invalid:',
cookie: 'Cookie value invalid:'
}.freeze
private_constant :TOPICS

def error_response(error_object)
@error_response_class.new(**error_object)
end
Expand Down
19 changes: 19 additions & 0 deletions lib/openapi_first/request_validation_error_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module OpenapiFirst
module RequestValidationErrorMessage
TOPICS = {
body: 'Request body invalid:',
query: 'Query parameter invalid:',
header: 'Header parameter invalid:',
path: 'Path segment invalid:',
cookie: 'Cookie value invalid:'
}.freeze

def self.build(title, location)
return title unless location

"#{TOPICS.fetch(location)} #{title}"
end
end
end

0 comments on commit 1d7e0a9

Please sign in to comment.