[MM-56061] Only render where field in model.AppError when it's present (#25648)
* Only render where field in model.AppError when it's present * Remove trailing comma from permission error
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a54f927e3d
Коммит
5b6b425cfc
@@ -3,6 +3,10 @@
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
PermissionScopeSystem = "system_scope"
|
||||
PermissionScopeTeam = "team_scope"
|
||||
@@ -2455,3 +2459,14 @@ func initializePermissions() {
|
||||
func init() {
|
||||
initializePermissions()
|
||||
}
|
||||
|
||||
func MakePermissionError(s *Session, permissions []*Permission) *AppError {
|
||||
permissionsStr := "permission="
|
||||
for i, permission := range permissions {
|
||||
permissionsStr += permission.Id
|
||||
if i != len(permissions)-1 {
|
||||
permissionsStr += ","
|
||||
}
|
||||
}
|
||||
return NewAppError("Permissions", "api.context.permissions.app_error", nil, "userId="+s.UserId+", "+permissionsStr, http.StatusForbidden)
|
||||
}
|
||||
|
||||
53
server/public/model/permission_test.go
Обычный файл
53
server/public/model/permission_test.go
Обычный файл
@@ -0,0 +1,53 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMakePermissionError(t *testing.T) {
|
||||
userID := NewId()
|
||||
for name, tc := range map[string]struct {
|
||||
s *Session
|
||||
permissions []*Permission
|
||||
expectedError string
|
||||
}{
|
||||
"nil permissions, nil session": {
|
||||
s: &Session{},
|
||||
permissions: nil,
|
||||
expectedError: "Permissions: api.context.permissions.app_error, userId=, permission=",
|
||||
},
|
||||
"nil permissions": {
|
||||
s: &Session{UserId: userID},
|
||||
permissions: nil,
|
||||
expectedError: fmt.Sprintf("Permissions: api.context.permissions.app_error, userId=%s, permission=", userID),
|
||||
},
|
||||
"empty permissions": {
|
||||
s: &Session{UserId: userID},
|
||||
permissions: []*Permission{},
|
||||
expectedError: fmt.Sprintf("Permissions: api.context.permissions.app_error, userId=%s, permission=", userID),
|
||||
},
|
||||
"one permission": {
|
||||
s: &Session{UserId: userID},
|
||||
permissions: []*Permission{PermissionManageSystem},
|
||||
expectedError: fmt.Sprintf("Permissions: api.context.permissions.app_error, userId=%s, permission=manage_system", userID),
|
||||
},
|
||||
"two permissions": {
|
||||
s: &Session{UserId: userID},
|
||||
permissions: []*Permission{PermissionManageSystem, PermissionAssignSystemAdminRole},
|
||||
expectedError: fmt.Sprintf("Permissions: api.context.permissions.app_error, userId=%s, permission=manage_system,assign_system_admin_role", userID),
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
appErr := MakePermissionError(tc.s, tc.permissions)
|
||||
require.NotNil(t, appErr)
|
||||
assert.Equal(t, tc.expectedError, appErr.Error())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -246,8 +246,11 @@ func (er *AppError) Error() string {
|
||||
var sb strings.Builder
|
||||
|
||||
// render the error information
|
||||
sb.WriteString(er.Where)
|
||||
sb.WriteString(": ")
|
||||
if er.Where != "" {
|
||||
sb.WriteString(er.Where)
|
||||
sb.WriteString(": ")
|
||||
}
|
||||
|
||||
if er.Message != NoTranslation {
|
||||
sb.WriteString(er.Message)
|
||||
}
|
||||
|
||||
@@ -97,6 +97,11 @@ func TestAppErrorRender(t *testing.T) {
|
||||
assert.EqualError(t, aerr, "here: message")
|
||||
})
|
||||
|
||||
t.Run("Without where", func(t *testing.T) {
|
||||
aerr := NewAppError("", "message", nil, "details", http.StatusTeapot)
|
||||
assert.EqualError(t, aerr, "message, details")
|
||||
})
|
||||
|
||||
t.Run("Detailed", func(t *testing.T) {
|
||||
aerr := NewAppError("here", "message", nil, "details", http.StatusTeapot)
|
||||
assert.EqualError(t, aerr, "here: message, details")
|
||||
|
||||
Ссылка в новой задаче
Block a user