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

Commit 8fcc9bd

Browse files
author
Noah Hanjun Lee
authored
Remove errors of 'vo' package (#192)
* Remove errors of vo * Fix the schema of error
1 parent e929347 commit 8fcc9bd

File tree

12 files changed

+77
-149
lines changed

12 files changed

+77
-149
lines changed

internal/pkg/github/repos.go

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
graphql "github.com/shurcooL/githubv4"
99

1010
"github.com/gitploy-io/gitploy/ent"
11+
"github.com/gitploy-io/gitploy/pkg/e"
1112
"github.com/gitploy-io/gitploy/vo"
1213
)
1314

@@ -72,12 +73,15 @@ func (g *Github) GetCommit(ctx context.Context, u *ent.User, r *ent.Repo, sha st
7273
GetCommit(ctx, r.Namespace, r.Name, sha)
7374
// Github returns Unprocessable entity if the commit is not found.
7475
if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusUnprocessableEntity {
75-
return nil, &vo.RefNotFoundError{
76-
Ref: sha,
77-
}
78-
}
79-
if err != nil {
80-
return nil, err
76+
return nil, e.NewError(
77+
e.ErrorCodeRefNotFound,
78+
err,
79+
)
80+
} else if err != nil {
81+
return nil, e.NewError(
82+
e.ErrorCodeInternalError,
83+
err,
84+
)
8185
}
8286

8387
return mapGithubCommitToCommit(cm), nil
@@ -93,7 +97,10 @@ func (g *Github) ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Rep
9397
PerPage: 100,
9498
})
9599
if err != nil {
96-
return nil, err
100+
return nil, e.NewError(
101+
e.ErrorCodeInternalError,
102+
err,
103+
)
97104
}
98105

99106
for _, rs := range cs.Statuses {
@@ -108,12 +115,15 @@ func (g *Github) ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Rep
108115
})
109116
// check-runs secures the commit is exist.
110117
if res.StatusCode == http.StatusUnprocessableEntity {
111-
return nil, &vo.RefNotFoundError{
112-
Ref: sha,
113-
}
114-
}
115-
if err != nil {
116-
return nil, err
118+
return nil, e.NewError(
119+
e.ErrorCodeRefNotFound,
120+
err,
121+
)
122+
} else if err != nil {
123+
return nil, e.NewError(
124+
e.ErrorCodeInternalError,
125+
err,
126+
)
117127
}
118128

119129
for _, c := range result.CheckRuns {
@@ -133,7 +143,10 @@ func (g *Github) ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, pag
133143
},
134144
})
135145
if err != nil {
136-
return nil, err
146+
return nil, e.NewError(
147+
e.ErrorCodeInternalError,
148+
err,
149+
)
137150
}
138151

139152
branches := []*vo.Branch{}
@@ -149,12 +162,15 @@ func (g *Github) GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch
149162
Repositories.
150163
GetBranch(ctx, r.Namespace, r.Name, branch)
151164
if res.StatusCode == http.StatusNotFound {
152-
return nil, &vo.RefNotFoundError{
153-
Ref: branch,
154-
}
155-
}
156-
if err != nil {
157-
return nil, err
165+
return nil, e.NewError(
166+
e.ErrorCodeRefNotFound,
167+
err,
168+
)
169+
} else if err != nil {
170+
return nil, e.NewError(
171+
e.ErrorCodeInternalError,
172+
err,
173+
)
158174
}
159175

160176
return mapGithubBranchToBranch(b), nil
@@ -226,13 +242,17 @@ func (g *Github) GetTag(ctx context.Context, u *ent.User, r *ent.Repo, tag strin
226242
"tag": graphql.String(tag),
227243
}
228244
if err := client.Query(ctx, &q, v); err != nil {
229-
return nil, err
245+
return nil, e.NewError(
246+
e.ErrorCodeInternalError,
247+
err,
248+
)
230249
}
231250

232251
if q.Repository.Refs.TotalCount == 0 {
233-
return nil, &vo.RefNotFoundError{
234-
Ref: tag,
235-
}
252+
return nil, e.NewError(
253+
e.ErrorCodeRefNotFound,
254+
nil,
255+
)
236256
}
237257

238258
n := q.Repository.Refs.Nodes[0]

internal/server/api/v1/repos/branch.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/gitploy-io/gitploy/ent"
1010
gb "github.com/gitploy-io/gitploy/internal/server/global"
11-
"github.com/gitploy-io/gitploy/vo"
1211
)
1312

