Skip to content

Commit

Permalink
test with the actual exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
apocelipes committed Jul 1, 2024
1 parent 15c18ba commit c4a66b1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const (
ErrorTypeSequence
)

var exceptionPrefixMap = map[string]DuckDBErrorType{
var errorPrefixMap = map[string]DuckDBErrorType{
"Invalid Error": ErrorTypeInvalid,
"Out of Range Error": ErrorTypeOutOfRange,
"Conversion Error": ErrorTypeConversion,
Expand Down Expand Up @@ -192,7 +192,7 @@ func getDuckDBError(errMsg string) error {
errType := ErrorTypeInvalid
// find the end of the prefix ("<error-type> Error: ")
if idx := strings.Index(errMsg, ": "); idx != -1 {
if typ, ok := exceptionPrefixMap[errMsg[:idx]]; ok {
if typ, ok := errorPrefixMap[errMsg[:idx]]; ok {
errType = typ
}
}
Expand Down
84 changes: 62 additions & 22 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,48 +301,88 @@ func TestErrAPISetValue(t *testing.T) {
testError(t, err, errAPI.Error(), columnCountErrMsg)
}

func TestGetDuckDBError(t *testing.T) {
func TestDuckDBErrors(t *testing.T) {
db := openDB(t)
defer db.Close()
createTable(db, t, `CREATE TABLE duckdberror_test(bar VARCHAR UNIQUE, baz INT32)`)
_, err := db.Exec("INSERT INTO duckdberror_test(bar, baz) VALUES('bar', 0)")
require.NoError(t, err)

testCases := []struct {
msg string
typ DuckDBErrorType
tpl string
errTyp DuckDBErrorType
}{
{
msg: "",
typ: ErrorTypeInvalid,
tpl: "SELECT * FROM not_exist WHERE baz=0",
errTyp: ErrorTypeCatalog,
},
{
tpl: "COPY duckdberror_test FROM 'test.json'",
errTyp: ErrorTypeCatalog,
},
{
msg: "Unknown",
typ: ErrorTypeInvalid,
tpl: "SELECT * FROM duckdberror_test WHERE col=?",
errTyp: ErrorTypeBinder,
},
{
msg: "Error: xxx",
typ: ErrorTypeUnknownType,
tpl: "SELEC * FROM duckdberror_test baz=0",
errTyp: ErrorTypeParser,
},
{
msg: "Constraint Error: Duplicate key \"key\" violates unique constraint. If this is an unexpected constraint violation please double check with the known index limitations section in our documentation (https://duckdb.org/docs/sql/indexes).",
typ: ErrorTypeConstraint,
tpl: "INSERT INTO duckdberror_test(bar, baz) VALUES('bar', 1)",
errTyp: ErrorTypeConstraint,
},
{
tpl: "INSERT INTO duckdberror_test(bar, baz) VALUES('foo', 18446744073709551615)",
errTyp: ErrorTypeConversion,
},
{
tpl: "INSTALL not_exist",
errTyp: ErrorTypeHTTP,
},
{
tpl: "LOAD not_exist",
errTyp: ErrorTypeIO,
},
}
for _, tc := range testCases {
_, err := db.Exec(tc.tpl)
de, ok := err.(*DuckDBError)
if !ok {
require.Fail(t, "error type is not DuckDBError", "tql: %s\ngot: %#v", tc.tpl, err)
}
require.Equal(t, de.Type, tc.errTyp, "tql: %s\nactual error msg: %s", tc.tpl, de.Msg)
}
}

func TestGetDuckDBError(t *testing.T) {
// only for the corner cases
testCases := []*DuckDBError{
{
Msg: "",
Type: ErrorTypeInvalid,
},
{
msg: "Invalid Error: xxx",
typ: ErrorTypeInvalid,
Msg: "Unknown",
Type: ErrorTypeInvalid,
},
{
msg: "Invalid Input Error: xxx",
typ: ErrorTypeInvalidInput,
Msg: "Error: xxx",
Type: ErrorTypeUnknownType,
},
// next two for the prefix testing
{
msg: "Parameter Not Resolved Error",
typ: ErrorTypeInvalid,
Msg: "Invalid Error: xxx",
Type: ErrorTypeInvalid,
},
{
msg: "Parameter Not Resolved Error: correct error messages format",
typ: ErrorTypeParameterNotResolved,
Msg: "Invalid Input Error: xxx",
Type: ErrorTypeInvalidInput,
},
}

for _, tc := range testCases {
err := getDuckDBError(tc.msg).(*DuckDBError)
require.Equal(t, tc.typ, err.Type)
require.Equal(t, tc.msg, err.Msg)
err := getDuckDBError(tc.Msg).(*DuckDBError)
require.Equal(t, tc, err)
}
}

0 comments on commit c4a66b1

Please sign in to comment.