Revert "fix: ignore all unmarshal errors from locale (#673)"

This reverts commit fbf009fe75.
This commit is contained in:
Tim Möhlmann 2024-11-11 11:55:44 +02:00
parent fbf009fe75
commit 759105530d
2 changed files with 15 additions and 10 deletions

View file

@ -3,6 +3,7 @@ package oidc
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
@ -77,18 +78,24 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
}
// UnmarshalJSON implements json.Unmarshaler.
// All unmarshal errors for are ignored.
// When an error is encountered, the containing tag will be set
// When [language.ValueError] 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 {
l.tag = language.Tag{}
}
if err == nil {
return nil
}
// catch "well-formed but unknown" errors
var target language.ValueError
if errors.As(err, &target) {
l.tag = language.Tag{}
return nil
}
return err
}
type Locales []language.Tag
// ParseLocales parses a slice of strings into Locales.

View file

@ -234,9 +234,7 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
{
name: "bad form, error",
input: `{"locale": "g!!!!!"}`,
want: dst{
Locale: &Locale{},
},
wantErr: true,
},
}