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

Commit 8d6332f

Browse files
author
Noah Hanjun Lee
authored
Add a new function to log with the level of an error code. (#193)
* Add the func to log with an error * Replace to log by the function
1 parent 8fcc9bd commit 8d6332f

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

internal/server/api/v1/license/license.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import (
55

66
"github.com/gin-gonic/gin"
77
gb "github.com/gitploy-io/gitploy/internal/server/global"
8+
"go.uber.org/zap"
89
)
910

1011
type (
1112
Licenser struct {
12-
i Interactor
13+
i Interactor
14+
log *zap.Logger
1315
}
1416
)
1517

1618
func NewLicenser(intr Interactor) *Licenser {
1719
return &Licenser{
18-
i: intr,
20+
i: intr,
21+
log: zap.L().Named("license"),
1922
}
2023
}
2124

@@ -24,6 +27,7 @@ func (l *Licenser) GetLicense(c *gin.Context) {
2427

2528
lic, err := l.i.GetLicense(ctx)
2629
if err != nil {
30+
gb.LogWithError(l.log, "It has failed to get the license.", err)
2731
gb.ResponseWithError(c, err)
2832
return
2933
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r *Repo) GetBranch(c *gin.Context) {
4747

4848
b, err := r.i.GetBranch(ctx, u, repo, branch)
4949
if err != nil {
50-
r.log.Error("It has failed to get the branch.", zap.Error(err))
50+
gb.LogWithError(r.log, "It has failed to get the branch.", err)
5151
gb.ResponseWithError(c, err)
5252
return
5353
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *Repo) GetCommit(c *gin.Context) {
5050

5151
commit, err := r.i.GetCommit(ctx, u, repo, sha)
5252
if err != nil {
53-
r.log.Error("It has failed to get the commit.", zap.Error(err))
53+
gb.LogWithError(r.log, "It has failed to get the commit.", err)
5454
gb.ResponseWithError(c, err)
5555
return
5656
}
@@ -73,7 +73,7 @@ func (r *Repo) ListStatuses(c *gin.Context) {
7373

7474
ss, err := r.i.ListCommitStatuses(ctx, u, repo, sha)
7575
if err != nil {
76-
r.log.Error("It has failed to list commit statuses.", zap.Error(err))
76+
gb.LogWithError(r.log, "It has failed to list commit statuses.", err)
7777
gb.ResponseWithError(c, err)
7878
return
7979
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
9090

9191
cf, err := r.i.GetConfig(ctx, u, re)
9292
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
93-
r.log.Error("The configuration file is not found.", zap.Error(err))
93+
gb.LogWithError(r.log, "The configuration file is not found.", err)
9494
// To override the HTTP status 422.
9595
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
9696
return
9797
} else if err != nil {
98-
r.log.Error("It has failed to get the configuration.")
98+
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
9999
gb.ResponseWithError(c, err)
100100
return
101101
}
@@ -122,7 +122,7 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
122122
cf.GetEnv(p.Env),
123123
)
124124
if err != nil {
125-
r.log.Error("It has failed to deploy.", zap.Error(err))
125+
gb.LogWithError(r.log, "It has failed to deploy.", err)
126126
gb.ResponseWithError(c, err)
127127
return
128128
}
@@ -172,12 +172,12 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {
172172

173173
cf, err := r.i.GetConfig(ctx, u, re)
174174
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
175-
r.log.Error("The configuration file is not found.", zap.Error(err))
175+
gb.LogWithError(r.log, "The configuration file is not found.", err)
176176
// To override the HTTP status 422.
177177
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
178178
return
179179
} else if err != nil {
180-
r.log.Error("It has failed to get the configuration.")
180+
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
181181
gb.ResponseWithError(c, err)
182182
return
183183
}
@@ -197,7 +197,7 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {
197197

198198
if p.Status == string(deployment.StatusCreated) && d.Status == deployment.StatusWaiting {
199199
if d, err = r.i.DeployToRemote(ctx, u, re, d, env); err != nil {
200-
r.log.Error("It has failed to deploy to the remote.", zap.Error(err))
200+
gb.LogWithError(r.log, "It has failed to deploy to the remote.", err)
201201
gb.ResponseWithError(c, err)
202202
return
203203
}
@@ -241,12 +241,12 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
241241

242242
cf, err := r.i.GetConfig(ctx, u, re)
243243
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
244-
r.log.Error("The configuration file is not found.", zap.Error(err))
244+
gb.LogWithError(r.log, "The configuration file is not found.", err)
245245
// To override the HTTP status 422.
246246
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
247247
return
248248
} else if err != nil {
249-
r.log.Error("It has failed to get the configuration.")
249+
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
250250
gb.ResponseWithError(c, err)
251251
return
252252
}
@@ -274,7 +274,7 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
274274
cf.GetEnv(d.Env),
275275
)
276276
if err != nil {
277-
r.log.Error("It has failed to deploy.", zap.Error(err))
277+
gb.LogWithError(r.log, "It has failed to deploy.", err)
278278
gb.ResponseWithError(c, err)
279279
return
280280
}
@@ -336,7 +336,7 @@ func (r *Repo) ListDeploymentChanges(c *gin.Context) {
336336
if sha == "" {
337337
sha, err = r.getCommitSha(ctx, u, re, d.Type, d.Ref)
338338
if err != nil {
339-
r.log.Error("It has failed to get the commit SHA.", zap.Error(err))
339+
gb.LogWithError(r.log, "It has failed to get the commit SHA.", err)
340340
gb.ResponseWithError(c, err)
341341
return
342342
}
@@ -391,7 +391,7 @@ func (r *Repo) GetConfig(c *gin.Context) {
391391

392392
config, err := r.i.GetConfig(ctx, u, re)
393393
if err != nil {
394-
r.log.Error("It has failed to get the configuration.", zap.Error(err))
394+
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
395395
gb.ResponseWithError(c, err)
396396
return
397397
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ func (r *Repo) CreateLock(c *gin.Context) {
8181

8282
cfg, err := r.i.GetConfig(ctx, u, re)
8383
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
84-
r.log.Error("The configuration file is not found.", zap.Error(err))
84+
gb.LogWithError(r.log, "The configuration file is not found.", err)
8585
// To override the HTTP status 422.
8686
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
8787
return
8888
} else if err != nil {
89-
r.log.Error("It has failed to get the configuration.")
89+
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
9090
gb.ResponseWithError(c, err)
9191
return
9292
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r *Repo) GetTag(c *gin.Context) {
4747

4848
t, err := r.i.GetTag(ctx, u, repo, tag)
4949
if err != nil {
50-
r.log.Error("It has failed to get the tag.", zap.Error(err))
50+
gb.LogWithError(r.log, "It has failed to get the tag.", err)
5151
gb.ResponseWithError(c, err)
5252
return
5353
}

internal/server/global/log.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package global
2+
3+
import (
4+
"go.uber.org/zap"
5+
6+
"github.com/gitploy-io/gitploy/pkg/e"
7+
)
8+
9+
// LogWithError handles the level of log by the error code.
10+
func LogWithError(logger *zap.Logger, message string, err error) {
11+
ge, ok := err.(*e.Error)
12+
if !ok {
13+
logger.Error(message, zap.Error(err))
14+
return
15+
}
16+
17+
if ge.Code == e.ErrorCodeInternalError {
18+
logger.Error(message, zap.Error(err))
19+
return
20+
}
21+
22+
logger.Warn(message, zap.Error(err))
23+
}

0 commit comments

Comments
 (0)