Sfoglia il codice sorgente

fix(locale): guard nil wrapped errors in Translate

Frédéric Guillot 3 settimane fa
parent
commit
7516bdfcaf
2 ha cambiato i file con 13 aggiunte e 0 eliminazioni
  1. 3 0
      internal/locale/error.go
  2. 10 0
      internal/locale/error_test.go

+ 3 - 0
internal/locale/error.go

@@ -25,6 +25,9 @@ func (l *LocalizedErrorWrapper) Error() error {
 
 
 func (l *LocalizedErrorWrapper) Translate(language string) string {
 func (l *LocalizedErrorWrapper) Translate(language string) string {
 	if l.translationKey == "" {
 	if l.translationKey == "" {
+		if l.originalErr == nil {
+			return ""
+		}
 		return l.originalErr.Error()
 		return l.originalErr.Error()
 	}
 	}
 	return NewPrinter(language).Printf(l.translationKey, l.translationArgs...)
 	return NewPrinter(language).Printf(l.translationKey, l.translationArgs...)

+ 10 - 0
internal/locale/error_test.go

@@ -283,6 +283,16 @@ func TestLocalizedErrorWrapper_WithNilError(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func TestLocalizedErrorWrapper_TranslateWithEmptyKeyAndNilError(t *testing.T) {
+	wrapper := NewLocalizedErrorWrapper(nil, "")
+
+	result := wrapper.Translate("en_US")
+	expected := ""
+	if result != expected {
+		t.Errorf("Expected empty string for nil wrapped error, got %q", result)
+	}
+}
+
 func TestLocalizedError_EmptyKey(t *testing.T) {
 func TestLocalizedError_EmptyKey(t *testing.T) {
 	localizedErr := NewLocalizedError("")
 	localizedErr := NewLocalizedError("")