Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log non actionable errors on WARN level #353

Merged
merged 1 commit into from
Jan 4, 2023
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
16 changes: 8 additions & 8 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (p *Plugin) withRecovery(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if x := recover(); x != nil {
p.API.LogError("Recovered from a panic",
p.API.LogWarn("Recovered from a panic",
"url", r.URL.String(),
"error", x,
"stack", string(debug.Stack()))
Expand Down Expand Up @@ -171,7 +171,7 @@ func (p *Plugin) checkAuth(handler http.HandlerFunc, responseType ResponseType)
case ResponseTypePlain:
http.Error(w, "Not authorized", http.StatusUnauthorized)
default:
p.API.LogError("Unknown ResponseType detected")
p.API.LogDebug("Unknown ResponseType detected")
}
return
}
Expand Down Expand Up @@ -204,18 +204,18 @@ func (p *Plugin) writeAPIError(w http.ResponseWriter, err *APIErrorResponse) {
b, _ := json.Marshal(err)
w.WriteHeader(err.StatusCode)
if _, err := w.Write(b); err != nil {
p.API.LogError("can't write api error http response", "err", err.Error())
p.API.LogWarn("can't write api error http response", "err", err.Error())
}
}

func (p *Plugin) writeAPIResponse(w http.ResponseWriter, resp interface{}) {
b, jsonErr := json.Marshal(resp)
if jsonErr != nil {
p.API.LogError("Error encoding JSON response", "err", jsonErr.Error())
p.API.LogWarn("Error encoding JSON response", "err", jsonErr.Error())
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Encountered an unexpected error. Please try again.", StatusCode: http.StatusInternalServerError})
}
if _, err := w.Write(b); err != nil {
p.API.LogError("can't write response user to http", "err", err.Error())
p.API.LogWarn("can't write response user to http", "err", err.Error())
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Encountered an unexpected error. Please try again.", StatusCode: http.StatusInternalServerError})
}
}
Expand Down Expand Up @@ -645,7 +645,7 @@ func (p *Plugin) getChannelSubscriptions(c *UserContext, w http.ResponseWriter,
config := p.getConfiguration()
subscriptions, err := p.GetSubscriptionsByChannel(channelID)
if err != nil {
p.API.LogError("unable to get subscriptions by channel", "err", err.Error())
p.API.LogWarn("unable to get subscriptions by channel", "err", err.Error())
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to get subscriptions by channel.", StatusCode: http.StatusInternalServerError})
return
}
Expand All @@ -654,9 +654,9 @@ func (p *Plugin) getChannelSubscriptions(c *UserContext, w http.ResponseWriter,

b, err := json.Marshal(resp)
if err != nil {
p.API.LogError("failed to marshal channel subscriptions response", "err", err.Error())
p.API.LogWarn("failed to marshal channel subscriptions response", "err", err.Error())
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Encountered an unexpected error. Please try again.", StatusCode: http.StatusInternalServerError})
} else if _, err := w.Write(b); err != nil {
p.API.LogError("can't write api error http response", "err", err.Error())
p.API.LogWarn("can't write api error http response", "err", err.Error())
}
}
2 changes: 0 additions & 2 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func (p *Plugin) sendMessageToCluster(id string, v interface{}) {
}

func (p *Plugin) HandleClusterEvent(ev model.PluginClusterEvent) {
p.API.LogError("received cluster event", "id", ev.Id)

switch ev.Id {
case oauthCompleteEventID:
var event OAuthCompleteEvent
Expand Down
16 changes: 8 additions & 8 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
case "todo":
_, text, err := p.GetToDo(ctx, info)
if err != nil {
p.API.LogError("can't get todo in command", "err", err.Error())
p.API.LogWarn("can't get todo in command", "err", err.Error())
return p.getCommandResponse(args, "Encountered an error getting your to do items."), nil
}
return p.getCommandResponse(args, text), nil
Expand Down Expand Up @@ -249,15 +249,15 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
case SettingNotifications:
if value {
if err := p.storeGitlabToUserIDMapping(info.GitlabUsername, info.UserID); err != nil {
p.API.LogError("can't store GitLab to user id mapping", "err", err.Error())
p.API.LogWarn("can't store GitLab to user id mapping", "err", err.Error())
return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil
}
if err := p.storeGitlabIDToUserIDMapping(info.GitlabUsername, info.GitlabUserID); err != nil {
p.API.LogError("can't store GitLab to GitLab id mapping", "err", err.Error())
p.API.LogWarn("can't store GitLab to GitLab id mapping", "err", err.Error())
return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil
}
} else if err := p.deleteGitlabToUserIDMapping(info.GitlabUsername); err != nil {
p.API.LogError("can't delete GitLab username in kvstore", "err", err.Error())
p.API.LogWarn("can't delete GitLab username in kvstore", "err", err.Error())
return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil
}
info.Settings.Notifications = value
Expand All @@ -268,7 +268,7 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
}

if err := p.storeGitlabUserInfo(info); err != nil {
p.API.LogError("can't store user info after update by command", "err", err.Error())
p.API.LogWarn("can't store user info after update by command", "err", err.Error())
return p.getCommandResponse(args, "Unknown error please retry or ask to an administrator to look at logs"), nil
}

Expand Down Expand Up @@ -484,7 +484,7 @@ func (p *Plugin) subscriptionDelete(info *gitlab.UserInfo, config *configuration
normalizedPath := normalizePath(fullPath, config.GitlabURL)
deleted, err := p.Unsubscribe(channelID, normalizedPath)
if err != nil {
p.API.LogError("can't unsubscribe channel in command", "err", err.Error())
p.API.LogWarn("can't unsubscribe channel in command", "err", err.Error())
return "Encountered an error trying to unsubscribe. Please try again.", nil
}

Expand Down Expand Up @@ -528,15 +528,15 @@ func (p *Plugin) subscriptionsAddCommand(ctx context.Context, info *gitlab.UserI
} else if errors.Is(err, gitlab.ErrPrivateResource) {
return "Requested resource is private."
}
p.API.LogError(
p.API.LogWarn(
"unable to resolve subscription namespace and project name",
"err", err.Error(),
)
return err.Error()
}

if subscribeErr := p.Subscribe(info, namespace, project, channelID, features); subscribeErr != nil {
p.API.LogError(
p.API.LogWarn(
"failed to subscribe",
"namespace", namespace,
"project", project,
Expand Down
24 changes: 12 additions & 12 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (p *Plugin) getGitlabUserInfoByMattermostID(userID string) (*gitlab.UserInf

unencryptedToken, err := decrypt([]byte(config.EncryptionKey), userInfo.Token.AccessToken)
if err != nil {
p.API.LogError("can't decrypt token", "err", err.Error())
p.API.LogWarn("can't decrypt token", "err", err.Error())
return nil, &APIErrorResponse{ID: "", Message: "Unable to decrypt access token.", StatusCode: http.StatusInternalServerError}
}

Expand Down Expand Up @@ -321,43 +321,43 @@ func (p *Plugin) deleteGitlabIDToUserIDMapping(gitlabID int) error {
func (p *Plugin) getGitlabToUserIDMapping(gitlabUsername string) string {
userID, err := p.API.KVGet(gitlabUsername + GitlabUsernameKey)
if err != nil {
p.API.LogError("can't get userId from store with username", "err", err.DetailedError, "username", gitlabUsername)
p.API.LogWarn("can't get userId from store with username", "err", err.DetailedError, "username", gitlabUsername)
}
return string(userID)
}

func (p *Plugin) getGitlabIDToUsernameMapping(gitlabUserID string) string {
gitlabUsername, err := p.API.KVGet(gitlabUserID + GitlabIDUsernameKey)
if err != nil {
p.API.LogError("can't get user id by login", "err", err.DetailedError)
p.API.LogWarn("can't get user id by login", "err", err.DetailedError)
}
return string(gitlabUsername)
}

func (p *Plugin) disconnectGitlabAccount(userID string) {
userInfo, err := p.getGitlabUserInfoByMattermostID(userID)
if err != nil {
p.API.LogError("can't get GitLab user info from mattermost id", "err", err.Message)
p.API.LogWarn("can't get GitLab user info from mattermost id", "err", err.Message)
return
}
if userInfo == nil {
return
}

if err := p.deleteGitlabUserInfo(userID); err != nil {
p.API.LogError("can't delete token in store", "err", err.Error, "userId", userID)
p.API.LogWarn("can't delete token in store", "err", err.Error, "userId", userID)
}
if err := p.deleteGitlabToUserIDMapping(userInfo.GitlabUsername); err != nil {
p.API.LogError("can't delete username in store", "err", err.Error, "username", userInfo.GitlabUsername)
p.API.LogWarn("can't delete username in store", "err", err.Error, "username", userInfo.GitlabUsername)
}
if err := p.deleteGitlabIDToUserIDMapping(userInfo.GitlabUserID); err != nil {
p.API.LogError("can't delete user id in store", "err", err.Error, "id", userInfo.GitlabUserID)
p.API.LogWarn("can't delete user id in store", "err", err.Error, "id", userInfo.GitlabUserID)
}

if user, err := p.API.GetUser(userID); err == nil && user.Props != nil && len(user.Props["git_user"]) > 0 {
delete(user.Props, "git_user")
if _, err := p.API.UpdateUser(user); err != nil {
p.API.LogError("can't update user after delete git account", "err", err.DetailedError)
p.API.LogWarn("can't update user after delete git account", "err", err.DetailedError)
}
}

Expand All @@ -383,7 +383,7 @@ func (p *Plugin) registerChimeraURL() {
func (p *Plugin) CreateBotDMPost(userID, message, postType string) *model.AppError {
channel, err := p.API.GetDirectChannel(userID, p.BotUserID)
if err != nil {
p.API.LogError("Couldn't get bot's DM channel", "user_id", userID)
p.API.LogWarn("Couldn't get bot's DM channel", "user_id", userID)
return err
}

Expand All @@ -395,7 +395,7 @@ func (p *Plugin) CreateBotDMPost(userID, message, postType string) *model.AppErr
}

if _, err := p.API.CreatePost(post); err != nil {
p.API.LogError("can't post DM", "err", err.DetailedError)
p.API.LogWarn("can't post DM", "err", err.DetailedError)
return err
}

Expand All @@ -405,15 +405,15 @@ func (p *Plugin) CreateBotDMPost(userID, message, postType string) *model.AppErr
func (p *Plugin) PostToDo(ctx context.Context, info *gitlab.UserInfo) {
hasTodo, text, err := p.GetToDo(ctx, info)
if err != nil {
p.API.LogError("can't post todo", "err", err.Error())
p.API.LogWarn("can't post todo", "err", err.Error())
return
}
if !hasTodo {
return
}

if err := p.CreateBotDMPost(info.UserID, text, "custom_git_todo"); err != nil {
p.API.LogError("can't create dm post in post todo", "err", err.DetailedError)
p.API.LogWarn("can't create dm post in post todo", "err", err.DetailedError)
}
}

Expand Down
6 changes: 3 additions & 3 deletions server/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (p *Plugin) GetSubscriptions() (*Subscriptions, error) {

value, err := p.API.KVGet(SubscriptionsKey)
if err != nil {
p.API.LogError("can't get subscriptions from kvstore", "err", err.DetailedError)
p.API.LogWarn("can't get subscriptions from kvstore", "err", err.DetailedError)
return nil, err
}

Expand All @@ -107,7 +107,7 @@ func (p *Plugin) StoreSubscriptions(s *Subscriptions) error {
return err
}
if err := p.API.KVSet(SubscriptionsKey, b); err != nil {
p.API.LogError("can't set subscriptions in kvstore", "err", err.DetailedError)
p.API.LogWarn("can't set subscriptions in kvstore", "err", err.DetailedError)
}
return nil
}
Expand All @@ -122,7 +122,7 @@ func (p *Plugin) GetSubscribedChannelsForProject(

subs, err := p.GetSubscriptions()
if err != nil {
p.API.LogError("can't retrieve subscriptions", "err", err.Error())
p.API.LogWarn("can't retrieve subscriptions", "err", err.Error())
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
if len(userTo) > 0 && len(res.Message) > 0 {
info, err := p.getGitlabUserInfoByMattermostID(userTo)
if err != nil {
p.API.LogError("can't get user info to know if user wants to receive notifications", "err", err.Message)
p.API.LogWarn("can't get user info to know if user wants to receive notifications", "err", err.Message)
continue
}
if info.Settings.Notifications {
if err := p.CreateBotDMPost(userTo, res.Message, "custom_git_review_request"); err != nil {
p.API.LogError("can't send dm post", "err", err.Error())
p.API.LogWarn("can't send dm post", "err", err.Error())
}
}
}
Expand All @@ -173,7 +173,7 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
ChannelId: to,
}
if _, err := p.API.CreatePost(post); err != nil {
p.API.LogError("can't create post for webhook event", "err", err.Error())
p.API.LogWarn("can't create post for webhook event", "err", err.Error())
}
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (p *Plugin) permissionToProject(ctx context.Context, userID, namespace, pro

if result, err := p.GitlabClient.GetProject(ctx, info, namespace, project); result == nil || err != nil {
if err != nil {
p.API.LogError("can't get project in webhook", "err", err.Error(), "project", namespace+"/"+project)
p.API.LogWarn("can't get project in webhook", "err", err.Error(), "project", namespace+"/"+project)
}
return false
}
Expand Down