fix: ignore all unmarshal errors from locale (#673)
This commit is contained in:
parent
f1e4cb2245
commit
fbf009fe75
2 changed files with 9 additions and 14 deletions
|
@ -3,7 +3,6 @@ package oidc
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -78,22 +77,16 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements json.Unmarshaler.
|
// 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.
|
// to an empty value (language "und") and no error will be returned.
|
||||||
// This state can be checked with the `l.Tag().IsRoot()` method.
|
// This state can be checked with the `l.Tag().IsRoot()` method.
|
||||||
func (l *Locale) UnmarshalJSON(data []byte) error {
|
func (l *Locale) UnmarshalJSON(data []byte) error {
|
||||||
err := json.Unmarshal(data, &l.tag)
|
err := json.Unmarshal(data, &l.tag)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// catch "well-formed but unknown" errors
|
|
||||||
var target language.ValueError
|
|
||||||
if errors.As(err, &target) {
|
|
||||||
l.tag = language.Tag{}
|
l.tag = language.Tag{}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Locales []language.Tag
|
type Locales []language.Tag
|
||||||
|
|
|
@ -234,7 +234,9 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "bad form, error",
|
name: "bad form, error",
|
||||||
input: `{"locale": "g!!!!!"}`,
|
input: `{"locale": "g!!!!!"}`,
|
||||||
wantErr: true,
|
want: dst{
|
||||||
|
Locale: &Locale{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue