Skip to content

Commit

Permalink
Merge branch 'refactor/error_handling_be' into refactor/error_handlin…
Browse files Browse the repository at this point in the history
…g_fe
  • Loading branch information
soneda-yuya authored Jan 12, 2025
2 parents 1a33a1a + def2d22 commit a6afa54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions server/internal/locales/locales.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func loadLocales(langs []string, localesFileType []string) {
// also if key is not found, it will panic
// because we want to know if the key is not found when server starts
func LoadError(key ErrorKey) (map[string]*Error, error) {
if key == "" {
return nil, fmt.Errorf("invalid key: empty string")
}

loadOnce.Do(func() {
loadLocales(langs, localesFileType)
})
Expand Down
14 changes: 12 additions & 2 deletions server/internal/locales/locales_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ func TestLoadLocales_FileNotFound(t *testing.T) {
cache = nil
loadOnce = sync.Once{}

t.Cleanup(func() {
cache = nil
loadOnce = sync.Once{}
})

defer func() {
if r := recover(); r != nil {
assert.Contains(t, fmt.Sprintf("%v", r), "open aaa/error.json: file does not exist")
assert.Equal(t, "open aaa/error.json: file does not exist", fmt.Sprintf("%v", r))
} else {
t.Fatal("Expected panic but it did not occur")
}
Expand All @@ -28,7 +33,6 @@ func TestLoadLocales_FileNotFound(t *testing.T) {
}

func TestLoadError(t *testing.T) {
// モック用の localesJson を置き換える
localesJson = testLocales

tests := []struct {
Expand Down Expand Up @@ -66,6 +70,12 @@ func TestLoadError(t *testing.T) {
expected: nil,
err: fmt.Errorf("message not found: pkg.project"),
},
{
name: "Empty key",
key: "",
expected: nil,
err: fmt.Errorf("invalid key: empty string"),
},
}

for _, tt := range tests {
Expand Down

0 comments on commit a6afa54

Please sign in to comment.