Exception: Treaty::Exceptions::SpecifiedVersionNotFound
- Defined in:
- lib/treaty/exceptions/specified_version_not_found.rb
Overview
Raised when no API version is specified and no default version is configured
Purpose
Prevents treaty execution when the client doesn't specify a version and the treaty hasn't defined a default version to fall back to. Enforces explicit version selection for API contracts.
Usage
Raised automatically during version resolution in two scenarios:
Scenario 1: No Version Specified, No Default Configured
class PostsTreaty < ApplicationTreaty
version 1 do
# No default: true specified
request { string :title }
response(200) { object :post }
end
version 2 do
request { string :title }
response(200) { object :post }
end
end
# Client request without version header
PostsTreaty.call!(version: nil, params: { title: "Test" })
# => Raises Treaty::Exceptions::SpecifiedVersionNotFound
# => "Specified version is required for validation"
Scenario 2: Empty Version String
PostsTreaty.call!(version: "", params: { title: "Test" })
# => Raises Treaty::Exceptions::SpecifiedVersionNotFound
Prevention: Define Default Version
class PostsTreaty < ApplicationTreaty
version 1 do
request { string :title }
response(200) { object :post }
end
version 2, default: true do # Marks version 2 as default
request { string :title }
response(200) { object :post }
end
end
# Now works without explicit version
PostsTreaty.call!(version: nil, params: { title: "Test" })
# => Uses version 2 by default
Integration
Can be rescued by application controllers to return appropriate HTTP status:
rescue_from Treaty::Exceptions::SpecifiedVersionNotFound, with: :render_version_required
def render_version_required(exception)
render json: {
error: exception.,
hint: "Please specify an API version in the request header"
}, status: :bad_request # HTTP 400
end
HTTP Status
Typically returns HTTP 400 Bad Request, indicating that the client failed to provide required version information.
Best Practices
For API Providers
-
Always define a default version for backward compatibility:
version 1, default: true do # ... end -
Document version requirements in API documentation
-
Provide helpful error messages in rescue handlers
For API Clients
- Always specify version explicitly in production code
- Don't rely on default versions for critical applications
- Handle this exception with version selection logic
Difference from VersionNotFound
- SpecifiedVersionNotFound: No version specified (nil/blank)
- VersionNotFound: Specific version specified but doesn't exist
Version Selection Flow
- Client provides version → Use specified version
- Client provides no version → Look for default version
- No default version configured → Raise SpecifiedVersionNotFound