Update cached response create or update service
🔭 Context
In Maven virtual registry, we have an API endpoint that will receive an file upload and we need to create or update a cached response.
To cope with that requirement, we implemented a service.
All good so far.
While implementing feature specs, I stumbled upon a situation. The service can receive a nil content_type (which on of the required attributes of the cached responses).
This blowed an ActiveRecordError and it bubbled up into the API class without being handled (and thus ended with a 500 server response code).
This is not great: errors should be correctly handled.
🤔 What does this MR do and why?
- Update the cached response create or update service to properly handle errors and return a
ServiceResponse.errorproperly. - Update attributes handling so that we can leverage the database default values. This is the case for
nilcontent_type. - Update the related service spec, model spec and request spec.
📚 References
General epic for maven virtual registries: &14137 (closed)
🗒️ MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
🌈 Screenshots or screen recordings
No UI changes
⚙️ How to set up and validate locally
In a rails console:
r = ::VirtualRegistries::Packages::Maven::Registry.create!(group: <root_group>)
u = ::VirtualRegistries::Packages::Maven::Upstream.create!(group: <root_group>, url: 'https://repo1.maven.org/maven2')
VirtualRegistries::Packages::Maven::RegistryUpstream.create!(group: <root_group>, registry: r, upstream: u)
file = Tempfile.new('foo')
file.write('test')
::VirtualRegistries::Packages::Maven::CachedResponses::CreateOrUpdateService.new(upstream: u, current_user: User.first, params: { etag: 'etag', content_type: 'text/plain', path: '/test/foo/bar', file: UploadedFile.new(file.path, sha1: 'sha1', md5: 'md5') }).execute # => all good
::VirtualRegistries::Packages::Maven::CachedResponses::CreateOrUpdateService.new(upstream: u, current_user: User.first, params: { etag: 'etag', path: '/test/foo/bar', file: UploadedFile.new(file.path, sha1: 'sha1', md5: 'md5') }).execute # no content_type but still ok
Related to #480360 (closed)