// verifyError checks that either no error was found where none was // expected, or that an error was found when one was expected. Returns // "true" to indicate the behavior was as expected. func (t *logicTest) verifyError(sql, pos, expectErr, expectErrCode string, err error) bool { if expectErr == "" && expectErrCode == "" && err != nil { return t.unexpectedError(sql, pos, err) } if expectErr != "" && !testutils.IsError(err, expectErr) { t.Errorf("%s: expected %q, but found %v", pos, expectErr, err) return false } if expectErrCode != "" { if err != nil { pqErr, ok := err.(*pq.Error) if !ok { t.Errorf("%s: expected error code %q, but the error we found is not "+ "a libpq error: %s", pos, expectErrCode, err) return false } if pqErr.Code != pq.ErrorCode(expectErrCode) { t.Errorf("%s: expected error code %q, but found code %q (%s)", pos, expectErrCode, pqErr.Code, pqErr.Code.Name()) return false } } else { t.Errorf("%s: expected error code %q, but found success", pos, expectErrCode) return false } } return true }
} path, _ := filepath.Split(filepath.Join("/", uri.Path)) if _, err := insert.Exec(uri.Corpus, uri.Root, path, ticket, true); err != nil { return fmt.Errorf("error inserting file: %v", err) } uri.Signature, uri.Language = "", "" for { uri.Path = path path, _ = filepath.Split(strings.TrimSuffix(path, "/")) if path == "" { break } if _, err := insert.Exec(uri.Corpus, uri.Root, path, uri.String(), false); err != nil { if err, ok := err.(*pq.Error); ok && err.Code == pqUniqueViolationErrorCode { // Since we've found the current directory, we can stop recursively // adding parent directories now break } return fmt.Errorf("error inserting directory: %v", err) } } } return nil } const pqUniqueViolationErrorCode = pq.ErrorCode("23505")
package postgres import ( "database/sql" "log" "runtime" "github.com/lib/pq" ) const ( PG_UNIQUE_VIOLATION = pq.ErrorCode("23505") ) type transactionBody func(tx *sql.Tx) // runTransaction(db, body) wraps a transaction. // // The body function should panic on any error, using the error as the parameter to panic func runTransaction(db *sql.DB, body transactionBody) (err error) { tx, err := db.Begin() if err != nil { log.Printf("could not start a transaction. %v", err) return err } defer func() { if r := recover(); r != nil { tx.Rollback() if _, ok := r.(runtime.Error); ok { panic(r) }
package pqerror import ( "github.com/lib/pq" ) const ( CodeSuccessfulCompletion = pq.ErrorCode("00000") CodeWarning = pq.ErrorCode("01000") CodeWarningDynamicResultSetsReturned = pq.ErrorCode("0100C") CodeWarningImplicitZeroBitPadding = pq.ErrorCode("01008") CodeWarningNullValueEliminatedInSetFunction = pq.ErrorCode("01003") CodeWarningPrivilegeNotGranted = pq.ErrorCode("01007") CodeWarningPrivilegeNotRevoked = pq.ErrorCode("01006") CodeWarningStringDataRightTruncation = pq.ErrorCode("01004") CodeWarningDeprecatedFeature = pq.ErrorCode("01P01") CodeNoData = pq.ErrorCode("02000") CodeNoDataNoAdditionalDynamicResultSetsReturned = pq.ErrorCode("02001") CodeSqlStatementNotYetComplete = pq.ErrorCode("03000") CodeConnectionException = pq.ErrorCode("08000") CodeConnectionExceptionConnectionDoesNotExist = pq.ErrorCode("08003") CodeConnectionExceptionConnectionFailure = pq.ErrorCode("08006") CodeConnectionExceptionSqlclientUnableToEstablishSqlconnection = pq.ErrorCode("08001") CodeConnectionExceptionSqlserverRejectedEstablishmentOfSqlconnection = pq.ErrorCode("08004") CodeConnectionExceptionTransactionResolutionUnknown = pq.ErrorCode("08007") CodeConnectionExceptionProtocolViolation = pq.ErrorCode("08P01")
"fmt" "testing" "github.com/go-kit/kit/log" "github.com/lib/pq" "github.com/piotrkowalczuk/sklog" "github.com/piotrkowalczuk/sklog/ctxpq" "github.com/stretchr/testify/assert" ) var ( pqErrors = []*pq.Error{ &pq.Error{Message: "pq_test: example pq errror"}, &pq.Error{ Message: "pq_test: example pq errror", Code: pq.ErrorCode("01000"), Table: "example_table", }, } genericErrors = []error{ errors.New("ctxpq_test: example generic error"), } ) func TestNewContextErrorGeneric(t *testing.T) { sklog.SetContextErrorFunc(ctxpq.NewContextErrorGeneric) b := bytes.NewBuffer(nil) l := log.NewJSONLogger(b) for _, e := range pqErrors {