From d8172e57a3b1a699b11c3061cafec8ac219b4591 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Mon, 6 May 2024 10:19:18 +0530 Subject: [PATCH 1/8] fix: return Panic error when error not found w/signoff Signed-off-by: Jougan-0 --- errors/errors.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index 2f2b0bab..f0a6336f 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -102,35 +102,35 @@ func (e *Error) ErrorV2(additionalInfo interface{}) ErrorV2 { } func GetCode(err error) string { - if obj := err.(*Error); obj != nil && obj.Code != " " { + if obj, ok := err.(*Error); obj != nil && obj.Code != " " && !ok { return obj.Code } return strings.Join(NoneString[:], "") } func GetSeverity(err error) Severity { - if obj := err.(*Error); obj != nil { + if obj, ok := err.(*Error); obj != nil && !ok { return obj.Severity } return None } func GetSDescription(err error) string { - if obj := err.(*Error); obj != nil { + if obj, ok := err.(*Error); obj != nil && !ok { return strings.Join(err.(*Error).ShortDescription[:], ".") } return strings.Join(NoneString[:], "") } func GetCause(err error) string { - if obj := err.(*Error); obj != nil { + if obj, ok := err.(*Error); obj != nil && !ok { return strings.Join(err.(*Error).ProbableCause[:], ".") } return strings.Join(NoneString[:], "") } func GetRemedy(err error) string { - if obj := err.(*Error); obj != nil { + if obj, ok := err.(*Error); obj != nil && !ok { return strings.Join(err.(*Error).SuggestedRemediation[:], ".") } return strings.Join(NoneString[:], "") From e6f53be6ded9142b80518c887c6765b663db2039 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Wed, 29 May 2024 16:49:26 +0530 Subject: [PATCH 2/8] New error and return types w/signoff Signed-off-by: Jougan-0 --- models/meshmodel/registry/error.go | 29 +++++++++++++++++++++++++-- models/meshmodel/registry/registry.go | 11 +++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/models/meshmodel/registry/error.go b/models/meshmodel/registry/error.go index 5167c0c0..3f92b147 100644 --- a/models/meshmodel/registry/error.go +++ b/models/meshmodel/registry/error.go @@ -1,14 +1,39 @@ package registry import ( + "fmt" + "github.com/layer5io/meshkit/errors" ) var ( - ErrUnknownHostCode = "meshkit-11146" - ErrRegisterEntityCode = "" + ErrUnknownHostCode = "meshkit-11146" + ErrEmptySchemaCode = "meshkit-11098" + ErrMarshalingRegisteryAttemptsCode = "meshkit-11099" + ErrWritingRegisteryAttemptsCode = "meshkit-11100" + ErrRegisteringEntityCode = "meshkit-11101" + ErrUnknownHostInMapCode = "meshkit-11102" + ErrCreatingUserDataDirectoryCode = "meshkit-11103" ) func ErrUnknownHost(err error) error { return errors.New(ErrUnknownHostCode, errors.Alert, []string{"host is not supported"}, []string{err.Error()}, []string{"The component's host is not supported by the version of server you are running"}, []string{"Try upgrading to latest available version"}) } +func ErrUnknownHostInMap() error { + return errors.New(ErrUnknownHostCode, errors.Alert, []string{"Registrant has no error or it is not supported or unknown."}, nil, []string{"The host registering the entites has no errors with it's entites or is unknown."}, []string{"Validate the registrant name again or check /server/cmd/registery_attempts.json for futher details"}) +} +func ErrEmptySchema() error { + return errors.New(ErrEmptySchemaCode, errors.Alert, []string{"Empty schema for the component"}, []string{"Empty schema for the component"}, []string{"The schema is empty for the component."}, []string{"For the particular component the schema is empty. Use the docs or discussion forum for more details "}) +} +func ErrMarshalingRegisteryAttempts(err error) error { + return errors.New(ErrMarshalingRegisteryAttemptsCode, errors.Alert, []string{"Error marshaling RegisterAttempts to JSON"}, []string{"Error marshaling RegisterAttempts to JSON: ", err.Error()}, []string{}, []string{}) +} +func ErrWritingRegisteryAttempts(err error) error { + return errors.New(ErrWritingRegisteryAttemptsCode, errors.Alert, []string{"Error writing RegisteryAttempts JSON data to file"}, []string{"Error writing RegisteryAttempts JSON data to file:", err.Error()}, []string{}, []string{}) +} +func ErrRegisteringEntity(failedMsg string, hostName string) error { + return errors.New(ErrRegisteringEntityCode, errors.Alert, []string{fmt.Sprintf("The import process for a registrant %s encountered difficulties,due to which %s. Specific issues during the import process resulted in certain entities not being successfully registered in the table.", hostName, failedMsg)}, []string{fmt.Sprintf("For registrant %s %s", hostName, failedMsg)}, []string{"Could be because of empty schema or some issue with the json or yaml file"}, []string{"Check /server/cmd/registery_attempts.json for futher details"}) +} +func ErrCreatingUserDataDirectory(dir string) error { + return errors.New(ErrCreatingUserDataDirectoryCode, errors.Fatal, []string{"Unable to create the directory for storing user data at: ", dir}, []string{"Unable to create the directory for storing user data at: ", dir}, []string{}, []string{}) +} diff --git a/models/meshmodel/registry/registry.go b/models/meshmodel/registry/registry.go index a5d1873e..f70e2e85 100644 --- a/models/meshmodel/registry/registry.go +++ b/models/meshmodel/registry/registry.go @@ -74,17 +74,16 @@ func (rm *RegistryManager) Cleanup() { &v1alpha2.RelationshipDefinition{}, ) } -func (rm *RegistryManager) RegisterEntity(h v1beta1.Host, en entity.Entity) error { +func (rm *RegistryManager) RegisterEntity(h v1beta1.Host, en entity.Entity) (bool, bool, error) { registrantID, err := h.Create(rm.db) if err != nil { - return err + return true, false, err } entityID, err := en.Create(rm.db, registrantID) if err != nil { - return err + return false, true, err } - entry := Registry{ ID: uuid.New(), RegistrantID: registrantID, @@ -95,9 +94,9 @@ func (rm *RegistryManager) RegisterEntity(h v1beta1.Host, en entity.Entity) erro } err = rm.db.Create(&entry).Error if err != nil { - return err + return false, false, err } - return nil + return false, false, nil } // UpdateEntityIgnoreStatus updates the ignore status of an entity based on the provided parameters. From 3de3cf9b629d8b2991bf6568df3a0c1b398b6167 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Wed, 29 May 2024 16:56:41 +0530 Subject: [PATCH 3/8] panic error fix w/signoff Signed-off-by: Jougan-0 --- errors/errors.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index f0a6336f..b0885d53 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -102,36 +102,38 @@ func (e *Error) ErrorV2(additionalInfo interface{}) ErrorV2 { } func GetCode(err error) string { - if obj, ok := err.(*Error); obj != nil && obj.Code != " " && !ok { - return obj.Code + if obj, ok := err.(*Error); ok { + if obj.Code != "" { + return obj.Code + } } return strings.Join(NoneString[:], "") } func GetSeverity(err error) Severity { - if obj, ok := err.(*Error); obj != nil && !ok { + if obj, ok := err.(*Error); ok { return obj.Severity } return None } func GetSDescription(err error) string { - if obj, ok := err.(*Error); obj != nil && !ok { - return strings.Join(err.(*Error).ShortDescription[:], ".") + if obj, ok := err.(*Error); ok { + return strings.Join(obj.ShortDescription[:], ".") } return strings.Join(NoneString[:], "") } func GetCause(err error) string { - if obj, ok := err.(*Error); obj != nil && !ok { - return strings.Join(err.(*Error).ProbableCause[:], ".") + if obj, ok := err.(*Error); ok { + return strings.Join(obj.ProbableCause[:], ".") } return strings.Join(NoneString[:], "") } func GetRemedy(err error) string { - if obj, ok := err.(*Error); obj != nil && !ok { - return strings.Join(err.(*Error).SuggestedRemediation[:], ".") + if obj, ok := err.(*Error); ok { + return strings.Join(obj.SuggestedRemediation[:], ".") } return strings.Join(NoneString[:], "") } From 2996e9708e41814b7832c5f85add930e30f82a87 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Thu, 30 May 2024 18:31:25 +0530 Subject: [PATCH 4/8] rewrite error to more appropritate w/signoff Signed-off-by: Jougan-0 --- models/meshmodel/registry/error.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/models/meshmodel/registry/error.go b/models/meshmodel/registry/error.go index 3f92b147..b30b8bf7 100644 --- a/models/meshmodel/registry/error.go +++ b/models/meshmodel/registry/error.go @@ -20,8 +20,10 @@ func ErrUnknownHost(err error) error { return errors.New(ErrUnknownHostCode, errors.Alert, []string{"host is not supported"}, []string{err.Error()}, []string{"The component's host is not supported by the version of server you are running"}, []string{"Try upgrading to latest available version"}) } func ErrUnknownHostInMap() error { - return errors.New(ErrUnknownHostCode, errors.Alert, []string{"Registrant has no error or it is not supported or unknown."}, nil, []string{"The host registering the entites has no errors with it's entites or is unknown."}, []string{"Validate the registrant name again or check /server/cmd/registery_attempts.json for futher details"}) + return errors.New( + ErrUnknownHostInMapCode, errors.Alert, []string{"Host not found in registry logs."}, nil, []string{"The specified host does not have any associated registry logs or is unrecognized.", "Ensure the host name is correct and exists in the registry logs.", "Refer to /server/cmd/registryLogs.txt for more details."}, []string{"Verify the host name used during the registration process.", "Check the registry logs file for potential errors and additional information."}) } + func ErrEmptySchema() error { return errors.New(ErrEmptySchemaCode, errors.Alert, []string{"Empty schema for the component"}, []string{"Empty schema for the component"}, []string{"The schema is empty for the component."}, []string{"For the particular component the schema is empty. Use the docs or discussion forum for more details "}) } From b9defcabe0bc63898d69159ca57b51d4da3f78c2 Mon Sep 17 00:00:00 2001 From: Shlok Mishra <99207534+Jougan-0@users.noreply.github.com> Date: Thu, 30 May 2024 19:07:58 +0530 Subject: [PATCH 5/8] Update error.go Signed-off-by: Shlok Mishra <99207534+Jougan-0@users.noreply.github.com> --- models/meshmodel/registry/error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/meshmodel/registry/error.go b/models/meshmodel/registry/error.go index b30b8bf7..b49f08ca 100644 --- a/models/meshmodel/registry/error.go +++ b/models/meshmodel/registry/error.go @@ -21,7 +21,7 @@ func ErrUnknownHost(err error) error { } func ErrUnknownHostInMap() error { return errors.New( - ErrUnknownHostInMapCode, errors.Alert, []string{"Host not found in registry logs."}, nil, []string{"The specified host does not have any associated registry logs or is unrecognized.", "Ensure the host name is correct and exists in the registry logs.", "Refer to /server/cmd/registryLogs.txt for more details."}, []string{"Verify the host name used during the registration process.", "Check the registry logs file for potential errors and additional information."}) + ErrUnknownHostInMapCode, errors.Alert, []string{"Host not found in registry logs."}, nil, []string{"The specified host does not have any associated registry logs or is unrecognized.", "Ensure the host name is correct and exists in the registry logs.", "Refer to .meshery/logs/registryLogs.txt for more details."}, []string{"Verify the host name used during the registration process.", "Check the registry logs file for potential errors and additional information."}) } func ErrEmptySchema() error { From f34e48a7202641b320e2f7bbb625eebb647cacb2 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Mon, 3 Jun 2024 22:32:14 +0530 Subject: [PATCH 6/8] fix for weird None error w/signoff Signed-off-by: Jougan-0 --- errors/errors.go | 81 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index b0885d53..081f1340 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -36,7 +36,10 @@ // } package errors -import "strings" +import ( + "fmt" + "strings" +) // Deprecated: NewDefault is deprecated, use New(...) instead. func NewDefault(code string, ldescription ...string) *Error { @@ -102,40 +105,90 @@ func (e *Error) ErrorV2(additionalInfo interface{}) ErrorV2 { } func GetCode(err error) string { - if obj, ok := err.(*Error); ok { - if obj.Code != "" { - return obj.Code + var code string + defer func() { + if r := recover(); r != nil { + code = strings.Join(NoneString[:], "") } + }() + if obj, ok := err.(*Error); ok && obj.Code != "" { + code = obj.Code + } else if err != nil { + code = fmt.Sprintf("%v", err) } - return strings.Join(NoneString[:], "") -} + return code +} func GetSeverity(err error) Severity { + var severity Severity + defer func() { + if r := recover(); r != nil { + severity = None + fmt.Println("Recovered from panic:", r) + } + }() + if obj, ok := err.(*Error); ok { - return obj.Severity + severity = obj.Severity + } else if err != nil { + severity = None } - return None + + return severity } func GetSDescription(err error) string { + var description string + defer func() { + if r := recover(); r != nil { + description = "" + fmt.Println("Recovered from panic:", r) + } + }() + if obj, ok := err.(*Error); ok { - return strings.Join(obj.ShortDescription[:], ".") + description = strings.Join(obj.ShortDescription[:], ".") + } else if err != nil { + description = "" } - return strings.Join(NoneString[:], "") + + return description } func GetCause(err error) string { + var cause string + defer func() { + if r := recover(); r != nil { + cause = "" + fmt.Println("Recovered from panic:", r) + } + }() + if obj, ok := err.(*Error); ok { - return strings.Join(obj.ProbableCause[:], ".") + cause = strings.Join(obj.ProbableCause[:], ".") + } else if err != nil { + cause = "" } - return strings.Join(NoneString[:], "") + + return cause } func GetRemedy(err error) string { + var remedy string + defer func() { + if r := recover(); r != nil { + remedy = "" + fmt.Println("Recovered from panic:", r) + } + }() + if obj, ok := err.(*Error); ok { - return strings.Join(obj.SuggestedRemediation[:], ".") + remedy = strings.Join(obj.SuggestedRemediation[:], ".") + } else if err != nil { + remedy = "" } - return strings.Join(NoneString[:], "") + + return remedy } func Is(err error) (*Error, bool) { From 8ff80256d3498ba0f255bac886d8de05fb280147 Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Wed, 5 Jun 2024 05:12:08 +0530 Subject: [PATCH 7/8] revert back to old logic w/signoff Signed-off-by: Jougan-0 --- errors/errors.go | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index 081f1340..d7f1e88c 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -111,29 +111,26 @@ func GetCode(err error) string { code = strings.Join(NoneString[:], "") } }() - if obj, ok := err.(*Error); ok && obj.Code != "" { + if obj := err.(*Error); obj != nil && obj.Code != " " { code = obj.Code - } else if err != nil { - code = fmt.Sprintf("%v", err) + } else { + code = strings.Join(NoneString[:], "") } - return code } + func GetSeverity(err error) Severity { var severity Severity defer func() { if r := recover(); r != nil { severity = None - fmt.Println("Recovered from panic:", r) } }() - - if obj, ok := err.(*Error); ok { + if obj := err.(*Error); obj != nil { severity = obj.Severity - } else if err != nil { + } else { severity = None } - return severity } @@ -142,16 +139,13 @@ func GetSDescription(err error) string { defer func() { if r := recover(); r != nil { description = "" - fmt.Println("Recovered from panic:", r) } }() - - if obj, ok := err.(*Error); ok { + if obj := err.(*Error); obj != nil { description = strings.Join(obj.ShortDescription[:], ".") - } else if err != nil { + } else { description = "" } - return description } @@ -160,16 +154,13 @@ func GetCause(err error) string { defer func() { if r := recover(); r != nil { cause = "" - fmt.Println("Recovered from panic:", r) } }() - - if obj, ok := err.(*Error); ok { + if obj := err.(*Error); obj != nil { cause = strings.Join(obj.ProbableCause[:], ".") - } else if err != nil { - cause = "" + } else { + cause = fmt.Sprintf("%v", err) } - return cause } @@ -178,16 +169,13 @@ func GetRemedy(err error) string { defer func() { if r := recover(); r != nil { remedy = "" - fmt.Println("Recovered from panic:", r) } }() - - if obj, ok := err.(*Error); ok { + if obj := err.(*Error); obj != nil { remedy = strings.Join(obj.SuggestedRemediation[:], ".") } else if err != nil { remedy = "" } - return remedy } From 13c80bbefba8e42591c95fdc6c65787ae0bb1e8b Mon Sep 17 00:00:00 2001 From: Jougan-0 Date: Wed, 5 Jun 2024 05:34:20 +0530 Subject: [PATCH 8/8] revert to None from empty string w/signoff Signed-off-by: Jougan-0 --- errors/errors.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index d7f1e88c..5f1dca80 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -37,7 +37,6 @@ package errors import ( - "fmt" "strings" ) @@ -138,13 +137,13 @@ func GetSDescription(err error) string { var description string defer func() { if r := recover(); r != nil { - description = "" + description = strings.Join(NoneString[:], "") } }() if obj := err.(*Error); obj != nil { description = strings.Join(obj.ShortDescription[:], ".") } else { - description = "" + description = strings.Join(NoneString[:], "") } return description } @@ -153,13 +152,13 @@ func GetCause(err error) string { var cause string defer func() { if r := recover(); r != nil { - cause = "" + cause = strings.Join(NoneString[:], "") } }() if obj := err.(*Error); obj != nil { cause = strings.Join(obj.ProbableCause[:], ".") } else { - cause = fmt.Sprintf("%v", err) + cause = strings.Join(NoneString[:], "") } return cause } @@ -168,13 +167,13 @@ func GetRemedy(err error) string { var remedy string defer func() { if r := recover(); r != nil { - remedy = "" + remedy = strings.Join(NoneString[:], "") } }() if obj := err.(*Error); obj != nil { remedy = strings.Join(obj.SuggestedRemediation[:], ".") } else if err != nil { - remedy = "" + remedy = strings.Join(NoneString[:], "") } return remedy }