Skip to content

Commit

Permalink
refactor: http status code and error messages in api(daytona #934)
Browse files Browse the repository at this point in the history
Signed-off-by: the-johnwick <[email protected]>
  • Loading branch information
the-johnwick committed Sep 9, 2024
1 parent 480cf32 commit 3a433e9
Show file tree
Hide file tree
Showing 36 changed files with 901 additions and 800 deletions.
40 changes: 20 additions & 20 deletions internal/testing/server/workspaces/mocks/git_provider_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ func (m *MockGitProviderService) GetGitProviderForUrl(url string) (gitprovider.G
return args.Get(0).(gitprovider.GitProvider), args.String(1), args.Error(2)
}

func (m *MockGitProviderService) GetGitUser(gitProviderId string) (*gitprovider.GitUser, error) {
func (m *MockGitProviderService) GetGitUser(gitProviderId string) (*gitprovider.GitUser, int, error) {
args := m.Called(gitProviderId)
return args.Get(0).(*gitprovider.GitUser), args.Error(1)
return args.Get(0).(*gitprovider.GitUser), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) GetNamespaces(gitProviderId string) ([]*gitprovider.GitNamespace, error) {
func (m *MockGitProviderService) GetNamespaces(gitProviderId string) ([]*gitprovider.GitNamespace, int, error) {
args := m.Called(gitProviderId)
return args.Get(0).([]*gitprovider.GitNamespace), args.Error(1)
return args.Get(0).([]*gitprovider.GitNamespace), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) GetRepoBranches(gitProviderId string, namespaceId string, repositoryId string) ([]*gitprovider.GitBranch, error) {
func (m *MockGitProviderService) GetRepoBranches(gitProviderId string, namespaceId string, repositoryId string) ([]*gitprovider.GitBranch, int, error) {
args := m.Called(gitProviderId, namespaceId, repositoryId)
return args.Get(0).([]*gitprovider.GitBranch), args.Error(1)
return args.Get(0).([]*gitprovider.GitBranch), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) GetRepoPRs(gitProviderId string, namespaceId string, repositoryId string) ([]*gitprovider.GitPullRequest, error) {
func (m *MockGitProviderService) GetRepoPRs(gitProviderId string, namespaceId string, repositoryId string) ([]*gitprovider.GitPullRequest, int, error) {
args := m.Called(gitProviderId, namespaceId, repositoryId)
return args.Get(0).([]*gitprovider.GitPullRequest), args.Error(1)
return args.Get(0).([]*gitprovider.GitPullRequest), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) GetRepositories(gitProviderId string, namespaceId string) ([]*gitprovider.GitRepository, error) {
func (m *MockGitProviderService) GetRepositories(gitProviderId string, namespaceId string) ([]*gitprovider.GitRepository, int, error) {
args := m.Called(gitProviderId, namespaceId)
return args.Get(0).([]*gitprovider.GitRepository), args.Error(1)
return args.Get(0).([]*gitprovider.GitRepository), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) ListConfigs() ([]*gitprovider.GitProviderConfig, error) {
Expand All @@ -80,26 +80,26 @@ func (m *MockGitProviderService) RemoveGitProvider(gitProviderId string) error {
return args.Error(0)
}

func (m *MockGitProviderService) SetGitProviderConfig(providerConfig *gitprovider.GitProviderConfig) error {
func (m *MockGitProviderService) SetGitProviderConfig(providerConfig *gitprovider.GitProviderConfig) (int, error) {
args := m.Called(providerConfig)
return args.Error(0)
return args.Int(0), args.Error(1)
}

func (m *MockGitProviderService) GetLastCommitSha(repo *gitprovider.GitRepository) (string, error) {
func (m *MockGitProviderService) GetLastCommitSha(repo *gitprovider.GitRepository) (string, int, error) {
args := m.Called(repo)
return args.String(0), args.Error(1)
return args.String(0), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) RegisterPrebuildWebhook(gitProviderId string, repo *gitprovider.GitRepository, endpointUrl string) (string, error) {
func (m *MockGitProviderService) RegisterPrebuildWebhook(gitProviderId string, repo *gitprovider.GitRepository, endpointUrl string) (string, int, error) {
args := m.Called(gitProviderId, repo, endpointUrl)
return args.String(0), args.Error(1)
return args.String(0), args.Int(1), args.Error(2)
}
func (m *MockGitProviderService) GetPrebuildWebhook(gitProviderId string, repo *gitprovider.GitRepository, endpointUrl string) (*string, error) {
func (m *MockGitProviderService) GetPrebuildWebhook(gitProviderId string, repo *gitprovider.GitRepository, endpointUrl string) (*string, int, error) {
args := m.Called(gitProviderId, repo, endpointUrl)
return args.Get(0).(*string), args.Error(1)
return args.Get(0).(*string), args.Int(1), args.Error(2)
}

func (m *MockGitProviderService) UnregisterPrebuildWebhook(gitProviderId string, repo *gitprovider.GitRepository, id string) error {
func (m *MockGitProviderService) UnregisterPrebuildWebhook(gitProviderId string, repo *gitprovider.GitRepository, id string) (int, error) {
args := m.Called(gitProviderId, repo, id)
return args.Error(0)
return args.Int(0), args.Error(1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func NewMockProjectConfigService() *mockProjectConfigService {
return &mockProjectConfigService{}
}

func (m *mockProjectConfigService) Delete(name string, force bool) []error {
func (m *mockProjectConfigService) Delete(name string, force bool) (int, []error) {
args := m.Called(name, force)
return args.Get(0).([]error)
return args.Int(0), args.Get(1).([]error)
}

func (m *mockProjectConfigService) Find(filter *config.ProjectConfigFilter) (*config.ProjectConfig, error) {
Expand All @@ -45,9 +45,9 @@ func (m *mockProjectConfigService) Save(pc *config.ProjectConfig) error {
return args.Error(0)
}

func (m *mockProjectConfigService) SetPrebuild(projectConfigName string, createProjectDto dto.CreatePrebuildDTO) (*dto.PrebuildDTO, error) {
func (m *mockProjectConfigService) SetPrebuild(projectConfigName string, createProjectDto dto.CreatePrebuildDTO) (*dto.PrebuildDTO, int, error) {
args := m.Called(projectConfigName, createProjectDto)
return args.Get(0).(*dto.PrebuildDTO), args.Error(1)
return args.Get(0).(*dto.PrebuildDTO), args.Int(1), args.Error(2)
}

func (m *mockProjectConfigService) FindPrebuild(projectConfigFilter *config.ProjectConfigFilter, prebuildFilter *config.PrebuildFilter) (*dto.PrebuildDTO, error) {
Expand All @@ -60,9 +60,9 @@ func (m *mockProjectConfigService) ListPrebuilds(projectConfigFilter *config.Pro
return args.Get(0).([]*dto.PrebuildDTO), args.Error(1)
}

func (m *mockProjectConfigService) DeletePrebuild(projectConfigName string, id string, force bool) []error {
func (m *mockProjectConfigService) DeletePrebuild(projectConfigName string, id string, force bool) (int, []error) {
args := m.Called(projectConfigName, id, force)
return args.Get(0).([]error)
return args.Int(0), args.Get(1).([]error)
}

func (m *mockProjectConfigService) StartRetentionPoller() error {
Expand All @@ -75,7 +75,7 @@ func (m *mockProjectConfigService) EnforceRetentionPolicy() error {
return args.Error(0)
}

func (m *mockProjectConfigService) ProcessGitEvent(data gitprovider.GitEventData) error {
func (m *mockProjectConfigService) ProcessGitEvent(data gitprovider.GitEventData) (int, error) {
args := m.Called(data)
return args.Error(0)
return args.Int(0), args.Error(1)
}
4 changes: 2 additions & 2 deletions pkg/api/controllers/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func CreateBuild(ctx *gin.Context) {
return
}

repo, err := gitProvider.GetRepositoryContext(gitprovider.GetRepositoryContext{
repo, statusCode, err := gitProvider.GetRepositoryContext(gitprovider.GetRepositoryContext{
Url: projectConfig.RepositoryUrl,
Branch: &createBuildDto.Branch,
})
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get repository: %s", err.Error()))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get repository: %s", err.Error()))
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers/gitprovider/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func GetRepoBranches(ctx *gin.Context) {

server := server.GetInstance(nil)

response, err := server.GitProviderService.GetRepoBranches(gitProviderId, namespaceId, repositoryId)
response, statusCode, err := server.GitProviderService.GetRepoBranches(gitProviderId, namespaceId, repositoryId)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get repo branches: %w", err))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get repo branches: %w", err))
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers/gitprovider/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func GetGitContext(ctx *gin.Context) {
return
}

repo, err := gitProvider.GetRepositoryContext(repositoryContext)
repo, statusCode, err := gitProvider.GetRepositoryContext(repositoryContext)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get repository: %s", err.Error()))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get repository: %s", err.Error()))
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers/gitprovider/gitprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ func SetGitProvider(ctx *gin.Context) {

server := server.GetInstance(nil)

err = server.GitProviderService.SetGitProviderConfig(&gitProviderConfig)
statusCode, err := server.GitProviderService.SetGitProviderConfig(&gitProviderConfig)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("failed to set git provider: %w", err))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to set git provider: %w", err))
return
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/api/controllers/gitprovider/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package gitprovider

import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
Expand All @@ -27,9 +26,9 @@ func GetNamespaces(ctx *gin.Context) {

server := server.GetInstance(nil)

response, err := server.GitProviderService.GetNamespaces(gitProviderId)
response, statusCode, err := server.GitProviderService.GetNamespaces(gitProviderId)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get namespaces: %w", err))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get namespaces: %w", err))
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers/gitprovider/pull_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func GetRepoPRs(ctx *gin.Context) {

server := server.GetInstance(nil)

response, err := server.GitProviderService.GetRepoPRs(gitProviderId, namespaceId, repositoryId)
response, statusCode, err := server.GitProviderService.GetRepoPRs(gitProviderId, namespaceId, repositoryId)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get repository pull requests: %w", err))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get repository pull requests: %w", err))
return
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/api/controllers/gitprovider/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package gitprovider

import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
Expand All @@ -29,9 +28,9 @@ func GetRepositories(ctx *gin.Context) {

server := server.GetInstance(nil)

response, err := server.GitProviderService.GetRepositories(gitProviderId, namespaceId)
response, statusCode, err := server.GitProviderService.GetRepositories(gitProviderId, namespaceId)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get repositories for url: %w", err))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get repositories for url: %w", err))
return
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/api/controllers/gitprovider/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package gitprovider

import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
Expand All @@ -27,9 +26,9 @@ func GetGitUser(ctx *gin.Context) {

server := server.GetInstance(nil)

response, err := server.GitProviderService.GetGitUser(gitProviderId)
response, statusCode, err := server.GitProviderService.GetGitUser(gitProviderId)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get git user: %w", err))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to get git user: %w", err))
return
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/api/controllers/projectconfig/prebuild/prebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func SetPrebuild(ctx *gin.Context) {
}

server := server.GetInstance(nil)
prebuild, err := server.ProjectConfigService.SetPrebuild(configName, dto)
prebuild, statusCode, err := server.ProjectConfigService.SetPrebuild(configName, dto)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to set prebuild: %s", err.Error()))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to set prebuild: %s", err.Error()))
return
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func DeletePrebuild(ctx *gin.Context) {
}

server := server.GetInstance(nil)
errs := server.ProjectConfigService.DeletePrebuild(configName, prebuildId, force)
statusCode, errs := server.ProjectConfigService.DeletePrebuild(configName, prebuildId, force)
if len(errs) > 0 {
if config.IsPrebuildNotFound(errs[0]) {
ctx.AbortWithError(http.StatusNotFound, errors.New("prebuild not found"))
Expand All @@ -173,7 +173,7 @@ func DeletePrebuild(ctx *gin.Context) {
for _, err := range errs {
_ = ctx.Error(err)
}
ctx.AbortWithStatus(http.StatusInternalServerError)
ctx.AbortWithStatus(statusCode)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ func ProcessGitEvent(ctx *gin.Context) {
return
}

gitEventData, err := gitProvider.ParseEventData(ctx.Request)
gitEventData, statusCode, err := gitProvider.ParseEventData(ctx.Request)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to parse event data: %s", err.Error()))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to parse event data: %s", err.Error()))
return
}

if gitEventData == nil {
return
}

err = server.ProjectConfigService.ProcessGitEvent(*gitEventData)
statusCode, err = server.ProjectConfigService.ProcessGitEvent(*gitEventData)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to process git event: %s", err.Error()))
ctx.AbortWithError(statusCode, fmt.Errorf("failed to process git event: %s", err.Error()))
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers/projectconfig/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func DeleteProjectConfig(ctx *gin.Context) {
return
}

errs := server.ProjectConfigService.Delete(projectConfig.Name, force)
statusCode, errs := server.ProjectConfigService.Delete(projectConfig.Name, force)
if len(errs) > 0 {
if config.IsProjectConfigNotFound(errs[0]) {
ctx.AbortWithError(http.StatusNotFound, errors.New("project config not found"))
Expand All @@ -210,7 +210,7 @@ func DeleteProjectConfig(ctx *gin.Context) {
for _, err := range errs {
_ = ctx.Error(err)
}
ctx.AbortWithStatus(http.StatusInternalServerError)
ctx.AbortWithStatus(statusCode)
return
}

Expand Down
Loading

0 comments on commit 3a433e9

Please sign in to comment.