From ac2c9a6eb7c4fbd5dfe5afaf89ff84b35c7d58b9 Mon Sep 17 00:00:00 2001 From: Daylon Wilkins Date: Fri, 22 Nov 2024 19:23:20 -0800 Subject: [PATCH] Better error message for bad string for charset --- enginetest/enginetests.go | 4 ++-- enginetest/queries/script_queries.go | 6 +++--- sql/types/strings.go | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/enginetest/enginetests.go b/enginetest/enginetests.go index 5cf3a8c69c..6dde55b703 100644 --- a/enginetest/enginetests.go +++ b/enginetest/enginetests.go @@ -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 (?);", diff --git a/enginetest/queries/script_queries.go b/enginetest/queries/script_queries.go index 35f4896643..cb80446166 100644 --- a/enginetest/queries/script_queries.go +++ b/enginetest/queries/script_queries.go @@ -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');", diff --git a/sql/types/strings.go b/sql/types/strings.go index eeb4275ee6..fc9da28df0 100644 --- a/sql/types/strings.go +++ b/sql/types/strings.go @@ -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) @@ -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) } } }