Skip to content

Commit

Permalink
Merge pull request #499 from Jougan-0/errorHandlingForRegistry
Browse files Browse the repository at this point in the history
Error handling for registry
  • Loading branch information
leecalcote authored Jun 17, 2024
2 parents b08551c + 43f8927 commit 761df57
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
64 changes: 53 additions & 11 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
// }
package errors

import "strings"
import (
"strings"
)

// Deprecated: NewDefault is deprecated, use New(...) instead.
func NewDefault(code string, ldescription ...string) *Error {
Expand Down Expand Up @@ -102,38 +104,78 @@ func (e *Error) ErrorV2(additionalInfo interface{}) ErrorV2 {
}

func GetCode(err error) string {
var code string
defer func() {
if r := recover(); r != nil {
code = strings.Join(NoneString[:], "")
}
}()
if obj := err.(*Error); obj != nil && obj.Code != " " {
return obj.Code
code = obj.Code
} else {
code = strings.Join(NoneString[:], "")
}
return strings.Join(NoneString[:], "")
return code
}

func GetSeverity(err error) Severity {
var severity Severity
defer func() {
if r := recover(); r != nil {
severity = None
}
}()
if obj := err.(*Error); obj != nil {
return obj.Severity
severity = obj.Severity
} else {
severity = None
}
return None
return severity
}

func GetSDescription(err error) string {
var description string
defer func() {
if r := recover(); r != nil {
description = strings.Join(NoneString[:], "")
}
}()
if obj := err.(*Error); obj != nil {
return strings.Join(err.(*Error).ShortDescription[:], ".")
description = strings.Join(obj.ShortDescription[:], ".")
} else {
description = strings.Join(NoneString[:], "")
}
return strings.Join(NoneString[:], "")
return description
}

func GetCause(err error) string {
var cause string
defer func() {
if r := recover(); r != nil {
cause = strings.Join(NoneString[:], "")
}
}()
if obj := err.(*Error); obj != nil {
return strings.Join(err.(*Error).ProbableCause[:], ".")
cause = strings.Join(obj.ProbableCause[:], ".")
} else {
cause = strings.Join(NoneString[:], "")
}
return strings.Join(NoneString[:], "")
return cause
}

func GetRemedy(err error) string {
var remedy string
defer func() {
if r := recover(); r != nil {
remedy = strings.Join(NoneString[:], "")
}
}()
if obj := err.(*Error); obj != nil {
return strings.Join(err.(*Error).SuggestedRemediation[:], ".")
remedy = strings.Join(obj.SuggestedRemediation[:], ".")
} else if err != nil {
remedy = strings.Join(NoneString[:], "")
}
return strings.Join(NoneString[:], "")
return remedy
}

func Is(err error) (*Error, bool) {
Expand Down
33 changes: 31 additions & 2 deletions models/meshmodel/registry/error.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
package registry

import (
"fmt"

"github.com/layer5io/meshkit/errors"
)

var (
ErrUnknownHostCode = "meshkit-11146"
ErrRegisterEntityCode = "meshkit-11244"

ErrUnknownHostCode = "replace_me"
ErrEmptySchemaCode = "replace_me"
ErrMarshalingRegisteryAttemptsCode = "replace_me"
ErrWritingRegisteryAttemptsCode = "replace_me"
ErrRegisteringEntityCode = "replace_me"
ErrUnknownHostInMapCode = "replace_me"
ErrCreatingUserDataDirectoryCode = "replace_me"

)

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(
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 {
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{})
}
11 changes: 5 additions & 6 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

// UpdateEntityStatus updates the ignore status of an entity based on the provided parameters.
Expand Down

0 comments on commit 761df57

Please sign in to comment.