mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-25 14:46:02 +03:00
Sentry update, go 1.15 support
* Clone() method in Localizer component allows to clone it just like LocalizationMiddleware does. * RavenClientInterface contains all *raven.Client method, which allows external use without type casting. * Tests and godoc fix for Go 1.15.
This commit is contained in:
parent
66b13fc30b
commit
c79b3b0c0f
@ -5,6 +5,7 @@ go:
|
||||
- '1.12'
|
||||
- '1.13'
|
||||
- '1.14'
|
||||
- '1.15'
|
||||
before_install:
|
||||
- go mod tidy
|
||||
script:
|
||||
|
@ -85,17 +85,10 @@ func DefaultLocalizerMatcher() language.Matcher {
|
||||
return language.NewMatcher(DefaultLanguages)
|
||||
}
|
||||
|
||||
// LocalizationMiddleware returns gin.HandlerFunc which will set localizer language by Accept-Language header
|
||||
// Result Localizer instance will share it's internal data (translations, bundles, etc) with instance which was used
|
||||
// to append middleware to gin.
|
||||
// Because of that all Localizer instances from this middleware will share *same* mutex. This mutex is used to wrap
|
||||
// i18n.Bundle methods (those aren't goroutine-safe to use).
|
||||
// Usage:
|
||||
// engine := gin.New()
|
||||
// localizer := NewLocalizer("en", DefaultLocalizerBundle(), DefaultLocalizerMatcher(), "translations")
|
||||
// engine.Use(localizer.LocalizationMiddleware())
|
||||
func (l *Localizer) LocalizationMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// Clone *core.Localizer. Clone shares it's translations with the parent localizer. Language tag will not be shared.
|
||||
// Because of that you can change clone's language without affecting parent localizer.
|
||||
// This method should be used when LocalizationMiddleware is not feasible (outside of *gin.HandlerFunc).
|
||||
func (l *Localizer) Clone() *Localizer {
|
||||
clone := &Localizer{
|
||||
i18nStorage: l.i18nStorage,
|
||||
TranslationsBox: l.TranslationsBox,
|
||||
@ -104,6 +97,23 @@ func (l *Localizer) LocalizationMiddleware() gin.HandlerFunc {
|
||||
TranslationsPath: l.TranslationsPath,
|
||||
loadMutex: l.loadMutex,
|
||||
}
|
||||
clone.SetLanguage(DefaultLanguage)
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
// LocalizationMiddleware returns gin.HandlerFunc which will set localizer language by Accept-Language header
|
||||
// Result Localizer instance will share it's internal data (translations, bundles, etc) with instance which was used
|
||||
// to append middleware to gin.
|
||||
// Because of that all Localizer instances from this middleware will share *same* mutex. This mutex is used to wrap
|
||||
// i18n.Bundle methods (those aren't goroutine-safe to use).
|
||||
// Usage:
|
||||
// engine := gin.New()
|
||||
// localizer := NewLocalizer("en", DefaultLocalizerMatcher(), "translations")
|
||||
// engine.Use(localizer.LocalizationMiddleware())
|
||||
func (l *Localizer) LocalizationMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
clone := l.Clone()
|
||||
clone.SetLocale(c.GetHeader("Accept-Language"))
|
||||
c.Set(LocalizerContextKey, clone)
|
||||
}
|
||||
|
@ -157,6 +157,19 @@ func (l *LocalizerTest) Test_GetLocalizedMessage() {
|
||||
assert.Equal(l.T(), "Test message", message)
|
||||
}
|
||||
|
||||
func (l *LocalizerTest) Test_Clone() {
|
||||
defer func() {
|
||||
require.Nil(l.T(), recover())
|
||||
}()
|
||||
|
||||
localizer := l.localizer.Clone()
|
||||
localizer.SetLanguage(language.Russian)
|
||||
|
||||
assert.NotEqual(l.T(), l.localizer.LanguageTag, localizer.LanguageTag)
|
||||
assert.Equal(l.T(), "Test message", l.localizer.GetLocalizedMessage("message"))
|
||||
assert.Equal(l.T(), "Тестовое сообщение", localizer.GetLocalizedMessage("message"))
|
||||
}
|
||||
|
||||
func (l *LocalizerTest) Test_GetLocalizedTemplateMessage() {
|
||||
defer func() {
|
||||
require.Nil(l.T(), recover())
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@ -61,6 +62,7 @@ func (r ravenPacket) getRequest() (*raven.Http, bool) {
|
||||
}
|
||||
|
||||
type ravenClientMock struct {
|
||||
raven.Client
|
||||
captured []ravenPacket
|
||||
mu sync.RWMutex
|
||||
wg sync.WaitGroup
|
||||
@ -92,7 +94,7 @@ func (r *ravenClientMock) CaptureMessageAndWait(message string, tags map[string]
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
defer r.wg.Done()
|
||||
eventID := string(rand.Uint64())
|
||||
eventID := strconv.FormatUint(rand.Uint64(), 10)
|
||||
r.captured = append(r.captured, ravenPacket{
|
||||
EventID: eventID,
|
||||
Message: message,
|
||||
|
@ -4,7 +4,28 @@ import "github.com/getsentry/raven-go"
|
||||
|
||||
// RavenClientInterface includes all necessary calls from *raven.Client. Therefore, it can be mocked or replaced.
|
||||
type RavenClientInterface interface {
|
||||
SetIgnoreErrors(errs []string) error
|
||||
SetDSN(dsn string) error
|
||||
SetRelease(release string)
|
||||
SetEnvironment(environment string)
|
||||
SetDefaultLoggerName(name string)
|
||||
SetSampleRate(rate float32) error
|
||||
Capture(packet *raven.Packet, captureTags map[string]string) (eventID string, ch chan error)
|
||||
CaptureMessage(message string, tags map[string]string, interfaces ...raven.Interface) string
|
||||
CaptureMessageAndWait(message string, tags map[string]string, interfaces ...raven.Interface) string
|
||||
CaptureError(err error, tags map[string]string, interfaces ...raven.Interface) string
|
||||
CaptureErrorAndWait(err error, tags map[string]string, interfaces ...raven.Interface) string
|
||||
CapturePanic(f func(), tags map[string]string, interfaces ...raven.Interface) (err interface{}, errorID string)
|
||||
CapturePanicAndWait(f func(), tags map[string]string, interfaces ...raven.Interface) (err interface{}, errorID string)
|
||||
Close()
|
||||
Wait()
|
||||
URL() string
|
||||
ProjectID() string
|
||||
Release() string
|
||||
IncludePaths() []string
|
||||
SetIncludePaths(p []string)
|
||||
SetUserContext(u *raven.User)
|
||||
SetHttpContext(h *raven.Http)
|
||||
SetTagsContext(t map[string]string)
|
||||
ClearContext()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user