1413
func (r *Repo) ListBranches(c *gin.Context) {
@@ -47,13 +46,9 @@ func (r *Repo) GetBranch(c *gin.Context) {
4746
repo := rv.(*ent.Repo)
4847

4948
b, err := r.i.GetBranch(ctx, u, repo, branch)
50-
if vo.IsRefNotFoundError(err) {
51-
r.log.Warn("The branch is not found.", zap.String("repo", repo.Name), zap.String("branch", branch), zap.Error(err))
52-
gb.ErrorResponse(c, http.StatusNotFound, "The branch is not found.")
53-
return
54-
} else if err != nil {
55-
r.log.Error("failed to get the branch.", zap.String("repo", repo.Name), zap.String("branch", branch), zap.Error(err))
56-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the branch.")
49+
if err != nil {
50+
r.log.Error("It has failed to get the branch.", zap.Error(err))
51+
gb.ResponseWithError(c, err)
5752
return
5853
}
5954

internal/server/api/v1/repos/commit.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,9 @@ func (r *Repo) GetCommit(c *gin.Context) {
4949
repo := rv.(*ent.Repo)
5050

5151
commit, err := r.i.GetCommit(ctx, u, repo, sha)
52-
if vo.IsRefNotFoundError(err) {
53-
r.log.Warn("The commit is not found.", zap.String("repo", repo.Name), zap.String("sha", sha), zap.Error(err))
54-
gb.ErrorResponse(c, http.StatusNotFound, "The commit is not found.")
55-
return
56-
} else if err != nil {
57-
r.log.Error("failed to get the commit.", zap.String("repo", repo.Name), zap.String("sha", sha), zap.Error(err))
58-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the commit.")
52+
if err != nil {
53+
r.log.Error("It has failed to get the commit.", zap.Error(err))
54+
gb.ResponseWithError(c, err)
5955
return
6056
}
6157

@@ -76,13 +72,9 @@ func (r *Repo) ListStatuses(c *gin.Context) {
7672
repo := rv.(*ent.Repo)
7773

7874
ss, err := r.i.ListCommitStatuses(ctx, u, repo, sha)
79-
if vo.IsRefNotFoundError(err) {
80-
r.log.Warn("The commit is not found.", zap.String("repo", repo.Name), zap.String("sha", sha), zap.Error(err))
81-
gb.ErrorResponse(c, http.StatusNotFound, "The commit is not found.")
82-
return
83-
} else if err != nil {
84-
r.log.Error("failed to get the commit status.", zap.String("repo", repo.Name), zap.String("sha", sha), zap.Error(err))
85-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the commit.")
75+
if err != nil {
76+
r.log.Error("It has failed to list commit statuses.", zap.Error(err))
77+
gb.ResponseWithError(c, err)
8678
return
8779
}
8880

internal/server/api/v1/repos/deployment.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,9 @@ func (r *Repo) ListDeploymentChanges(c *gin.Context) {
335335
sha := d.Sha
336336
if sha == "" {
337337
sha, err = r.getCommitSha(ctx, u, re, d.Type, d.Ref)
338-
if vo.IsRefNotFoundError(err) {
339-
r.log.Warn("The REF is not found.", zap.Error(err))
340-
gb.Response(c, http.StatusOK, nil)
341-
return
342-
} else if err != nil {
343-
r.log.Error("It has failed to get the SHA of deployment.", zap.Error(err))
344-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the SHA of deployment.")
338+
if err != nil {
339+
r.log.Error("It has failed to get the commit SHA.", zap.Error(err))
340+
gb.ResponseWithError(c, err)
345341
return
346342
}
347343
}

internal/server/api/v1/repos/lock.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/gitploy-io/gitploy/ent"
2121
"github.com/gitploy-io/gitploy/internal/server/global"
2222
gb "github.com/gitploy-io/gitploy/internal/server/global"
23-
"github.com/gitploy-io/gitploy/vo"
23+
"github.com/gitploy-io/gitploy/pkg/e"
2424
)
2525

2626
type (
@@ -79,15 +79,15 @@ func (r *Repo) CreateLock(c *gin.Context) {
7979
vu, _ := c.Get(global.KeyUser)
8080
u := vu.(*ent.User)
8181

82-
// Validate the payload, it check whether the env exist or not in deploy.yml.
8382
cfg, err := r.i.GetConfig(ctx, u, re)
84-
if vo.IsConfigNotFoundError(err) || vo.IsConfigParseError(err) {
85-
r.log.Warn("The config is invalid.", zap.Error(err))
86-
gb.ErrorResponse(c, http.StatusUnprocessableEntity, "The config is invalid.")
83+
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
84+
r.log.Error("The configuration file is not found.", zap.Error(err))
85+
// To override the HTTP status 422.
86+
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
8787
return
8888
} else if err != nil {
89-
r.log.Error("It has failed to get the config file.", zap.Error(err))
90-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the config file.")
89+
r.log.Error("It has failed to get the configuration.")
90+
gb.ResponseWithError(c, err)
9191
return
9292
}
9393

internal/server/api/v1/repos/tag.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/gitploy-io/gitploy/ent"
1010
gb "github.com/gitploy-io/gitploy/internal/server/global"
11-
"github.com/gitploy-io/gitploy/vo"
1211
)
1312

1413
func (r *Repo) ListTags(c *gin.Context) {
@@ -47,13 +46,9 @@ func (r *Repo) GetTag(c *gin.Context) {
4746
repo := rv.(*ent.Repo)
4847

4948
t, err := r.i.GetTag(ctx, u, repo, tag)
50-
if vo.IsRefNotFoundError(err) {
51-
r.log.Warn("the tag is not found.", zap.String("repo", repo.Name), zap.String("tag", tag), zap.Error(err))
52-
gb.ErrorResponse(c, http.StatusNotFound, "the tag is not found.")
53-
return
54-
} else if err != nil {
55-
r.log.Error("failed to get the tag.", zap.String("repo", repo.Name), zap.String("tag", tag), zap.Error(err))
56-
gb.ErrorResponse(c, http.StatusInternalServerError, "It has failed to get the tag.")
49+
if err != nil {
50+
r.log.Error("It has failed to get the tag.", zap.Error(err))
51+
gb.ResponseWithError(c, err)
5752
return
5853
}
5954

internal/server/slack/deploy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/gitploy-io/gitploy/ent/callback"
1616
"github.com/gitploy-io/gitploy/ent/deployment"
1717
"github.com/gitploy-io/gitploy/ent/event"
18+
"github.com/gitploy-io/gitploy/pkg/e"
1819
"github.com/gitploy-io/gitploy/vo"
1920
)
2021

@@ -261,7 +262,7 @@ func (s *Slack) interactDeploy(c *gin.Context) {
261262

262263
// Validate the entity is processible.
263264
_, err := s.getCommitSha(ctx, cu.Edges.User, cb.Edges.Repo, sm.Type, sm.Ref)
264-
if vo.IsRefNotFoundError(err) {
265+
if e.HasErrorCode(err, e.ErrorCodeRefNotFound) {
265266
c.JSON(http.StatusOK, buildErrorsPayload(map[string]string{
266267
blockRef: "The reference is not found.",
267268
}))

internal/server/slack/lock.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ func (s *Slack) handleLockCmd(c *gin.Context) {
5757

5858
// Build the modal with unlocked envs.
5959
config, err := s.i.GetConfig(ctx, cu.Edges.User, r)
60-
if vo.IsConfigNotFoundError(err) || vo.IsConfigParseError(err) {
61-
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, "The config is invalid.")
60+
if err != nil {
61+
postMessageWithError(cu, err)
6262
c.Status(http.StatusOK)
6363
return
64-
} else if err != nil {
65-
s.log.Error("It has failed to get the config file.", zap.Error(err))
66-
c.Status(http.StatusInternalServerError)
67-
return
6864
}
6965

7066
locks, err := s.i.ListLocksOfRepo(ctx, r)

openapi/v1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,8 @@ components:
19811981
Error:
19821982
type: object
19831983
properties:
1984+
code:
1985+
type: string
19841986
message:
19851987
type: string
19861988
required:

pkg/e/code.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const (
2525
// ErrorCodeLicenseDecode is that the license.
2626
ErrorCodeLicenseDecode ErrorCode = "license_decode"
2727

28+
// ErrorCodeRefNotFound is that the ref is not found.
29+
ErrorCodeRefNotFound ErrorCode = "ref_not_found"
30+
2831
ErrorCodeInternalError ErrorCode = "internal_error"
2932
)
3033

0 commit comments

Comments
 (0)