Skip to content

Commit

Permalink
Merge pull request #241 from taniabogatsch/error-fix
Browse files Browse the repository at this point in the history
Generalise exported Error
  • Loading branch information
taniabogatsch authored Jul 1, 2024
2 parents 2725c1c + 16038c0 commit 14c115e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
96 changes: 48 additions & 48 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,54 +80,54 @@ var (
errCreateConfig = errors.New("could not create config for database")
)

type DuckDBErrorType int
type ErrorType int

const (
ErrorTypeInvalid DuckDBErrorType = iota // invalid type
ErrorTypeOutOfRange // value out of range error
ErrorTypeConversion // conversion/casting error
ErrorTypeUnknownType // unknown type error
ErrorTypeDecimal // decimal related
ErrorTypeMismatchType // type mismatch
ErrorTypeDivideByZero // divide by 0
ErrorTypeObjectSize // object size exceeded
ErrorTypeInvalidType // incompatible for operation
ErrorTypeSerialization // serialization
ErrorTypeTransaction // transaction management
ErrorTypeNotImplemented // method not implemented
ErrorTypeExpression // expression parsing
ErrorTypeCatalog // catalog related
ErrorTypeParser // parser related
ErrorTypePlanner // planner related
ErrorTypeScheduler // scheduler related
ErrorTypeExecutor // executor related
ErrorTypeConstraint // constraint related
ErrorTypeIndex // index related
ErrorTypeStat // stat related
ErrorTypeConnection // connection related
ErrorTypeSyntax // syntax related
ErrorTypeSettings // settings related
ErrorTypeBinder // binder related
ErrorTypeNetwork // network related
ErrorTypeOptimizer // optimizer related
ErrorTypeNullPointer // nullptr exception
ErrorTypeIO // IO exception
ErrorTypeInterrupt // interrupt
ErrorTypeFatal // Fatal exceptions are non-recoverable, and render the entire DB in an unusable state
ErrorTypeInternal // Internal exceptions indicate something went wrong internally (i.e. bug in the code base)
ErrorTypeInvalidInput // Input or arguments error
ErrorTypeOutOfMemory // out of memory
ErrorTypePermission // insufficient permissions
ErrorTypeParameterNotResolved // parameter types could not be resolved
ErrorTypeParameterNotAllowed // parameter types not allowed
ErrorTypeDependency // dependency
ErrorTypeInvalid ErrorType = iota // invalid type
ErrorTypeOutOfRange // value out of range error
ErrorTypeConversion // conversion/casting error
ErrorTypeUnknownType // unknown type error
ErrorTypeDecimal // decimal related
ErrorTypeMismatchType // type mismatch
ErrorTypeDivideByZero // divide by 0
ErrorTypeObjectSize // object size exceeded
ErrorTypeInvalidType // incompatible for operation
ErrorTypeSerialization // serialization
ErrorTypeTransaction // transaction management
ErrorTypeNotImplemented // method not implemented
ErrorTypeExpression // expression parsing
ErrorTypeCatalog // catalog related
ErrorTypeParser // parser related
ErrorTypePlanner // planner related
ErrorTypeScheduler // scheduler related
ErrorTypeExecutor // executor related
ErrorTypeConstraint // constraint related
ErrorTypeIndex // index related
ErrorTypeStat // stat related
ErrorTypeConnection // connection related
ErrorTypeSyntax // syntax related
ErrorTypeSettings // settings related
ErrorTypeBinder // binder related
ErrorTypeNetwork // network related
ErrorTypeOptimizer // optimizer related
ErrorTypeNullPointer // nullptr exception
ErrorTypeIO // IO exception
ErrorTypeInterrupt // interrupt
ErrorTypeFatal // Fatal exceptions are non-recoverable, and render the entire DB in an unusable state
ErrorTypeInternal // Internal exceptions indicate something went wrong internally (i.e. bug in the code base)
ErrorTypeInvalidInput // Input or arguments error
ErrorTypeOutOfMemory // out of memory
ErrorTypePermission // insufficient permissions
ErrorTypeParameterNotResolved // parameter types could not be resolved
ErrorTypeParameterNotAllowed // parameter types not allowed
ErrorTypeDependency // dependency
ErrorTypeHTTP
ErrorTypeMissingExtension // Thrown when an extension is used but not loaded
ErrorTypeAutoLoad // Thrown when an extension is used but not loaded
ErrorTypeSequence
)

var errorPrefixMap = map[string]DuckDBErrorType{
var errorPrefixMap = map[string]ErrorType{
"Invalid Error": ErrorTypeInvalid,
"Out of Range Error": ErrorTypeOutOfRange,
"Conversion Error": ErrorTypeConversion,
Expand Down Expand Up @@ -172,18 +172,18 @@ var errorPrefixMap = map[string]DuckDBErrorType{
"Sequence Error": ErrorTypeSequence,
}

type DuckDBError struct {
Type DuckDBErrorType
type Error struct {
Type ErrorType
Msg string
}

func (de *DuckDBError) Error() string {
return de.Msg
func (e *Error) Error() string {
return e.Msg
}

func (de *DuckDBError) Is(err error) bool {
if derr, ok := err.(*DuckDBError); ok {
return derr.Msg == de.Msg
func (e *Error) Is(err error) bool {
if other, ok := err.(*Error); ok {
return other.Msg == e.Msg
}
return false
}
Expand All @@ -196,7 +196,7 @@ func getDuckDBError(errMsg string) error {
errType = typ
}
}
return &DuckDBError{
return &Error{
Type: errType,
Msg: errMsg,
}
Expand Down
8 changes: 4 additions & 4 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestDuckDBErrors(t *testing.T) {

testCases := []struct {
tpl string
errTyp DuckDBErrorType
errTyp ErrorType
}{
{
tpl: "SELECT * FROM not_exist WHERE baz=0",
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestDuckDBErrors(t *testing.T) {
}
for _, tc := range testCases {
_, err := db.Exec(tc.tpl)
de, ok := err.(*DuckDBError)
de, ok := err.(*Error)
if !ok {
require.Fail(t, "error type is not DuckDBError", "tql: %s\ngot: %#v", tc.tpl, err)
}
Expand All @@ -357,7 +357,7 @@ func TestDuckDBErrors(t *testing.T) {

func TestGetDuckDBError(t *testing.T) {
// only for the corner cases
testCases := []*DuckDBError{
testCases := []*Error{
{
Msg: "",
Type: ErrorTypeInvalid,
Expand All @@ -382,7 +382,7 @@ func TestGetDuckDBError(t *testing.T) {
}

for _, tc := range testCases {
err := getDuckDBError(tc.Msg).(*DuckDBError)
err := getDuckDBError(tc.Msg).(*Error)
require.Equal(t, tc, err)
}
}
2 changes: 1 addition & 1 deletion statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestPrepareWithError(t *testing.T) {
for _, tc := range testCases {
stmt, err := db.Prepare(tc.tpl)
if err != nil {
if _, ok := err.(*DuckDBError); !ok {
if _, ok := err.(*Error); !ok {
require.Fail(t, "error type is not DuckDBError")
}
require.ErrorContains(t, err, tc.err)
Expand Down

0 comments on commit 14c115e

Please sign in to comment.