Implement yookassa client
This commit is contained in:
36
internal/api/common/error.go
Normal file
36
internal/api/common/error.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"payouts/internal/models"
|
||||
)
|
||||
|
||||
func Reason(reason string, params ...any) slog.Attr {
|
||||
return slog.String("reason", fmt.Sprintf(reason, params...))
|
||||
}
|
||||
|
||||
func ErrorResponse(w http.ResponseWriter, message string, err error, status int, logOpts ...any) {
|
||||
r, size := utf8.DecodeRuneInString(message)
|
||||
errorMsg := string(unicode.ToUpper(r)) + strings.ToLower(message[size:])
|
||||
|
||||
logFields := logOpts
|
||||
if err != nil {
|
||||
logFields = append([]any{slog.String("error", err.Error())}, logOpts...)
|
||||
}
|
||||
|
||||
slog.Error(errorMsg, logFields...)
|
||||
w.Header().Set("Content-type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
|
||||
json.NewEncoder(w).Encode(&models.ErrorResp{
|
||||
Message: errorMsg,
|
||||
Status: status,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user