diff --git a/controller/access_list.go b/controller/access_list.go index ba53652..6ee48e1 100644 --- a/controller/access_list.go +++ b/controller/access_list.go @@ -2,11 +2,13 @@ package controller import ( "context" + "fmt" + + "isp-system-service/domain" "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - "isp-system-service/domain" ) type AccessListService interface { @@ -34,14 +36,19 @@ func NewAccessList(service AccessListService) AccessList { // @Produce json // @Param body body domain.Identity false "идентификатор приложения" // @Success 200 {array} domain.MethodInfo "список доступности методов" -// @Failure 404 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /access_list/get_by_id [POST] func (c AccessList) GetById(ctx context.Context, req domain.Identity) ([]domain.MethodInfo, error) { result, err := c.service.GetById(ctx, req.Id) switch { case errors.Is(err, domain.ErrApplicationNotFound): - return nil, status.Errorf(codes.NotFound, "application not found") + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.Id), + err, + ) case err != nil: return nil, err default: @@ -58,14 +65,19 @@ func (c AccessList) GetById(ctx context.Context, req domain.Identity) ([]domain. // @Produce json // @Param body body domain.AccessListSetOneRequest false "объект для настройки доступа" // @Success 200 {object} domain.AccessListSetOneResponse "количество измененных строк" -// @Failure 404 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /access_list/set_one [POST] func (c AccessList) SetOne(ctx context.Context, req domain.AccessListSetOneRequest) (*domain.AccessListSetOneResponse, error) { result, err := c.service.SetOne(ctx, req) switch { case errors.Is(err, domain.ErrApplicationNotFound): - return nil, status.Errorf(codes.NotFound, "application not found") + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.AppId), + err, + ) case err != nil: return nil, err default: @@ -82,14 +94,19 @@ func (c AccessList) SetOne(ctx context.Context, req domain.AccessListSetOneReque // @Produce json // @Param body body domain.AccessListSetListRequest false "объект настройки доступа" // @Success 200 {array} domain.MethodInfo "список доступности методов" -// @Failure 404 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /access_list/set_list [POST] func (c AccessList) SetList(ctx context.Context, req domain.AccessListSetListRequest) ([]domain.MethodInfo, error) { result, err := c.service.SetList(ctx, req) switch { case errors.Is(err, domain.ErrApplicationNotFound): - return nil, status.Errorf(codes.NotFound, "application not found") + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.AppId), + err, + ) case err != nil: return nil, err default: diff --git a/controller/app_group.go b/controller/app_group.go index 99a7f22..171fcf0 100644 --- a/controller/app_group.go +++ b/controller/app_group.go @@ -2,11 +2,12 @@ package controller import ( "context" + "fmt" "isp-system-service/domain" "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type AppGroupService interface { @@ -35,15 +36,20 @@ func NewAppGroup(service AppGroupService) AppGroup { // @Produce json // @Param body body domain.CreateAppGroupRequest true "Объект группы приложений" // @Success 200 {object} domain.AppGroup -// @Failure 400 {object} domain.GrpcError -// @Failure 409 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 409 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application_group/create [POST] func (c AppGroup) Create(ctx context.Context, req domain.CreateAppGroupRequest) (*domain.AppGroup, error) { result, err := c.service.Create(ctx, req) switch { case errors.Is(err, domain.ErrAppGroupDuplicateName): - return nil, status.Errorf(codes.AlreadyExists, "application group with name %s already exists", req.Name) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeAppGroupDuplicateName, + fmt.Sprintf("application group with name %s already exists", req.Name), + err, + ) default: return result, err } @@ -58,18 +64,28 @@ func (c AppGroup) Create(ctx context.Context, req domain.CreateAppGroupRequest) // @Produce json // @Param body body domain.UpdateAppGroupRequest true "Объект группы приложений" // @Success 200 {object} domain.AppGroup -// @Failure 400 {object} domain.GrpcError -// @Failure 404 {object} domain.GrpcError -// @Failure 409 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 404 {object} apierrors.Error +// @Failure 409 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application_group/update [POST] func (c AppGroup) Update(ctx context.Context, req domain.UpdateAppGroupRequest) (*domain.AppGroup, error) { result, err := c.service.Update(ctx, req) switch { case errors.Is(err, domain.ErrAppGroupDuplicateName): - return nil, status.Errorf(codes.AlreadyExists, "application group with name %s already exists", req.Name) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeAppGroupDuplicateName, + fmt.Sprintf("application group with name %s already exists", req.Name), + err, + ) case errors.Is(err, domain.ErrAppGroupNotFound): - return nil, status.Errorf(codes.NotFound, "application group with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("application group with id %d not found", req.Id), + err, + ) default: return result, err } @@ -84,8 +100,8 @@ func (c AppGroup) Update(ctx context.Context, req domain.UpdateAppGroupRequest) // @Produce json // @Param body body domain.IdListRequest true "список идентификаторов групп приложений" // @Success 200 {object} domain.DeleteResponse -// @Failure 400 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application_group/delete_list [POST] func (c AppGroup) DeleteList(ctx context.Context, req domain.IdListRequest) (*domain.DeleteResponse, error) { return c.service.DeleteList(ctx, req) @@ -100,8 +116,8 @@ func (c AppGroup) DeleteList(ctx context.Context, req domain.IdListRequest) (*do // @Produce json // @Param body body domain.IdListRequest true "список идентификаторов групп приложений" // @Success 200 {array} domain.AppGroup -// @Failure 400 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application_group/get_by_id_list [POST] func (c AppGroup) GetByIdList(ctx context.Context, req domain.IdListRequest) ([]domain.AppGroup, error) { return c.service.GetByIdList(ctx, req.IdList) diff --git a/controller/application.go b/controller/application.go index 1a4fe50..aad3bb8 100644 --- a/controller/application.go +++ b/controller/application.go @@ -2,12 +2,13 @@ package controller import ( "context" + "fmt" "isp-system-service/domain" "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type ApplicationService interface { @@ -42,15 +43,20 @@ func NewApplication(service ApplicationService) Application { // @Produce json // @Param body body domain.Identity true "Идентификатор приложения" // @Success 200 {object} domain.ApplicationWithTokens -// @Failure 400 {object} domain.GrpcError -// @Failure 404 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application/get_application_by_id [POST] func (c Application) GetById(ctx context.Context, req domain.Identity) (*domain.ApplicationWithTokens, error) { result, err := c.service.GetById(ctx, req.Id) switch { case errors.Is(err, domain.ErrApplicationNotFound): - return nil, status.Errorf(codes.NotFound, "application with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.Id), + err, + ) case err != nil: return nil, err default: @@ -67,7 +73,7 @@ func (c Application) GetById(ctx context.Context, req domain.Identity) (*domain. // @Produce json // @Param body body []integer false "Массив идентификаторов приложений" // @Success 200 {array} domain.ApplicationWithTokens -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /application/get_applications [POST] func (c Application) GetByIdList(ctx context.Context, req []int) ([]*domain.ApplicationWithTokens, error) { return c.service.GetByIdList(ctx, req) @@ -82,7 +88,7 @@ func (c Application) GetByIdList(ctx context.Context, req []int) ([]*domain.Appl // @Produce json // @Param body body domain.Identity true "Идентификатор сервиса" // @Success 200 {array} domain.ApplicationWithTokens -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /application/get_applications_by_service_id [POST] func (c Application) GetByServiceId(ctx context.Context, req domain.Identity) ([]*domain.ApplicationWithTokens, error) { return c.service.GetByServiceId(ctx, req.Id) @@ -96,7 +102,7 @@ func (c Application) GetByServiceId(ctx context.Context, req domain.Identity) ([ // @Accept json // @Produce json // @Success 200 {array} domain.DomainWithService -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /application/get_system_tree [POST] func (c Application) GetSystemTree(ctx context.Context) ([]*domain.DomainWithService, error) { return c.service.SystemTree(ctx, domain.DefaultSystemId) @@ -111,20 +117,34 @@ func (c Application) GetSystemTree(ctx context.Context) ([]*domain.DomainWithSer // @Produce json // @Param body body domain.ApplicationCreateUpdateRequest true "Объект приложения" // @Success 200 {object} domain.ApplicationWithTokens -// @Failure 400 {object} domain.GrpcError -// @Failure 404 {object} domain.GrpcError -// @Failure 409 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 404 {object} apierrors.Error +// @Failure 409 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application/create_update_application [POST] func (c Application) CreateUpdate(ctx context.Context, req domain.ApplicationCreateUpdateRequest) (*domain.ApplicationWithTokens, error) { result, err := c.service.CreateUpdate(ctx, req) switch { case errors.Is(err, domain.ErrAppGroupNotFound): - return nil, status.Errorf(codes.InvalidArgument, "service with id %d not found", req.ServiceId) + return nil, apierrors.NewBusinessError( + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("service with id %d not found", req.ServiceId), + err, + ) case errors.Is(err, domain.ErrApplicationDuplicateName): - return nil, status.Errorf(codes.AlreadyExists, "application with name %s already exists", req.Name) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeApplicationDuplicateName, + fmt.Sprintf("application with name %s already exists", req.Name), + err, + ) case errors.Is(err, domain.ErrApplicationNotFound): - return nil, status.Errorf(codes.NotFound, "application with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.Id), + err, + ) case err != nil: return nil, err default: @@ -141,12 +161,13 @@ func (c Application) CreateUpdate(ctx context.Context, req domain.ApplicationCre // @Produce json // @Param body body []integer false "Массив идентификаторов приложений" // @Success 200 {object} domain.DeleteResponse -// @Failure 400 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application/delete_applications [POST] func (c Application) Delete(ctx context.Context, req []int) (*domain.DeleteResponse, error) { if len(req) == 0 { - return nil, status.Errorf(codes.InvalidArgument, "At least one id are required") + return nil, apierrors.NewBusinessError(domain.ErrCodeInvalidRequest, + "At least one id are required", errors.New("invalid id count")) } result, err := c.service.Delete(ctx, req) @@ -167,7 +188,7 @@ func (c Application) Delete(ctx context.Context, req []int) (*domain.DeleteRespo // @Accept json // @Produce json // @Success 200 {object} integer -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /application/next_id [POST] func (c Application) NextId(ctx context.Context) (int, error) { return c.service.NextId(ctx) @@ -181,7 +202,7 @@ func (c Application) NextId(ctx context.Context) (int, error) { // @Accept json // @Produce json // @Success 200 {array} domain.Application -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /application/get_all [POST] func (c Application) GetAll(ctx context.Context) ([]domain.Application, error) { return c.service.GetAll(ctx) @@ -196,19 +217,33 @@ func (c Application) GetAll(ctx context.Context) ([]domain.Application, error) { // @Produce json // @Param body body domain.CreateApplicationRequest true "Объект приложения" // @Success 200 {object} domain.ApplicationWithTokens -// @Failure 400 {object} domain.GrpcError -// @Failure 409 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 409 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /application/create_application [POST] func (c Application) Create(ctx context.Context, req domain.CreateApplicationRequest) (*domain.ApplicationWithTokens, error) { result, err := c.service.Create(ctx, req) switch { case errors.Is(err, domain.ErrAppGroupNotFound): - return nil, status.Errorf(codes.InvalidArgument, "application group with id %d not found", req.ApplicationGroupId) + return nil, apierrors.NewBusinessError( + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("application group with id %d not found", req.ApplicationGroupId), + err, + ) case errors.Is(err, domain.ErrApplicationDuplicateName): - return nil, status.Errorf(codes.AlreadyExists, "application with name %s already exists", req.Name) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeApplicationDuplicateName, + fmt.Sprintf("application with name %s already exists", req.Name), + err, + ) case errors.Is(err, domain.ErrApplicationDuplicateId): - return nil, status.Errorf(codes.AlreadyExists, "application with id %d already exists", req.Id) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeApplicationDuplicateId, + fmt.Sprintf("application with id %d already exists", req.Id), + err, + ) default: return result, err } diff --git a/controller/domain.go b/controller/domain.go index 4e479f9..7e40923 100644 --- a/controller/domain.go +++ b/controller/domain.go @@ -2,11 +2,13 @@ package controller import ( "context" + "fmt" + + "isp-system-service/domain" "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - "isp-system-service/domain" ) type DomainService interface { @@ -35,14 +37,19 @@ func NewDomain(service DomainService) Domain { // @Produce json // @Param body body domain.Identity true "Идентификатор домена" // @Success 200 {object} domain.Domain -// @Failure 404 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /domain/get_domain_by_id [POST] func (c Domain) GetById(ctx context.Context, req domain.Identity) (*domain.Domain, error) { result, err := c.service.GetById(ctx, req.Id) switch { case errors.Is(err, domain.ErrDomainNotFound): - return nil, status.Errorf(codes.NotFound, "domain with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeDomainNotFound, + fmt.Sprintf("domain with id %d not found", req.Id), + err, + ) case err != nil: return nil, err default: @@ -59,7 +66,7 @@ func (c Domain) GetById(ctx context.Context, req domain.Identity) (*domain.Domai // @Produce json // @Param body body integer false "Идентификатор системы" // @Success 200 {array} domain.Domain -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /domain/get_domains_by_system_id [POST] func (c Domain) GetBySystemId(ctx context.Context) ([]domain.Domain, error) { return c.service.GetBySystemId(ctx, domain.DefaultSystemId) @@ -74,17 +81,31 @@ func (c Domain) GetBySystemId(ctx context.Context) ([]domain.Domain, error) { // @Produce json // @Param body body domain.DomainCreateUpdateRequest true "Объект домена" // @Success 200 {object} domain.Domain -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /domain/create_update_domain [POST] func (c Domain) CreateUpdate(ctx context.Context, req domain.DomainCreateUpdateRequest) (*domain.Domain, error) { result, err := c.service.CreateUpdate(ctx, req, domain.DefaultSystemId) switch { case errors.Is(err, domain.ErrSystemNotFound): - return nil, status.Errorf(codes.InvalidArgument, "system with id %d not found", domain.DefaultSystemId) + return nil, apierrors.NewBusinessError( + domain.ErrCodeSystemNotFound, + fmt.Sprintf("system with id %d not found", domain.DefaultSystemId), + err, + ) case errors.Is(err, domain.ErrDomainNotFound): - return nil, status.Errorf(codes.NotFound, "domain with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeDomainNotFound, + fmt.Sprintf("domain with id %d not found", req.Id), + err, + ) case errors.Is(err, domain.ErrDomainDuplicateName): - return nil, status.Errorf(codes.AlreadyExists, "domain with name %s already exists", req.Name) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeDomainDuplicateName, + fmt.Sprintf("domain with name %s already exists", req.Name), + err, + ) case err != nil: return nil, err default: @@ -101,12 +122,13 @@ func (c Domain) CreateUpdate(ctx context.Context, req domain.DomainCreateUpdateR // @Produce json // @Param body body []integer false "Массив идентификаторов доменов" // @Success 200 {object} domain.DeleteResponse -// @Failure 400 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /domain/delete_domains [POST] func (c Domain) Delete(ctx context.Context, req []int) (*domain.DeleteResponse, error) { if len(req) == 0 { - return nil, status.Errorf(codes.InvalidArgument, "at least one id are required") + return nil, apierrors.NewBusinessError(domain.ErrCodeInvalidRequest, + "At least one id are required", errors.New("invalid id count")) } result, err := c.service.Delete(ctx, req) diff --git a/controller/secure.go b/controller/secure.go index f0eb07b..02b0144 100644 --- a/controller/secure.go +++ b/controller/secure.go @@ -31,7 +31,7 @@ func NewSecure(service SecureService) Secure { // @Produce json // @Param body body domain.AuthenticateRequest true "Тело запроса" // @Success 200 {array} domain.AuthenticateResponse -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /secure/authenticate [POST] func (c Secure) Authenticate(ctx context.Context, req domain.AuthenticateRequest) (*domain.AuthenticateResponse, error) { result, err := c.service.Authenticate(ctx, req.Token) @@ -65,7 +65,7 @@ func (c Secure) Authenticate(ctx context.Context, req domain.AuthenticateRequest // @Produce json // @Param body body domain.AuthorizeRequest true "Тело запрос" // @Success 200 {array} domain.AuthorizeResponse -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /secure/authorize [POST] func (c Secure) Authorize(ctx context.Context, req domain.AuthorizeRequest) (*domain.AuthorizeResponse, error) { result, err := c.service.Authorize(ctx, req.ApplicationId, req.Endpoint) diff --git a/controller/service.go b/controller/service.go index 6117153..f081369 100644 --- a/controller/service.go +++ b/controller/service.go @@ -2,12 +2,13 @@ package controller import ( "context" + "fmt" "isp-system-service/domain" "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type ServiceService interface { @@ -37,14 +38,19 @@ func NewService(service ServiceService) Service { // @Produce json // @Param body body domain.Identity true "Идентификатор сервиса" // @Success 200 {object} domain.Service -// @Failure 404 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 404 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /service/get_service_by_id [POST] func (c Service) GetById(ctx context.Context, req domain.Identity) (*domain.Service, error) { result, err := c.service.GetById(ctx, req.Id) switch { case errors.Is(err, domain.ErrAppGroupNotFound): - return nil, status.Errorf(codes.NotFound, "service with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("service with id %d not found", req.Id), + err, + ) case err != nil: return nil, err default: @@ -61,7 +67,7 @@ func (c Service) GetById(ctx context.Context, req domain.Identity) (*domain.Serv // @Produce json // @Param body body []integer false "Массив идентификаторов сервисов" // @Success 200 {array} domain.Service -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /service/get_service [POST] func (c Service) Get(ctx context.Context, req []int) ([]domain.Service, error) { return c.service.GetByIdList(ctx, req) @@ -76,7 +82,7 @@ func (c Service) Get(ctx context.Context, req []int) ([]domain.Service, error) { // @Produce json // @Param body body domain.Identity true "Идентификатор домена" // @Success 200 {array} domain.Service -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /service/get_services_by_domain_id [POST] func (c Service) GetByDomainId(ctx context.Context, req domain.Identity) ([]domain.Service, error) { return c.service.GetByDomainId(ctx, req.Id) @@ -91,20 +97,34 @@ func (c Service) GetByDomainId(ctx context.Context, req domain.Identity) ([]doma // @Produce json // @Param body body domain.ServiceCreateUpdateRequest true "Объект сервиса" // @Success 200 {object} domain.Service -// @Failure 400 {object} domain.GrpcError -// @Failure 404 {object} domain.GrpcError -// @Failure 409 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 404 {object} apierrors.Error +// @Failure 409 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /service/create_update_service [POST] func (c Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdateRequest) (*domain.Service, error) { result, err := c.service.CreateUpdate(ctx, req) switch { case errors.Is(err, domain.ErrDomainNotFound): - return nil, status.Errorf(codes.InvalidArgument, "domain with id %d not found", req.DomainId) + return nil, apierrors.NewBusinessError( + domain.ErrCodeDomainNotFound, + fmt.Sprintf("domain with id %d not found", req.DomainId), + err, + ) case errors.Is(err, domain.ErrAppGroupNotFound): - return nil, status.Errorf(codes.NotFound, "service with id %d not found", req.Id) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("service with id %d not found", req.Id), + err, + ) case errors.Is(err, domain.ErrAppGroupDuplicateName): - return nil, status.Errorf(codes.AlreadyExists, "service with name %s already exists", req.Name) + return nil, apierrors.New( + codes.AlreadyExists, + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("service with name %s already exists", req.Name), + err, + ) case err != nil: return nil, err default: @@ -121,12 +141,13 @@ func (c Service) CreateUpdate(ctx context.Context, req domain.ServiceCreateUpdat // @Produce json // @Param body body []integer true "Массив идентификаторов сервисов" // @Success 200 {object} domain.DeleteResponse -// @Failure 400 {object} domain.GrpcError -// @Failure 500 {object} domain.GrpcError +// @Failure 400 {object} apierrors.Error +// @Failure 500 {object} apierrors.Error // @Router /service/delete_service [POST] func (c Service) Delete(ctx context.Context, req []int) (*domain.DeleteResponse, error) { if len(req) == 0 { - return nil, status.Error(codes.InvalidArgument, "at least one id are required") + return nil, apierrors.NewBusinessError(domain.ErrCodeInvalidRequest, + "At least one id are required", errors.New("invalid id count")) } result, err := c.service.Delete(ctx, req) diff --git a/controller/token.go b/controller/token.go index fdf64ab..63f65fc 100644 --- a/controller/token.go +++ b/controller/token.go @@ -2,12 +2,13 @@ package controller import ( "context" + "fmt" "isp-system-service/domain" "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type TokenService interface { @@ -36,7 +37,7 @@ func NewToken(service TokenService) Token { // @Produce json // @Param body body domain.Identity true "Идентификатор приложения" // @Success 200 {array} entity.Token -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /token/get_tokens_by_app_id [POST] func (c Token) GetByAppId(ctx context.Context, req domain.Identity) ([]domain.Token, error) { return c.service.GetByAppId(ctx, req.Id) @@ -51,17 +52,32 @@ func (c Token) GetByAppId(ctx context.Context, req domain.Identity) ([]domain.To // @Produce json // @Param body body domain.TokenCreateRequest true "Объект создания токена" // @Success 200 {object} domain.ApplicationWithTokens -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /token/create_token [POST] func (c Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*domain.ApplicationWithTokens, error) { result, err := c.service.Create(ctx, req) switch { case errors.Is(err, domain.ErrApplicationNotFound): - return nil, status.Errorf(codes.NotFound, "application with id %d not found", req.AppId) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeApplicationNotFound, + fmt.Sprintf("application with id %d not found", req.AppId), + err, + ) case errors.Is(err, domain.ErrAppGroupNotFound): - return nil, status.Errorf(codes.NotFound, "service for app_id id %d not found", req.AppId) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeAppGroupNotFound, + fmt.Sprintf("service for app_id id %d not found", req.AppId), + err, + ) case errors.Is(err, domain.ErrDomainNotFound): - return nil, status.Errorf(codes.NotFound, "domain for app_id %d not found", req.AppId) + return nil, apierrors.New( + codes.NotFound, + domain.ErrCodeDomainNotFound, + fmt.Sprintf("domain for app_id id %d not found", req.AppId), + err, + ) case err != nil: return nil, err default: @@ -78,7 +94,7 @@ func (c Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*doma // @Produce json // @Param body body domain.TokenRevokeRequest true "Объект отзыва токенов" // @Success 200 {object} domain.ApplicationWithTokens -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /token/revoke_tokens [POST] func (c Token) Revoke(ctx context.Context, req domain.TokenRevokeRequest) (*domain.ApplicationWithTokens, error) { return c.service.Revoke(ctx, req) @@ -93,7 +109,7 @@ func (c Token) Revoke(ctx context.Context, req domain.TokenRevokeRequest) (*doma // @Produce json // @Param body body domain.Identity true "Идентификатор приложения" // @Success 200 {object} domain.DeleteResponse -// @Failure 500 {object} domain.GrpcError +// @Failure 500 {object} apierrors.Error // @Router /token/revoke_tokens_for_app [POST] func (c Token) RevokeForApp(ctx context.Context, req domain.Identity) (*domain.DeleteResponse, error) { return c.service.RevokeByAppId(ctx, req.Id) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 6009871..534432b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,5 +1,15 @@ basePath: /api/system definitions: + apierrors.Error: + properties: + details: + additionalProperties: true + type: object + errorCode: + type: integer + errorMessage: + type: string + type: object domain.AccessListSetListRequest: properties: appId: @@ -172,6 +182,7 @@ definitions: type: string required: - applicationGroupId + - id - name - type type: object @@ -219,16 +230,6 @@ definitions: $ref: '#/definitions/domain.ServiceWithApps' type: array type: object - domain.GrpcError: - properties: - details: - items: {} - type: array - errorCode: - type: string - errorMessage: - type: string - type: object domain.IdListRequest: properties: idList: @@ -343,13 +344,16 @@ definitions: properties: description: type: string - id: - type: integer name: type: string + newId: + type: integer + oldId: + type: integer required: - - id - name + - newId + - oldId type: object entity.Token: properties: @@ -395,11 +399,11 @@ paths: "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить список доступности методов для приложения tags: - accessList @@ -427,11 +431,11 @@ paths: "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Настроить доступность списка методов для приложения tags: - accessList @@ -456,11 +460,11 @@ paths: "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Настроить доступность метода для приложения tags: - accessList @@ -487,15 +491,15 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "409": description: Conflict schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Создать приложение tags: - application @@ -522,19 +526,19 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "409": description: Conflict schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Создать/обновить приложение tags: - application @@ -562,11 +566,11 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Удалить приложения tags: - application @@ -587,7 +591,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить список приложений tags: - application @@ -613,15 +617,15 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить приложение по идентификатору tags: - application @@ -650,7 +654,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить список приложений tags: - application @@ -678,7 +682,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить список приложений по идентификатору сервиса tags: - application @@ -699,7 +703,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Метод получения системного дерева tags: - application @@ -718,7 +722,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить следующий идентификатор приложения tags: - application @@ -745,19 +749,19 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "409": description: Conflict schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Обновить приложение tags: - application @@ -783,15 +787,15 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "409": description: Conflict schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Создать группу приложений tags: - application_group @@ -818,11 +822,11 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Удалить группы приложений tags: - application_group @@ -850,11 +854,11 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить группы приложений по списку идентификаторов tags: - application_group @@ -881,19 +885,19 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "409": description: Conflict schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Обновить группу приложений tags: - application_group @@ -920,7 +924,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Создать/обновить домен tags: - domain @@ -948,11 +952,11 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Удаление доменов tags: - domain @@ -978,11 +982,11 @@ paths: "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить домен по идентификатору tags: - domain @@ -1009,7 +1013,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить домены по идентификатору системы tags: - domain @@ -1037,7 +1041,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Метод аутентификации токена tags: - secure @@ -1065,7 +1069,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Метод авторизации приложения tags: - secure @@ -1092,19 +1096,19 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "409": description: Conflict schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Создать/обновить сервис tags: - service @@ -1133,11 +1137,11 @@ paths: "400": description: Bad Request schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Удалить сервисы tags: - service @@ -1166,7 +1170,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить список сервисов tags: - service @@ -1192,11 +1196,11 @@ paths: "404": description: Not Found schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить сервис по идентификатору tags: - service @@ -1224,7 +1228,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить список сервисов по идентификатору домена tags: - service @@ -1250,7 +1254,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Создать токен tags: - token @@ -1278,7 +1282,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Получить токены по идентификатору приложения tags: - token @@ -1304,7 +1308,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Отозвать токены tags: - token @@ -1331,7 +1335,7 @@ paths: "500": description: Internal Server Error schema: - $ref: '#/definitions/domain.GrpcError' + $ref: '#/definitions/apierrors.Error' summary: Отозвать токены для приложения tags: - token diff --git a/domain/error.go b/domain/error.go index 04c6a1c..5cf7fc9 100644 --- a/domain/error.go +++ b/domain/error.go @@ -5,11 +5,18 @@ import ( ) const ( - ErrCodeInvalidRequest = 600 - ErrCodeAppGroupNotFound = 601 + ErrCodeInvalidRequest = 600 + ErrCodeApplicationNotFound = 602 ErrCodeApplicationDuplicateName = 603 ErrCodeApplicationDuplicateId = 604 + + ErrCodeAppGroupNotFound = 605 + ErrCodeAppGroupDuplicateName = 606 + + ErrCodeSystemNotFound = 607 + ErrCodeDomainNotFound = 608 + ErrCodeDomainDuplicateName = 609 ) var ( diff --git a/domain/grpc_error.go b/domain/grpc_error.go deleted file mode 100644 index 7bb2cfa..0000000 --- a/domain/grpc_error.go +++ /dev/null @@ -1,7 +0,0 @@ -package domain - -type GrpcError struct { - ErrorMessage string `json:"errorMessage"` - ErrorCode string `json:"errorCode"` - Details []interface{} `json:"details"` -} diff --git a/tests/app_group_test.go b/tests/app_group_test.go index 775f8e2..06fd16e 100644 --- a/tests/app_group_test.go +++ b/tests/app_group_test.go @@ -11,13 +11,12 @@ import ( "github.com/stretchr/testify/suite" "github.com/txix-open/isp-kit/dbx" + "github.com/txix-open/isp-kit/grpc/apierrors" "github.com/txix-open/isp-kit/grpc/client" "github.com/txix-open/isp-kit/test" "github.com/txix-open/isp-kit/test/dbt" "github.com/txix-open/isp-kit/test/fake" "github.com/txix-open/isp-kit/test/grpct" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) func TestApplicationGroupSuite(t *testing.T) { @@ -171,10 +170,9 @@ func (s *AppGroupSuite) TestCreate_AppGroupNameNotUnique() { JsonRequestBody(apiReq). JsonResponseBody(&result). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.AlreadyExists, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeAppGroupDuplicateName, apiError.ErrorCode) } func (s *AppGroupSuite) TestUpdate_HappyPath() { @@ -213,10 +211,9 @@ func (s *AppGroupSuite) TestUpdate_AppGroupNameNotUnique() { err := s.api.Invoke("system/application_group/update"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.AlreadyExists, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeAppGroupDuplicateName, apiError.ErrorCode) } func (s *AppGroupSuite) TestUpdate_AppGroupNotFound() { @@ -228,10 +225,9 @@ func (s *AppGroupSuite) TestUpdate_AppGroupNotFound() { err := s.api.Invoke("system/application_group/update"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.NotFound, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeAppGroupNotFound, apiError.ErrorCode) } func (s *AppGroupSuite) TestDeleteList() { diff --git a/tests/application_test.go b/tests/application_test.go index 0e96a4f..15cc1f7 100644 --- a/tests/application_test.go +++ b/tests/application_test.go @@ -11,13 +11,12 @@ import ( "github.com/stretchr/testify/suite" "github.com/txix-open/isp-kit/dbx" + "github.com/txix-open/isp-kit/grpc/apierrors" "github.com/txix-open/isp-kit/grpc/client" "github.com/txix-open/isp-kit/test" "github.com/txix-open/isp-kit/test/dbt" "github.com/txix-open/isp-kit/test/fake" "github.com/txix-open/isp-kit/test/grpct" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) func TestApplicationSuite(t *testing.T) { @@ -104,10 +103,9 @@ func (s *ApplicationSuite) TestCreate_AppGroupNotFound() { err := s.api.Invoke("system/application/create_application"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.InvalidArgument, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeAppGroupNotFound, apiError.ErrorCode) } func (s *ApplicationSuite) TestCreate_ApplicationNameNotUniqueInAppGroup() { @@ -122,10 +120,9 @@ func (s *ApplicationSuite) TestCreate_ApplicationNameNotUniqueInAppGroup() { err := s.api.Invoke("system/application/create_application"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.AlreadyExists, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeApplicationDuplicateName, apiError.ErrorCode) } func (s *ApplicationSuite) TestCreate_ApplicationIdNotUnique() { @@ -140,10 +137,9 @@ func (s *ApplicationSuite) TestCreate_ApplicationIdNotUnique() { err := s.api.Invoke("system/application/create_application"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.AlreadyExists, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeApplicationDuplicateId, apiError.ErrorCode) } func (s *ApplicationSuite) TestUpdate_HappyPath() { @@ -209,10 +205,9 @@ func (s *ApplicationSuite) TestUpdate_ApplicationNameNotUniqueInAppGroup() { err := s.api.Invoke("system/application/update_application"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.AlreadyExists, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeApplicationDuplicateName, apiError.ErrorCode) } func (s *ApplicationSuite) TestUpdate_AppIdNotUnique() { @@ -227,10 +222,9 @@ func (s *ApplicationSuite) TestUpdate_AppIdNotUnique() { err := s.api.Invoke("system/application/update_application"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.AlreadyExists, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeApplicationDuplicateId, apiError.ErrorCode) } func (s *ApplicationSuite) TestUpdate_AppNotFound() { @@ -244,10 +238,9 @@ func (s *ApplicationSuite) TestUpdate_AppNotFound() { err := s.api.Invoke("system/application/update_application"). JsonRequestBody(apiReq). Do(s.T().Context()) - statusErr, ok := status.FromError(err) - s.Require().True(ok) - s.Require().NotNil(statusErr) - s.Require().Equal(codes.NotFound, statusErr.Code()) + apiError := apierrors.FromError(err) + s.Require().NotNil(apiError) + s.Require().Equal(domain.ErrCodeApplicationNotFound, apiError.ErrorCode) } func (s *ApplicationSuite) createAppGroup() *entity.AppGroup {