MM-52216: Trim errors (#23040)
https://mattermost.atlassian.net/browse/MM-52216 ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
041cbe2d24
Коммит
67735be261
@@ -3073,7 +3073,7 @@ func (s SqlChannelStore) Autocomplete(userID, term string, includeDeleted, isGue
|
||||
channels := model.ChannelListWithTeamData{}
|
||||
err = s.GetReplicaX().Select(&channels, sql, args...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "could not find channel with term=%s", term)
|
||||
return nil, errors.Wrapf(err, "could not find channel with term=%s", trimInput(term))
|
||||
}
|
||||
return channels, nil
|
||||
}
|
||||
@@ -3186,7 +3186,7 @@ func (s SqlChannelStore) AutocompleteInTeamForSearch(teamID string, userID strin
|
||||
// query the database
|
||||
err = s.GetReplicaX().Select(&channels, sql, args...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with term='%s'", term)
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with term='%s'", trimInput(term))
|
||||
}
|
||||
|
||||
directChannels, err := s.autocompleteInTeamForSearchDirectMessages(userID, term)
|
||||
@@ -3242,7 +3242,7 @@ func (s SqlChannelStore) autocompleteInTeamForSearchDirectMessages(userID string
|
||||
// query the channel list from the database using SQLX
|
||||
channels := model.ChannelList{}
|
||||
if err := s.GetReplicaX().Select(&channels, sql, args...); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with term='%s' (%s %% %v)", term, sql, args)
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with term='%s'", trimInput(term))
|
||||
}
|
||||
|
||||
return channels, nil
|
||||
@@ -3461,7 +3461,7 @@ func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearch
|
||||
}
|
||||
channels := model.ChannelListWithTeamData{}
|
||||
if err2 := s.GetReplicaX().Select(&channels, queryString, args...); err2 != nil {
|
||||
return nil, 0, errors.Wrapf(err2, "failed to find Channels with term='%s'", term)
|
||||
return nil, 0, errors.Wrapf(err2, "failed to find Channels with term='%s'", trimInput(term))
|
||||
}
|
||||
|
||||
var totalCount int64
|
||||
@@ -3474,7 +3474,7 @@ func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearch
|
||||
return nil, 0, errors.Wrap(err, "channel_tosql")
|
||||
}
|
||||
if err2 := s.GetReplicaX().Get(&totalCount, queryString, args...); err2 != nil {
|
||||
return nil, 0, errors.Wrapf(err2, "failed to find Channels with term='%s'", term)
|
||||
return nil, 0, errors.Wrapf(err2, "failed to find Channels with term='%s'", trimInput(term))
|
||||
}
|
||||
} else {
|
||||
totalCount = int64(len(channels))
|
||||
@@ -3651,7 +3651,7 @@ func (s SqlChannelStore) performSearch(searchQuery sq.SelectBuilder, term string
|
||||
channels := model.ChannelList{}
|
||||
err = s.GetReplicaX().Select(&channels, sql, args...)
|
||||
if err != nil {
|
||||
return channels, errors.Wrapf(err, "failed to find Channels with term='%s'", term)
|
||||
return channels, errors.Wrapf(err, "failed to find Channels with term='%s'", trimInput(term))
|
||||
}
|
||||
|
||||
return channels, nil
|
||||
@@ -3744,7 +3744,7 @@ func (s SqlChannelStore) SearchGroupChannels(userId, term string) (model.Channel
|
||||
|
||||
groupChannels := model.ChannelList{}
|
||||
if err := s.GetReplicaX().Select(&groupChannels, sql, params...); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with term='%s' and userId=%s", term, userId)
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with term='%s' and userId=%s", trimInput(term), userId)
|
||||
}
|
||||
return groupChannels, nil
|
||||
}
|
||||
|
||||
@@ -681,7 +681,7 @@ func (fs SqlFileInfoStore) Search(paramsList []*model.SearchParams, userId, team
|
||||
items := []fileInfoWithChannelID{}
|
||||
err = fs.GetSearchReplicaX().Select(&items, queryString, args...)
|
||||
if err != nil {
|
||||
mlog.Warn("Query error searching files.", mlog.Err(err))
|
||||
mlog.Warn("Query error searching files.", mlog.String("error", trimInput(err.Error())))
|
||||
// Don't return the error to the caller as it is of no use to the user. Instead return an empty set of search results.
|
||||
} else {
|
||||
for _, item := range items {
|
||||
|
||||
@@ -2075,7 +2075,7 @@ func (s *SqlPostStore) search(teamId string, userId string, params *model.Search
|
||||
var posts []*model.Post
|
||||
|
||||
if err := s.GetSearchReplicaX().Select(&posts, searchQuery, searchQueryArgs...); err != nil {
|
||||
mlog.Warn("Query error searching posts.", mlog.Err(err))
|
||||
mlog.Warn("Query error searching posts.", mlog.String("error", trimInput(err.Error())))
|
||||
// Don't return the error to the caller as it is of no use to the user. Instead return an empty set of search results.
|
||||
} else {
|
||||
for _, p := range posts {
|
||||
|
||||
@@ -233,3 +233,14 @@ func SanitizeDataSource(driverName, dataSource string) (string, error) {
|
||||
return "", errors.New("invalid drivername. Not postgres or mysql.")
|
||||
}
|
||||
}
|
||||
|
||||
const maxTokenSize = 50
|
||||
|
||||
// trimInput limits the string to a max size to prevent clogging up disk space
|
||||
// while logging
|
||||
func trimInput(input string) string {
|
||||
if len(input) > maxTokenSize {
|
||||
input = input[:maxTokenSize] + "..."
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
@@ -251,6 +251,8 @@ type AppError struct {
|
||||
wrapped error
|
||||
}
|
||||
|
||||
const maxErrorLength = 1024
|
||||
|
||||
func (er *AppError) Error() string {
|
||||
var sb strings.Builder
|
||||
|
||||
@@ -276,7 +278,11 @@ func (er *AppError) Error() string {
|
||||
sb.WriteString(err.Error())
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
res := sb.String()
|
||||
if len(res) > maxErrorLength {
|
||||
res = res[:maxErrorLength] + "..."
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (er *AppError) Translate(T i18n.TranslateFunc) {
|
||||
|
||||
@@ -116,6 +116,13 @@ func TestAppErrorRender(t *testing.T) {
|
||||
aerr := NewAppError("here", "message", nil, "details", http.StatusTeapot).Wrap(fmt.Errorf("my error (%w)", fmt.Errorf("inner error")))
|
||||
assert.EqualError(t, aerr, "here: message, details, my error (inner error)")
|
||||
})
|
||||
|
||||
t.Run("MaxLength", func(t *testing.T) {
|
||||
str := strings.Repeat("error", 65536)
|
||||
msg := "msg"
|
||||
aerr := NewAppError("id", msg, nil, str, http.StatusTeapot).Wrap(errors.New(str))
|
||||
assert.Len(t, aerr.Error(), maxErrorLength+len(msg))
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppErrorSerialize(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user