Skip to content

Commit

Permalink
feat: use application/problem+json for invalid URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
vwong committed Dec 4, 2023
1 parent 563ecdb commit 7819a58
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def configure_middleware
@app_builder.use PactBroker::Api::Middleware::HttpDebugLogs if configuration.http_debug_logging_enabled
configure_basic_auth
configure_rack_protection
@app_builder.use Rack::PactBroker::ApplicationContext, application_context
@app_builder.use Rack::PactBroker::InvalidUriProtection
@app_builder.use Rack::PactBroker::ResetThreadData
@app_builder.use Rack::PactBroker::AddPactBrokerVersionHeader
Expand All @@ -189,7 +190,6 @@ def configure_middleware
@app_builder.use Rack::PactBroker::ConvertFileExtensionToAcceptHeader
# Rack::PactBroker::SetBaseUrl needs to be before the Rack::PactBroker::HalBrowserRedirect
@app_builder.use Rack::PactBroker::SetBaseUrl, configuration.base_urls
@app_builder.use Rack::PactBroker::ApplicationContext, application_context

if configuration.use_hal_browser
logger.info "Mounting HAL browser"
Expand Down
16 changes: 14 additions & 2 deletions lib/rack/pact_broker/invalid_uri_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def initialize app
def call env
if (uri = valid_uri?(env))
if (error_message = validate(uri))
[422, {"Content-Type" => "text/plain"}, [error_message]]
[422, headers, [body(env, error_message, "Unprocessable", "validation-errors", 422)]]
else
app.call(env)
end
else
[404, {}, []]
[404, headers, [body(env, "Invalid Path", "Not Found", "not-found", 404)]]
end
end

Expand Down Expand Up @@ -56,6 +56,18 @@ def validate(uri)
message("errors.tab_in_url_path")
end
end

def headers
{"Content-Type" => "application/problem+json"}
end

def body(env, detail, title, type, status)
env["pactbroker.application_context"]
.decorator_configuration
.class_for(:custom_error_problem_json_decorator)
.new(detail: detail, title: title, type: type, status: status)
.to_json(user_options: { base_url: env["pactbroker.base_url"] })
end
end
end
end

0 comments on commit 7819a58

Please sign in to comment.