MSP-5014 Update error handling in config repo

This commit is contained in:
Синельник Тимур Романович committed 2025-03-21 10:08:25 +03:00
1 parent 0005f1ac0c
commit 69319f0d2b
3 files changed
+75 -51

No files matched your search

+7
View File
@@ -1,5 +1,12 @@
package entity
import "github.com/pkg/errors"
var (
ErrModuleNotFound = errors.New("module not found")
ErrVariableNotFound = errors.New("variable not found")
)
const (
SecretVariableType = "SECRET"
TextVariableType = "TEXT"
+51 -7
View File
@@ -2,10 +2,17 @@ package repository
import (
"context"
"github.com/pkg/errors"
"github.com/txix-open/isp-kit/grpc/client"
"fmt"
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
"ispctl/entity"
"strings"
"time"
"github.com/pkg/errors"
"github.com/txix-open/isp-kit/grpc/apierrors"
"github.com/txix-open/isp-kit/grpc/client"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
@@ -52,10 +59,14 @@ func (c Config) GetConfigByModuleName(name string) (*entity.Config, error) {
JsonResponseBody(&response).
Timeout(time.Second).
Do(c.baseCtx)
if err != nil {
switch {
case status.Code(err) == codes.NotFound:
return nil, entity.ErrModuleNotFound
case err != nil:
return nil, errors.WithMessagef(err, "call %s", getConfigByModuleName)
default:
return response, nil
}
return response, nil
}
func (c Config) CreateUpdateConfig(request entity.Config) (*entity.Config, error) {
@@ -66,7 +77,7 @@ func (c Config) CreateUpdateConfig(request entity.Config) (*entity.Config, error
Timeout(time.Second).
Do(c.baseCtx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", createUpdateConfig)
return nil, c.handleCreateUpdateConfigError(err)
}
return response, nil
}
@@ -106,7 +117,7 @@ func (c Config) GetVariableByName(variableName string) (*entity.Variable, error)
Timeout(time.Second).
Do(c.baseCtx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getVariableByName)
return nil, c.handleVariableError(err, getVariableByName)
}
return response, nil
}
@@ -129,7 +140,40 @@ func (c Config) DeleteVariable(variableName string) error {
Timeout(time.Second).
Do(c.baseCtx)
if err != nil {
return errors.WithMessagef(err, "call %s", deleteVariable)
return c.handleVariableError(err, deleteVariable)
}
return nil
}
func (c Config) handleVariableError(err error, endpoint string) error {
apiError := apierrors.FromError(err)
if apiError == nil {
return errors.WithMessagef(err, "call %s", endpoint)
}
switch {
case apiError.ErrorCode == entity.ErrCodeVariableNotFound:
return entity.ErrVariableNotFound
default:
return errors.WithMessagef(err, "call %s", endpoint)
}
}
func (c Config) handleCreateUpdateConfigError(err error) error {
errorStatus, ok := status.FromError(err)
if !ok || errorStatus.Code() != codes.InvalidArgument {
return errors.WithMessagef(err, "call %s", createUpdateConfig)
}
stringBuilder := strings.Builder{}
stringBuilder.WriteString("doesn't match with schema:\n")
for _, value := range errorStatus.Details() {
switch errorDetails := value.(type) {
case *epb.BadRequest:
for _, value := range errorDetails.FieldViolations {
stringBuilder.WriteString(fmt.Sprintf("%s | %s\n", value.Field, value.Description))
}
default:
stringBuilder.WriteString(fmt.Sprint(value))
}
}
return errors.New(stringBuilder.String())
}
+17 -44
View File
@@ -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 {