Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 47e89f1

Browse files
author
Noah Hanjun Lee
authored
Migrate every types of errors into the Error type of Gitploy (#198)
1 parent e3641e2 commit 47e89f1

39 files changed

+904
-499
lines changed

cmd/server/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func setGlobalLogger(debug bool) {
6161
config.Encoding = "json"
6262
} else {
6363
config = zap.NewProductionConfig()
64-
config.DisableStacktrace = true
6564
}
6665

6766
logger, _ := config.Build()

internal/pkg/github/deployment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ func (g *Github) GetConfig(ctx context.Context, u *ent.User, r *ent.Repo) (*vo.C
7575
Repositories.
7676
GetContents(ctx, r.Namespace, r.Name, r.ConfigPath, &github.RepositoryContentGetOptions{})
7777
if res.StatusCode == http.StatusNotFound {
78-
return nil, e.NewError(
79-
e.ErrorCodeConfigNotFound,
78+
return nil, e.NewErrorWithMessage(
79+
e.ErrorCodeNotFound,
80+
"The configuration file is not found.",
8081
err,
8182
)
8283
} else if err != nil {

internal/pkg/github/repos.go

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,9 @@ func (g *Github) GetCommit(ctx context.Context, u *ent.User, r *ent.Repo, sha st
7373
GetCommit(ctx, r.Namespace, r.Name, sha)
7474
// Github returns Unprocessable entity if the commit is not found.
7575
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusUnprocessableEntity {
76-
return nil, e.NewError(
77-
e.ErrorCodeRefNotFound,
78-
err,
79-
)
76+
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The commit is not found.", err)
8077
} else if err != nil {
81-
return nil, e.NewError(
82-
e.ErrorCodeInternalError,
83-
err,
84-
)
78+
return nil, e.NewError(e.ErrorCodeInternalError, err)
8579
}
8680

8781
return mapGithubCommitToCommit(cm), nil
@@ -97,10 +91,7 @@ func (g *Github) ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Rep
9791
PerPage: 100,
9892
})
9993
if err != nil {
100-
return nil, e.NewError(
101-
e.ErrorCodeInternalError,
102-
err,
103-
)
94+
return nil, err
10495
}
10596

10697
for _, rs := range cs.Statuses {
@@ -115,10 +106,7 @@ func (g *Github) ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Rep
115106
})
116107
// check-runs secures the commit is exist.
117108
if res.StatusCode == http.StatusUnprocessableEntity {
118-
return nil, e.NewError(
119-
e.ErrorCodeRefNotFound,
120-
err,
121-
)
109+
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The commit is not found.", err)
122110
} else if err != nil {
123111
return nil, e.NewError(
124112
e.ErrorCodeInternalError,
@@ -143,10 +131,7 @@ func (g *Github) ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, pag
143131
},
144132
})
145133
if err != nil {
146-
return nil, e.NewError(
147-
e.ErrorCodeInternalError,
148-
err,
149-
)
134+
return nil, e.NewError(e.ErrorCodeInternalError, err)
150135
}
151136

152137
branches := []*vo.Branch{}
@@ -162,15 +147,9 @@ func (g *Github) GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch
162147
Repositories.
163148
GetBranch(ctx, r.Namespace, r.Name, branch)
164149
if res.StatusCode == http.StatusNotFound {
165-
return nil, e.NewError(
166-
e.ErrorCodeRefNotFound,
167-
err,
168-
)
150+
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The branch is not found.", err)
169151
} else if err != nil {
170-
return nil, e.NewError(
171-
e.ErrorCodeInternalError,
172-
err,
173-
)
152+
return nil, e.NewError(e.ErrorCodeInternalError, err)
174153
}
175154

176155
return mapGithubBranchToBranch(b), nil
@@ -249,10 +228,7 @@ func (g *Github) GetTag(ctx context.Context, u *ent.User, r *ent.Repo, tag strin
249228
}
250229

251230
if q.Repository.Refs.TotalCount == 0 {
252-
return nil, e.NewError(
253-
e.ErrorCodeRefNotFound,
254-
nil,
255-
)
231+
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The tag is not found.", nil)
256232
}
257233

258234
n := q.Repository.Refs.Nodes[0]
@@ -278,7 +254,7 @@ func (g *Github) CreateWebhook(ctx context.Context, u *ent.User, r *ent.Repo, c
278254
Active: github.Bool(true),
279255
})
280256
if err != nil {
281-
return -1, err
257+
return -1, e.NewErrorWithMessage(e.ErrorCodeInternalError, "It has failed to create a webhook.", err)
282258
}
283259

