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

Add a new function to log with the level of an error code. #193

Merged
merged 3 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/server/api/v1/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (

"github.com/gin-gonic/gin"
gb "github.com/gitploy-io/gitploy/internal/server/global"
"go.uber.org/zap"
)

type (
Licenser struct {
i Interactor
i Interactor
log *zap.Logger
}
)

func NewLicenser(intr Interactor) *Licenser {
return &Licenser{
i: intr,
i: intr,
log: zap.L().Named("license"),
}
}

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

lic, err := l.i.GetLicense(ctx)
if err != nil {
gb.LogWithError(l.log, "It has failed to get the license.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/api/v1/repos/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Repo) GetBranch(c *gin.Context) {

b, err := r.i.GetBranch(ctx, u, repo, branch)
if err != nil {
r.log.Error("It has failed to get the branch.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to get the branch.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/api/v1/repos/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *Repo) GetCommit(c *gin.Context) {

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

ss, err := r.i.ListCommitStatuses(ctx, u, repo, sha)
if err != nil {
r.log.Error("It has failed to list commit statuses.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to list commit statuses.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down
22 changes: 11 additions & 11 deletions internal/server/api/v1/repos/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func (r *Repo) CreateDeployment(c *gin.Context) {

cf, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
r.log.Error("The configuration file is not found.", zap.Error(err))
gb.LogWithError(r.log, "The configuration file is not found.", err)
// To override the HTTP status 422.
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Error("It has failed to get the configuration.")
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
gb.ResponseWithError(c, err)
return
}
Expand All @@ -122,7 +122,7 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
cf.GetEnv(p.Env),
)
if err != nil {
r.log.Error("It has failed to deploy.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to deploy.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down Expand Up @@ -172,12 +172,12 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {

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

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

cf, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
r.log.Error("The configuration file is not found.", zap.Error(err))
gb.LogWithError(r.log, "The configuration file is not found.", err)
// To override the HTTP status 422.
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Error("It has failed to get the configuration.")
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
cf.GetEnv(d.Env),
)
if err != nil {
r.log.Error("It has failed to deploy.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to deploy.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func (r *Repo) ListDeploymentChanges(c *gin.Context) {
if sha == "" {
sha, err = r.getCommitSha(ctx, u, re, d.Type, d.Ref)
if err != nil {
r.log.Error("It has failed to get the commit SHA.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to get the commit SHA.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func (r *Repo) GetConfig(c *gin.Context) {

config, err := r.i.GetConfig(ctx, u, re)
if err != nil {
r.log.Error("It has failed to get the configuration.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/api/v1/repos/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func (r *Repo) CreateLock(c *gin.Context) {

cfg, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeConfigNotFound) {
r.log.Error("The configuration file is not found.", zap.Error(err))
gb.LogWithError(r.log, "The configuration file is not found.", err)
// To override the HTTP status 422.
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Error("It has failed to get the configuration.")
gb.LogWithError(r.log, "It has failed to get the configuration.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/api/v1/repos/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Repo) GetTag(c *gin.Context) {

t, err := r.i.GetTag(ctx, u, repo, tag)
if err != nil {
r.log.Error("It has failed to get the tag.", zap.Error(err))
gb.LogWithError(r.log, "It has failed to get the tag.", err)
gb.ResponseWithError(c, err)
return
}
Expand Down
23 changes: 23 additions & 0 deletions internal/server/global/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package global

import (
"go.uber.org/zap"

"github.com/gitploy-io/gitploy/pkg/e"
)

// LogWithError handles the level of log by the error code.
func LogWithError(logger *zap.Logger, message string, err error) {
ge, ok := err.(*e.Error)
if !ok {
logger.Error(message, zap.Error(err))
return
}

if ge.Code == e.ErrorCodeInternalError {
logger.Error(message, zap.Error(err))
return
}

logger.Warn(message, zap.Error(err))
}