chore: Consolidate error enums and use expect where possible - #803
chore: Consolidate error enums and use expect where possible#803adwk67 wants to merge 10 commits into
Conversation
| let _ = *RUN_REGION_MOVER_ENV; | ||
| let _ = *STACKABLE_LOG_DIR_ENV; | ||
| let _ = *CONTAINERDEBUG_LOG_DIRECTORY_ENV; | ||
| let _ = *LISTENER_PVC_NAME; |
There was a problem hiding this comment.
LISTENER_PVC_NAME should be covered by a test_constants() in listener.rs, where it is defined, rather than from the one in statefulset.rs. That also removes the need to make it pub, since its only other use is build_listener_pvc in the same module.
| .expect( | ||
| "The annotation keys are static and annotation values cannot be invalid.", | ||
| ), |
There was a problem hiding this comment.
This applies to every builder whose build() can fail and where the error handling has been replaced with expect("The annotation keys are static and annotation values cannot be invalid.").
Why a builder fails is generally an implementation detail of the builder. It typically fails either because an input was invalid or because a required method such as metadata was not called. Reading the current implementation and encoding "here is why it cannot fail today" into an expect couples us to that implementation, and that is what makes it risky.
I am raising it on this particular builder because it is less trivial than most. Something like
ConfigMapBuilder::new()
.metadata(some_metadata)
.add_data(...)
.build()
.expect("metadata is set")has the same problem in principle, but with a bit of good will it is acceptable.
This builder has many method calls, and it is not obvious which input ends up in the annotations the message talks about. It currently accepts any string as the service scope, and this function forwards whatever it is given. If the builder were extended to check that the string is a valid Service name, the operator would crash with the message "the annotations are valid". Granted, the example is contrived, since the service name used here is defined statically, but the operator can change in ways we cannot foresee.
I would do one of the following:
- keep the error handling on the less trivial builders, or
- change the message to something like "all inputs are valid and complete, so the builder does not fail", and commit to
- making the builders infallible.
Making builders infallible is hard in general, but for the ones in stackable-operator it should be tractable: required fields could be set in the constructor, methods could accept only
validated types, and coupled methods such as with_format(TlsPkcs12) and with_tls_pkcs12_password could be merged into one.
There was a problem hiding this comment.
Expects have been reverted other than when the inputs are static: this keeps things more readable and any builder input that depends on code elsewhere is safer with a Result.
Description
Part of stackabletech/issues#883
Definition of Done Checklist
Author
Reviewer
Acceptance
type/deprecationlabel & add to the deprecation scheduletype/experimentallabel & add to the experimental features tracker