MSP-5014 Update error handling in config repo
This commit is contained in:
1 parent
0005f1ac0c
commit
69319f0d2b
3 files changed
+75
-51
No files matched your search
+17
-44
@@ -2,18 +2,12 @@ package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"ispctl/entity"
|
||||
"ispctl/repository"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
"github.com/txix-open/isp-kit/rc/schema"
|
||||
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
var Config = &ConfigService{}
|
||||
@@ -42,14 +36,12 @@ func (c *ConfigService) GetConfigurationByModuleName(moduleName string) (*entity
|
||||
}
|
||||
|
||||
moduleConfiguration, err := c.configRepo.GetConfigByModuleName(moduleName)
|
||||
if err == nil {
|
||||
return moduleConfiguration, nil
|
||||
}
|
||||
errorStatus, ok := status.FromError(err)
|
||||
if ok && errorStatus.Code() == codes.NotFound {
|
||||
switch {
|
||||
case errors.Is(err, entity.ErrModuleNotFound):
|
||||
return nil, errors.Errorf("module [%s] not found", moduleName)
|
||||
default:
|
||||
return moduleConfiguration, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (c *ConfigService) GetSchemaByModuleId(moduleId string) (schema.Schema, error) {
|
||||
@@ -76,29 +68,10 @@ func (c *ConfigService) CreateUpdateConfig(stringToChange string, configuration
|
||||
func (c *ConfigService) CreateUpdateConfigV2(configuration *entity.Config) (map[string]any, error) {
|
||||
configuration.Unsafe = c.UnsafeEnable
|
||||
resp, err := c.configRepo.CreateUpdateConfig(*configuration)
|
||||
if err == nil {
|
||||
return resp.Data, nil
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
errorStatus, ok := status.FromError(err)
|
||||
if ok && errorStatus.Code() == codes.InvalidArgument {
|
||||
fmt.Print("doesn't match with schema:\n")
|
||||
defer func() {
|
||||
os.Exit(1)
|
||||
}()
|
||||
for _, value := range errorStatus.Details() {
|
||||
switch errorDetails := value.(type) {
|
||||
case *epb.BadRequest:
|
||||
for _, value := range errorDetails.FieldViolations {
|
||||
fmt.Printf("%s | %s\n", value.Field, value.Description)
|
||||
}
|
||||
default:
|
||||
fmt.Println(value)
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (c *ConfigService) GetAllVariables() ([]entity.Variable, error) {
|
||||
@@ -111,15 +84,14 @@ func (c *ConfigService) GetVariableByName(variableName string) (*entity.Variable
|
||||
}
|
||||
|
||||
variable, err := c.configRepo.GetVariableByName(variableName)
|
||||
if err == nil {
|
||||
switch {
|
||||
case errors.Is(err, entity.ErrVariableNotFound):
|
||||
return nil, errors.Errorf("variable [%s] not found", variableName)
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
return variable, nil
|
||||
}
|
||||
|
||||
apiError := apierrors.FromError(err)
|
||||
if apiError != nil && apiError.ErrorCode == entity.ErrCodeVariableNotFound {
|
||||
return nil, errors.Errorf("variable [%s] not found", variableName)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (c *ConfigService) DeleteVariable(variableName string) error {
|
||||
@@ -128,11 +100,12 @@ func (c *ConfigService) DeleteVariable(variableName string) error {
|
||||
}
|
||||
|
||||
err := c.configRepo.DeleteVariable(variableName)
|
||||
apiError := apierrors.FromError(err)
|
||||
if apiError != nil && apiError.ErrorCode == entity.ErrCodeVariableNotFound {
|
||||
switch {
|
||||
case errors.Is(err, entity.ErrVariableNotFound):
|
||||
return errors.Errorf("variable [%s] not found", variableName)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *ConfigService) UpsertVariables(vars []entity.UpsertVariableRequest) error {
|
||||
|
||||
Reference in new issue
Block a user