Skip to content

Commit

Permalink
Merge pull request #2763 from dolthub/daylon/better-error
Browse files Browse the repository at this point in the history
Better error message for invalid charset string
  • Loading branch information
Hydrocharged authored Nov 23, 2024
2 parents a5fe3d6 + 199d2b0 commit d5eeb13
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 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
12 changes: 6 additions & 6 deletions sql/types/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const (

var (
// ErrLengthTooLarge is thrown when a string's length is too large given the other parameters.
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'")
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")
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 d5eeb13

Please sign in to comment.