Skip to content

Commit

Permalink
chore: improve panic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jsenko committed Jul 22, 2024
1 parent 9344c1e commit 788a6e4
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 30 deletions.
4 changes: 2 additions & 2 deletions controllers/cf/cf_pod_template_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ func SanitizeBasePodSpec(log *zap.SugaredLogger, base *ar.ApicurioRegistryPodTem
}
currentContainer := common.GetContainerByName(current.Spec.Containers, f.REGISTRY_CONTAINER_NAME)
if currentContainer == nil {
panic("could not find current registry container")
panic("Could not find the registry container in the current Deployment.")
}
factoryContainer := common.GetContainerByName(factory.Spec.Containers, f.REGISTRY_CONTAINER_NAME)
if factoryContainer == nil {
panic("could not find factory registry container")
panic("Could not find the registry container in the initial Deployment.")
}

if len(base.Spec.Containers) > 0 && baseContainer != nil {
Expand Down
9 changes: 6 additions & 3 deletions controllers/client/client_crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func NewCRDClient(log *zap.Logger, scheme *runtime.Scheme, config *rest.Config)

c, err := rest.UnversionedRESTClientFor(config2)
if err != nil {
panic("Could not create CRD client.")
log.Sugar().Error(err)
panic("Could not create Kubernetes client for ApicurioRegistry CRD.")
}
return &CRDClient{
client: c,
Expand Down Expand Up @@ -97,14 +98,16 @@ func (this *CRDClient) PatchApicurioRegistryStatus(namespace common.Namespace, n
// Add "status" prefix to the patch path
var original map[string]interface{}
if err := json.Unmarshal(patchData, &original); err != nil {
panic(err) // TODO
this.log.Sugar().Error(err)
panic("Could not patch ApicurioRegistry status.")
}
extended := map[string]interface{}{
"status": original,
}
extendedPatchData, err := json.Marshal(extended)
if err != nil {
panic(err) // TODO
this.log.Sugar().Error(err)
panic("Could not patch ApicurioRegistry status.")
}

result := &ar.ApicurioRegistry{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/loop/context/context_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (this *loopContext) Finalize() (bool, time.Duration) {
this.requeueDelay = 0
}()
if this.reconcileSequence == math.MaxInt64 {
panic("int64 counter overflow. Restarting to reset.") // This will probably never happen
panic("int64 counter overflow. Restarting to reset.") // This will never happen
}
this.reconcileSequence += 1
return this.requeue, this.requeueDelay
Expand Down
14 changes: 7 additions & 7 deletions controllers/loop/context/context_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ func (this *LoopContextMock) GetAppNamespace() c.Namespace {
}

func (this *LoopContextMock) SetRequeueNow() {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopContextMock) SetRequeueDelaySoon() {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopContextMock) SetRequeueDelaySec(delay uint) {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopContextMock) Finalize() (bool, time.Duration) {
if this.reconcileSequence == math.MaxInt64 {
panic("int64 counter overflow. Restarting to reset.") // This will probably never happen
panic("int64 counter overflow. Restarting to reset.") // This will never happen
}
this.reconcileSequence += 1
return false, 0
Expand All @@ -71,7 +71,7 @@ func (this *LoopContextMock) GetResourceCache() resources.ResourceCache {
}

func (this *LoopContextMock) GetClients() *client.Clients {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopContextMock) GetEnvCache() env.EnvCache {
Expand All @@ -87,11 +87,11 @@ func (this *LoopContextMock) GetAttempts() int {
}

func (this *LoopContextMock) GetTestingSupport() *c.TestSupport {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopContextMock) GetSupportedFeatures() *c.SupportedFeatures {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopContextMock) GetReconcileSequence() int64 {
Expand Down
5 changes: 4 additions & 1 deletion controllers/loop/impl/control_loop_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (this *controlLoopImpl) Run() {
}
}
if attempt == maxAttempts {
panic("control loop stabilization limit exceeded")
panic("Control loop stabilization limit exceeded. Explanation: The operator is separated into multiple functions, " +
"that are each responsible for a single task, such as applying env. variables to a Deployment. " +
"They are each executed until the desired state of the cached resource (e.g. the Deployment) is reached, and can be updated on the cluster. " +
"This error occurs when the functions could not reach the desired state within a limited number of attempts.")
}

this.services.AfterRun()
Expand Down
10 changes: 5 additions & 5 deletions controllers/loop/services/services_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ func (this *LoopServicesMock) AfterRun() {
}

func (this *LoopServicesMock) GetPatchers() *patcher.Patchers {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopServicesMock) GetKubeFactory() *factory.KubeFactory {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopServicesMock) GetMonitoringFactory() *factory.MonitoringFactory {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopServicesMock) GetConditionManager() conditions.ConditionManager {
panic("Not implemented")
panic("not implemented")
}

func (this *LoopServicesMock) GetStatus() *status.Status {
panic("Not implemented")
panic("not implemented")
}
7 changes: 5 additions & 2 deletions controllers/svc/env/env_cache_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (this *envCacheEntryBuilder) SetPriority(priority Priority) EnvCacheEntryBu

func (this *envCacheEntryBuilder) Build() EnvCacheEntry {
if strings.TrimSpace(this.entry.GetName()) == "" {
panic("Environment variable can not be empty nor whitespace")
panic("Environment variable name cannot be empty nor contain only whitespace.")
}
return this.entry
}
Expand Down Expand Up @@ -202,7 +202,10 @@ func (this *envCache) computeSorted() {

func (this *envCache) processWithDependencies(depth int, processed map[string]bool, val EnvCacheEntry) {
if depth > len(this.cache) {
panic("Cycle detected during the processing of environment variables.")
panic("Cycle detected during the processing of environment variables (at " + val.GetName() + "), make sure that every env. variable is define once. " +
"Explanation: Ordering of the env. variables is significant, because some variables can reference others using interpolation. " +
"As a result, the operator tries to keep the order consistent (e.g. as defined in the CR). " +
"This error occurs when the operator could not order the variables correctly.")
}
if _, exists := processed[val.GetName()]; !exists && !this.WasDeleted(val.GetName()) { // Was not yet processed
for _, dependencyName := range val.GetDependencies() { // Process & add deps first
Expand Down
6 changes: 3 additions & 3 deletions controllers/svc/factory/factory_kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func (this *KubeFactory) GetLabels() map[string]string {

registryVersion := os.Getenv(ENV_REGISTRY_VERSION)
if registryVersion == "" {
panic("Could not determine registry version. Environment variable '" + ENV_REGISTRY_VERSION + "' is empty.")
panic("Could not determine Registry version. Environment variable '" + ENV_REGISTRY_VERSION + "' is empty.")
}
operatorName := os.Getenv(ENV_OPERATOR_NAME)
if operatorName == "" {
panic("Could not determine operator name. Environment variable '" + ENV_OPERATOR_NAME + "' is empty.")
panic("Could not determine Operator name. Environment variable '" + ENV_OPERATOR_NAME + "' is empty.")
}
app := this.ctx.GetAppName().Str()

Expand Down Expand Up @@ -180,7 +180,7 @@ func (this *KubeFactory) CreateService() *core.Service {

func (this *KubeFactory) CreateIngress(serviceName string) *networking.Ingress {
if serviceName == "" {
panic("Required argument.")
panic("Required argument, Ingress name, is empty.")
}
metaData := this.createObjectMeta("ingress")
metaData.Annotations = map[string]string{
Expand Down
4 changes: 2 additions & 2 deletions controllers/svc/patcher/patcher_kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (this *KubePatcher) patchApicurioRegistry() { // TODO move to separate file
"ar.ApicurioRegistry",
func(owner meta.Object, namespace c.Namespace, value interface{}) (interface{}, error) {
// This should be not used (at the moment)
panic("Unsupported operation.")
panic("unsupported operation")
},
func(namespace c.Namespace, name c.Name, data []byte) (interface{}, error) {
return this.ctx.GetClients().CRD().PatchApicurioRegistry(namespace, name, data)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (this *KubePatcher) patchApicurioRegistryStatus() {
"ar.ApicurioRegistryStatus",
func(owner meta.Object, namespace c.Namespace, value interface{}) (interface{}, error) {
// This should be not used (at the moment)
panic("Unsupported operation.")
panic("unsupported operation")
},
func(namespace c.Namespace, name c.Name, data []byte) (interface{}, error) {
return this.ctx.GetClients().CRD().PatchApicurioRegistryStatus(namespace, name, data)
Expand Down
3 changes: 2 additions & 1 deletion controllers/svc/patcher/patchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func patchGeneric(
// Optimization: Check if the patch is empty
var patchJson map[string]interface{}
if err := json.Unmarshal(patchData, &patchJson); err != nil {
panic(err) // TODO
ctx.GetLog().Sugar().Error(err)
panic("Could not patch a resource: " + typeString)
}
if patchJson == nil || len(patchJson) == 0 {
//ctx.GetLog().WithValues("resource", typeString, "name", name, "patch", string(patchData)).
Expand Down
2 changes: 1 addition & 1 deletion controllers/svc/status/conditions/conditions_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (this *condition) GetData() *metav1.Condition {

func (this *condition) Reset() {
if this.ctype == "" {
panic("Condition type not set!")
panic("Condition type is empty.")
}
if this.data == nil {
this.data = &metav1.Condition{
Expand Down
4 changes: 2 additions & 2 deletions controllers/svc/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (this *Status) init() {
func (this *Status) set(mapp map[string]string, key string, value string) {
ptr := &value
if key == "" {
panic("Fatal: Empty key for " + *ptr)
panic("Status key is empty for value: " + *ptr)
}
mapp[key] = *ptr
}
Expand All @@ -81,7 +81,7 @@ func (this *Status) SetConfigInt32P(key string, value *int32) {
func (this *Status) GetConfig(key string) string {
v, ok := this.config[key]
if !ok {
panic("Fatal: Status key '" + key + "' not found.")
panic("Value that belongs to status key " + key + " not found.")
}
return v
}
Expand Down

0 comments on commit 788a6e4

Please sign in to comment.