284260
return *h.ID, nil

internal/pkg/github/sync.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"context"
55

66
"github.com/gitploy-io/gitploy/ent"
7+
"github.com/gitploy-io/gitploy/pkg/e"
78
"github.com/gitploy-io/gitploy/vo"
89
"github.com/google/go-github/v32/github"
910
)
1011

1112
func (g *Github) ListRemoteRepos(ctx context.Context, u *ent.User) ([]*vo.RemoteRepo, error) {
1213
grs, err := g.listRemoteRepos(ctx, u)
1314
if err != nil {
14-
return nil, err
15+
return nil, e.NewError(e.ErrorCodeInternalError, err)
1516
}
1617

1718
remotes := make([]*vo.RemoteRepo, 0)

internal/pkg/store/approval.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package store
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"entgo.io/ent/dialect/sql"
89
"github.com/gitploy-io/gitploy/ent"
910
"github.com/gitploy-io/gitploy/ent/approval"
1011
"github.com/gitploy-io/gitploy/ent/predicate"
12+
"github.com/gitploy-io/gitploy/pkg/e"
1113
)
1214

1315
func (s *Store) SearchApprovals(ctx context.Context, u *ent.User, ss []approval.Status, from time.Time, to time.Time, page, perPage int) ([]*ent.Approval, error) {
@@ -59,7 +61,7 @@ func (s *Store) ListApprovals(ctx context.Context, d *ent.Deployment) ([]*ent.Ap
5961
}
6062

6163
func (s *Store) FindApprovalByID(ctx context.Context, id int) (*ent.Approval, error) {
62-
return s.c.Approval.
64+
ap, err := s.c.Approval.
6365
Query().
6466
Where(
6567
approval.IDEQ(id),
@@ -71,10 +73,17 @@ func (s *Store) FindApprovalByID(ctx context.Context, id int) (*ent.Approval, er
7173
WithUser()
7274
}).
7375
First(ctx)
76+
if ent.IsNotFound(err) {
77+
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The approval is not found.", err)
78+
} else if err != nil {
79+
return nil, e.NewError(e.ErrorCodeInternalError, err)
80+
}
81+
82+
return ap, nil
7483
}
7584

7685
func (s *Store) FindApprovalOfUser(ctx context.Context, d *ent.Deployment, u *ent.User) (*ent.Approval, error) {
77-
return s.c.Approval.
86+
ap, err := s.c.Approval.
7887
Query().
7988
Where(
8089
approval.And(
@@ -89,21 +98,48 @@ func (s *Store) FindApprovalOfUser(ctx context.Context, d *ent.Deployment, u *en
8998
WithUser()
9099
}).
91100
First(ctx)
101+
if ent.IsNotFound(err) {
102+
return nil, e.NewErrorWithMessage(e.ErrorCodeNotFound, "The user's approval is not found.", err)
103+
} else if err != nil {
104+
return nil, e.NewError(e.ErrorCodeInternalError, err)
105+
}
106+
107+
return ap, nil
92108
}
93109

94110
func (s *Store) CreateApproval(ctx context.Context, a *ent.Approval) (*ent.Approval, error) {
95-
return s.c.Approval.
111+
ap, err := s.c.Approval.
96112
Create().
97113
SetUserID(a.UserID).
98114
SetDeploymentID(a.DeploymentID).
99115
Save(ctx)
116+
if ent.IsValidationError(err) {
117+
return nil, e.NewErrorWithMessage(
118+
e.ErrorCodeUnprocessableEntity,
119+
fmt.Sprintf("Failed to create a approval. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
120+
err)
121+
} else if err != nil {
122+
return nil, e.NewError(e.ErrorCodeInternalError, err)
123+
}
124+
125+
return ap, nil
100126
}
101127

102128
func (s *Store) UpdateApproval(ctx context.Context, a *ent.Approval) (*ent.Approval, error) {
103-
return s.c.Approval.
129+
ap, err := s.c.Approval.
104130
UpdateOne(a).
105131
SetStatus(a.Status).
106132
Save(ctx)
133+
if ent.IsValidationError(err) {
134+
return nil, e.NewErrorWithMessage(
135+
e.ErrorCodeUnprocessableEntity,
136+
fmt.Sprintf("Failed to update the approval. The value of \"%s\" field is invalid.", err.(*ent.ValidationError).Name),
137+
err)
138+
} else if err != nil {
139+
return nil, e.NewError(e.ErrorCodeInternalError, err)
140+
}
141+
142+
return ap, nil
107143
}
108144

109145
func (s *Store) DeleteApproval(ctx context.Context, a *ent.Approval) error {

internal/pkg/store/approval_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/gitploy-io/gitploy/ent/approval"
1010
"github.com/gitploy-io/gitploy/ent/enttest"
1111
"github.com/gitploy-io/gitploy/ent/migrate"
12+
"github.com/gitploy-io/gitploy/pkg/e"
1213
)
1314

1415
func TestStore_SearchApprovals(t *testing.T) {
@@ -79,3 +80,61 @@ func TestStore_SearchApprovals(t *testing.T) {
7980
})
8081

8182
}
83+
84+
func TestStore_UpdateApproval(t *testing.T) {
85+
t.Run("Return unprocessable_entity error when the status is invalid.", func(t *testing.T) {
86+
ctx := context.Background()
87+
88+
client := enttest.Open(t,
89+
"sqlite3",
90+
"file:ent?mode=memory&cache=shared&_fk=1",
91+
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
92+
)
93+
defer client.Close()
94+
95+
a := client.Approval.
96+
Create().
97+
SetUserID(1).
98+
SetDeploymentID(1).
99+
SaveX(ctx)
100+
101+
store := NewStore(client)
102+
103+
t.Log("Update the approval with the invalid value.")
104+
a.Status = approval.Status("VALUE")
105+
_, err := store.UpdateApproval(ctx, a)
106+
if !e.HasErrorCode(err, e.ErrorCodeUnprocessableEntity) {
107+
t.Fatalf("UpdateApproval return error = %v, wanted ErrorCodeUnprocessableEntity", err)
108+
}
109+
})
110+
111+
t.Run("Return the updated approval.", func(t *testing.T) {
112+
ctx := context.Background()
113+
114+
client := enttest.Open(t,
115+
"sqlite3",
116+
"file:ent?mode=memory&cache=shared&_fk=1",
117+
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
118+
)
119+
defer client.Close()
120+
121+
a := client.Approval.
122+
Create().
123+
SetUserID(1).
124+
SetDeploymentID(1).
125+
SaveX(ctx)
126+
127+
store := NewStore(client)
128+
129+
t.Log("Update the approval ")
130+
a.Status = approval.StatusApproved
131+
a, err := store.UpdateApproval(ctx, a)
132+
if err != nil {
133+
t.Fatalf("UpdateApproval returns an error: %v", err)
134+
}
135+
136+
if a.Status != approval.StatusApproved {
137+
t.Fatalf("UpdateApproval status = %v, wanted %v", a.Status, approval.StatusApproved)
138+
}
139+
})
140+
}

0 commit comments

Comments
 (0)