Skip to content

Commit

Permalink
fix: ignore all unmarshal errors from locale (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
muir authored Nov 1, 2024
1 parent f1e4cb2 commit fbf009f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
15 changes: 4 additions & 11 deletions pkg/oidc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package oidc
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -78,22 +77,16 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON implements json.Unmarshaler.
// When [language.ValueError] is encountered, the containing tag will be set
// All unmarshal errors for are ignored.
// When an error is encountered, the containing tag will be set
// to an empty value (language "und") and no error will be returned.
// This state can be checked with the `l.Tag().IsRoot()` method.
func (l *Locale) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &l.tag)
if err == nil {
return nil
}

// catch "well-formed but unknown" errors
var target language.ValueError
if errors.As(err, &target) {
if err != nil {
l.tag = language.Tag{}
return nil
}
return err
return nil
}

type Locales []language.Tag
Expand Down
8 changes: 5 additions & 3 deletions pkg/oidc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
},
},
{
name: "bad form, error",
input: `{"locale": "g!!!!!"}`,
wantErr: true,
name: "bad form, error",
input: `{"locale": "g!!!!!"}`,
want: dst{
Locale: &Locale{},
},
},
}

Expand Down

0 comments on commit fbf009f

Please sign in to comment.