Skip to content

Commit

Permalink
Better error message for bad string for charset
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydrocharged committed Nov 23, 2024
1 parent a5fe3d6 commit ac2c9a6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4314,14 +4314,14 @@ func TestPreparedInsert(t *testing.T, harness Harness) {
Bindings: map[string]sqlparser.Expr{
"v1": mustBuildBindVariable([]byte{0x99, 0x98, 0x97}),
},
ExpectedErrStr: "incorrect string value: '[153 152 151]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[153 152 151]'",
},
{
Query: "INSERT INTO test VALUES (?);",
Bindings: map[string]sqlparser.Expr{
"v1": mustBuildBindVariable(string([]byte{0x99, 0x98, 0x97})),
},
ExpectedErrStr: "incorrect string value: '[153 152 151]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[153 152 151]'",
},
{
Query: "INSERT INTO test2 VALUES (?);",
Expand Down
6 changes: 3 additions & 3 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7135,15 +7135,15 @@ where
Assertions: []ScriptTestAssertion{
{
Query: "insert into t(c) values (X'9876543210');",
ExpectedErrStr: "incorrect string value: '[152 118 84 50 16]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[152 118 84 50 16]'",
},
{
Query: "insert into t(v) values (X'9876543210');",
ExpectedErrStr: "incorrect string value: '[152 118 84 50 16]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[152 118 84 50 16]'",
},
{
Query: "insert into t(txt) values (X'9876543210');",
ExpectedErrStr: "incorrect string value: '[152 118 84 50 16]'",
ExpectedErrStr: "invalid string for charset utf8mb4: '[152 118 84 50 16]'",
},
{
Query: "insert into t(b) values (X'9876543210');",
Expand Down
6 changes: 3 additions & 3 deletions sql/types/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
ErrLengthTooLarge = errors.NewKind("length is %v but max allowed is %v")
ErrLengthBeyondLimit = errors.NewKind("string '%v' is too large for column '%v'")
ErrBinaryCollation = errors.NewKind("binary types must have the binary collation: %v")
ErrIncorrectStringValue = errors.NewKind("incorrect string value: '%v'")
ErrBadCharsetString = errors.NewKind("invalid string for charset %s: '%v'")

TinyText = MustCreateStringWithDefaults(sqltypes.Text, TinyTextBlobMax)
Text = MustCreateStringWithDefaults(sqltypes.Text, TextBlobMax)
Expand Down Expand Up @@ -428,11 +428,11 @@ func ConvertToString(v interface{}, t sql.StringType) (string, error) {
if !IsBinaryType(t) && !utf8.Valid(bytesVal) {
charset := t.CharacterSet()
if charset == sql.CharacterSet_utf8mb4 {
return "", ErrIncorrectStringValue.New(bytesVal)
return "", ErrBadCharsetString.New(charset.String(), bytesVal)
} else {
var ok bool
if bytesVal, ok = t.CharacterSet().Encoder().Decode(bytesVal); !ok {
return "", ErrIncorrectStringValue.New(bytesVal)
return "", ErrBadCharsetString.New(charset.String(), bytesVal)
}
}
}
Expand Down

0 comments on commit ac2c9a6

Please sign in to comment.