Merge branch 'master' into MM-45118_my_top_dms

Этот коммит содержится в:
Mattermod
2022-07-29 10:46:32 +03:00
коммит произвёл GitHub
родитель f84a6ae7ac 887bc0173e
Коммит 8a532c0b8c
95 изменённых файлов: 1097 добавлений и 999 удалений

Просмотреть файл

@@ -105,6 +105,10 @@ type ValidateBusinessEmailRequest struct {
Email string `json:"email"`
}
type ValidateBusinessEmailResponse struct {
IsValid bool `json:"is_valid"`
}
// CloudCustomerInfo represents editable info of a customer.
type CloudCustomerInfo struct {
Name string `json:"name"`

Просмотреть файл

@@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/mail"
@@ -218,10 +217,32 @@ type AppError struct {
Where string `json:"-"` // The function where it happened in the form of Struct.Func
IsOAuth bool `json:"is_oauth,omitempty"` // Whether the error is OAuth specific
params map[string]any
wrapped error
}
func (er *AppError) Error() string {
return er.Where + ": " + er.Message + ", " + er.DetailedError
var sb strings.Builder
// render the error information
sb.WriteString(er.Where)
sb.WriteString(": ")
sb.WriteString(er.Message)
// only render the detailed error when it's present
if er.DetailedError != "" {
sb.WriteString(", ")
sb.WriteString(er.DetailedError)
}
// render all wrapped errors
err := er.wrapped
for err != nil {
sb.WriteString(", ")
sb.WriteString(err.Error())
err = errors.Unwrap(err)
}
return sb.String()
}
func (er *AppError) Translate(T i18n.TranslateFunc) {
@@ -249,10 +270,19 @@ func (er *AppError) ToJSON() string {
return string(b)
}
func (er *AppError) Unwrap() error {
return er.wrapped
}
func (er *AppError) Wrap(err error) *AppError {
er.wrapped = err
return er
}
// AppErrorFromJSON will decode the input and return an AppError
func AppErrorFromJSON(data io.Reader) *AppError {
str := ""
bytes, rerr := ioutil.ReadAll(data)
bytes, rerr := io.ReadAll(data)
if rerr != nil {
str = rerr.Error()
} else {
@@ -269,14 +299,15 @@ func AppErrorFromJSON(data io.Reader) *AppError {
}
func NewAppError(where string, id string, params map[string]any, details string, status int) *AppError {
ap := &AppError{}
ap.Id = id
ap.params = params
ap.Message = id
ap.Where = where
ap.DetailedError = details
ap.StatusCode = status
ap.IsOAuth = false
ap := &AppError{
Id: id,
params: params,
Message: id,
Where: where,
DetailedError: details,
StatusCode: status,
IsOAuth: false,
}
ap.Translate(translateFunc)
return ap
}

Просмотреть файл

@@ -85,6 +85,33 @@ func TestAppErrorJunk(t *testing.T) {
require.Equal(t, "body: <html><body>This is a broken test</body></html>", rerr.DetailedError)
}
func TestAppErrorRender(t *testing.T) {
t.Run("Minimal", func(t *testing.T) {
aerr := NewAppError("here", "message", nil, "", http.StatusTeapot)
assert.EqualError(t, aerr, "here: message")
})
t.Run("Detailed", func(t *testing.T) {
aerr := NewAppError("here", "message", nil, "details", http.StatusTeapot)
assert.EqualError(t, aerr, "here: message, details")
})
t.Run("Wrapped", func(t *testing.T) {
aerr := NewAppError("here", "message", nil, "", http.StatusTeapot).Wrap(fmt.Errorf("my error"))
assert.EqualError(t, aerr, "here: message, my error")
})
t.Run("WrappedMultiple", func(t *testing.T) {
aerr := NewAppError("here", "message", nil, "", http.StatusTeapot).Wrap(fmt.Errorf("my error (%w)", fmt.Errorf("inner error")))
assert.EqualError(t, aerr, "here: message, my error (inner error), inner error")
})
t.Run("DetailedWrappedMultiple", func(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), inner error")
})
}
func TestCopyStringMap(t *testing.T) {
itemKey := "item1"
originalMap := make(map[string]string)