github.com/blend/go-sdk@v1.20220411.3/db/errors.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package db 9 10 import ( 11 "net" 12 13 "github.com/blend/go-sdk/ex" 14 ) 15 16 const ( 17 // ErrDestinationNotStruct is an exception class. 18 ErrDestinationNotStruct ex.Class = "db: destination object is not a struct" 19 // ErrConfigUnset is an exception class. 20 ErrConfigUnset ex.Class = "db: config is unset" 21 // ErrUnsafeSSLMode is an error indicating unsafe ssl mode in production. 22 ErrUnsafeSSLMode ex.Class = "db: unsafe ssl mode in prodlike environment" 23 // ErrUsernameUnset is an error indicating there is no username set in a prodlike environment. 24 ErrUsernameUnset ex.Class = "db: username is unset in prodlike environment" 25 // ErrPasswordUnset is an error indicating there is no password set in a prodlike environment. 26 ErrPasswordUnset ex.Class = "db: password is unset in prodlike environment" 27 // ErrDurationConversion is the error returned when a duration cannot be 28 // converted to multiple of some base (e.g. milliseconds or seconds) 29 // without round off. 30 ErrDurationConversion ex.Class = "db: cannot convert duration" 31 // ErrConnectionAlreadyOpen is an error indicating the db connection was already opened. 32 ErrConnectionAlreadyOpen ex.Class = "db: the connection is already opened" 33 // ErrConnectionClosed is an error indicating the db connection hasn't been opened. 34 ErrConnectionClosed ex.Class = "db: the connection is closed, or is being used before opened" 35 // ErrPlanCacheUnset is an error indicating the statement cache is unset. 36 ErrPlanCacheUnset ex.Class = "db: the plan cache is unset" 37 // ErrPlanCacheKeyUnset is an error indicating the plan cache key is unset. 38 ErrPlanCacheKeyUnset ex.Class = "db: the plan cache key is unset" 39 // ErrCollectionNotSlice is an error returned by OutMany if the destination is not a slice. 40 ErrCollectionNotSlice ex.Class = "db: outmany destination collection is not a slice" 41 // ErrInvalidIDs is an error returned by Get if the ids aren't provided. 42 ErrInvalidIDs ex.Class = "db: invalid `ids` parameter" 43 // ErrNoPrimaryKey is an error returned by a number of operations that depend on a primary key. 44 ErrNoPrimaryKey ex.Class = "db: no primary key on object" 45 // ErrRowsNotColumnsProvider is returned by `PopulateByName` if you do not pass in `sql.Rows` as the scanner. 46 ErrRowsNotColumnsProvider ex.Class = "db: rows is not a columns provider" 47 // ErrTooManyRows is returned by Out if there is more than one row returned by the query 48 ErrTooManyRows ex.Class = "db: too many rows returned to map to single object" 49 50 // ErrNetwork is a grouped error for network issues. 51 ErrNetwork ex.Class = "db: network error" 52 ) 53 54 // IsConfigUnset returns if the error is an `ErrConfigUnset`. 55 func IsConfigUnset(err error) bool { 56 return ex.Is(err, ErrConfigUnset) 57 } 58 59 // IsUnsafeSSLMode returns if an error is an `ErrUnsafeSSLMode`. 60 func IsUnsafeSSLMode(err error) bool { 61 return ex.Is(err, ErrUnsafeSSLMode) 62 } 63 64 // IsUsernameUnset returns if an error is an `ErrUsernameUnset`. 65 func IsUsernameUnset(err error) bool { 66 return ex.Is(err, ErrUsernameUnset) 67 } 68 69 // IsPasswordUnset returns if an error is an `ErrPasswordUnset`. 70 func IsPasswordUnset(err error) bool { 71 return ex.Is(err, ErrPasswordUnset) 72 } 73 74 // IsDurationConversion returns if an error is an `ErrDurationConversion`. 75 func IsDurationConversion(err error) bool { 76 return ex.Is(err, ErrDurationConversion) 77 } 78 79 // IsConnectionClosed returns if the error is an `ErrConnectionClosed`. 80 func IsConnectionClosed(err error) bool { 81 return ex.Is(err, ErrConnectionClosed) 82 } 83 84 // IsPlanCacheUnset returns if the error is an `ErrConnectionClosed`. 85 func IsPlanCacheUnset(err error) bool { 86 return ex.Is(err, ErrPlanCacheUnset) 87 } 88 89 // IsPlanCacheKeyUnset returns if the error is an `ErrPlanCacheKeyUnset`. 90 func IsPlanCacheKeyUnset(err error) bool { 91 return ex.Is(err, ErrPlanCacheKeyUnset) 92 } 93 94 // Error returns a new exception by parsing (potentially) 95 // a driver error into relevant pieces. 96 func Error(err error, options ...ex.Option) error { 97 if err == nil { 98 return nil 99 } 100 if _, ok := err.(*net.OpError); ok { 101 return ex.New(ErrNetwork, ex.OptInner(err)) 102 } 103 return ex.New(err, options...) 104 }