Skip to content

Commit

Permalink
Moving errors to a sane location.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed May 15, 2013
1 parent 6eea564 commit f63b759
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 69 deletions.
18 changes: 18 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package db

import (
"errors"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -410,6 +411,23 @@ type MultiFlag bool
type SqlValues []string
type SqlArgs []string

// Error messages
var (
ErrExpectingPointer = errors.New(`Expecting a pointer destination (dst interface{}).`)
ErrExpectingSlicePointer = errors.New(`Expecting a pointer to an slice (dst interface{}).`)
ErrExpectingSliceMapStruct = errors.New(`Expecting a pointer to an slice of maps or structs (dst interface{}).`)
ErrExpectingMapOrStruct = errors.New(`Expecting either a pointer to a map or a pointer to a struct.`)
ErrNoMoreRows = errors.New(`There are no more rows in this result set.`)
ErrNotConnected = errors.New(`You're currently not connected.`)
ErrMissingDatabaseName = errors.New(`Missing a database name.`)
ErrCollectionDoesNotExists = errors.New(`Collection does not exists.`)
ErrSockerOrHost = errors.New(`You can connect either to a socket or a host but not both.`)
ErrQueryLimitParam = errors.New(`A query can accept only one db.Limit() parameter.`)
ErrQuerySortParam = errors.New(`A query can accept only one db.Sort{} parameter.`)
ErrQueryOffsetParam = errors.New(`A query can accept only one db.Offset() parameter.`)
ErrMissingConditions = errors.New(`Missing selector conditions.`)
)

// Registered wrappers.
var wrappers = make(map[string]Database)

Expand Down
6 changes: 3 additions & 3 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ package mongo

import (
"fmt"
"menteslibres.net/gosexy/db"
"labix.org/v2/mgo"
"menteslibres.net/gosexy/db"
"net/url"
"time"
)
Expand Down Expand Up @@ -84,7 +84,7 @@ func (self *Source) Collection(name string) (db.Collection, error) {
col.SetName = name

if col.Exists() == false {
err = fmt.Errorf("Collection %s does not exists.", name)
err = db.ErrCollectionDoesNotExists
}

return col, err
Expand Down Expand Up @@ -131,7 +131,7 @@ func (self *Source) Open() error {
self.session, err = mgo.DialWithTimeout(connURL.String(), 5*time.Second)

if err != nil {
return fmt.Errorf("Could not connect to %s: %s.", self.config.Host, err.Error())
return err
}

if self.config.Database != "" {
Expand Down
5 changes: 2 additions & 3 deletions mongo/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
package mongo

import (
"errors"
"labix.org/v2/mgo"
"menteslibres.net/gosexy/db"
"menteslibres.net/gosexy/db/util"
"labix.org/v2/mgo"
)

type Result struct {
Expand Down Expand Up @@ -61,7 +60,7 @@ func (self *Result) All(dst interface{}) error {
func (self *Result) Next(dst interface{}) error {

if self.iter.Next(dst) == false {
return errors.New("No more results.")
return db.ErrNoMoreRows
}

if self.iter.Err() != nil {
Expand Down
9 changes: 4 additions & 5 deletions mysql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package mysql

import (
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
"menteslibres.net/gosexy/db"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (self *Table) Query(terms ...interface{}) (db.Result, error) {
if queryChunks.Limit == "" {
queryChunks.Limit = fmt.Sprintf("LIMIT %d", v)
} else {
return nil, errors.New("A query can accept only one db.Limit() parameter.")
return nil, db.ErrQueryLimitParam
}
case db.Sort:
if queryChunks.Sort == "" {
Expand All @@ -73,13 +72,13 @@ func (self *Table) Query(terms ...interface{}) (db.Result, error) {
}
queryChunks.Sort = fmt.Sprintf("ORDER BY %s", strings.Join(sortChunks, ", "))
} else {
return nil, errors.New("A query can accept only one db.Sort{} parameter.")
return nil, db.ErrQuerySortParam
}
case db.Offset:
if queryChunks.Offset == "" {
queryChunks.Offset = fmt.Sprintf("OFFSET %d", v)
} else {
return nil, errors.New("A query can accept only one db.Offset() parameter.")
return nil, db.ErrQueryOffsetParam
}
case db.Fields:
queryChunks.Fields = append(queryChunks.Fields, v...)
Expand Down Expand Up @@ -264,7 +263,7 @@ func (self *Table) Update(selector interface{}, update interface{}) error {
selectorConds, selectorArgs := self.compileConditions(selector)

if selectorConds == "" {
return errors.New("Received no conditions.")
return db.ErrMissingConditions
}

fields, values, err := self.FieldValues(update, toInternal)
Expand Down
13 changes: 6 additions & 7 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package mysql

import (
"database/sql"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
"menteslibres.net/gosexy/db"
Expand Down Expand Up @@ -113,7 +112,7 @@ func (self *Source) Name() string {
// Wraps sql.DB.QueryRow
func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) {
if self.session == nil {
return nil, errors.New("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand All @@ -131,7 +130,7 @@ func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) {
// Wraps sql.DB.Query
func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand All @@ -149,7 +148,7 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) {
// Wraps sql.DB.Exec
func (self *Source) doExec(terms ...interface{}) (sql.Result, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand Down Expand Up @@ -201,11 +200,11 @@ func (self *Source) Open() error {
}

if self.config.Database == "" {
return fmt.Errorf("Database name is required.")
return db.ErrMissingDatabaseName
}

if self.config.Socket != "" && self.config.Host != "" {
return errors.New("Socket or Host are mutually exclusive.")
return db.ErrSockerOrHost
}

if self.config.Charset == "" {
Expand Down Expand Up @@ -303,7 +302,7 @@ func (self *Source) Collection(name string) (db.Collection, error) {

// Table exists?
if table.Exists() == false {
return table, fmt.Errorf("Table %s does not exists.", name)
return table, db.ErrCollectionDoesNotExists
}

// Fetching table datatypes and mapping to internal gotypes.
Expand Down
9 changes: 4 additions & 5 deletions postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package postgresql

import (
"errors"
"fmt"
"menteslibres.net/gosexy/db"
"menteslibres.net/gosexy/db/util/sqlutil"
Expand Down Expand Up @@ -53,7 +52,7 @@ func (self *Table) Query(terms ...interface{}) (db.Result, error) {
if queryChunks.Limit == "" {
queryChunks.Limit = fmt.Sprintf("LIMIT %d", v)
} else {
return nil, errors.New("A query can accept only one db.Limit() parameter.")
return nil, db.ErrQueryLimitParam
}
case db.Sort:
if queryChunks.Sort == "" {
Expand All @@ -72,13 +71,13 @@ func (self *Table) Query(terms ...interface{}) (db.Result, error) {
}
queryChunks.Sort = fmt.Sprintf("ORDER BY %s", strings.Join(sortChunks, ", "))
} else {
return nil, errors.New("A query can accept only one db.Sort{} parameter.")
return nil, db.ErrQuerySortParam
}
case db.Offset:
if queryChunks.Offset == "" {
queryChunks.Offset = fmt.Sprintf("OFFSET %d", v)
} else {
return nil, errors.New("A query can accept only one db.Offset() parameter.")
return nil, db.ErrQueryOffsetParam
}
case db.Fields:
queryChunks.Fields = append(queryChunks.Fields, v...)
Expand Down Expand Up @@ -287,7 +286,7 @@ func (self *Table) Update(selector interface{}, update interface{}) error {
selectorConds, selectorArgs := self.compileConditions(selector)

if selectorConds == "" {
return errors.New("Received no conditions.")
return db.ErrMissingConditions
}

fields, values, err := self.FieldValues(update, toInternal)
Expand Down
15 changes: 7 additions & 8 deletions postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package postgresql

import (
"database/sql"
"errors"
"fmt"
_ "github.com/xiam/gopostgresql"
"menteslibres.net/gosexy/db"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (self *Source) Name() string {
// Wraps sql.DB.QueryRow
func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand All @@ -132,7 +131,7 @@ func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) {
// Wraps sql.DB.Query
func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand All @@ -154,7 +153,7 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) {
// Wraps sql.DB.Exec
func (self *Source) doExec(terms ...interface{}) (sql.Result, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand Down Expand Up @@ -209,11 +208,11 @@ func (self *Source) Open() error {
}

if self.config.Database == "" {
return fmt.Errorf("Database name is required.")
return db.ErrMissingDatabaseName
}

if self.config.Socket != "" && self.config.Host != "" {
return errors.New("Socket or Host are mutually exclusive.")
return db.ErrSockerOrHost
}

var conn string
Expand All @@ -227,7 +226,7 @@ func (self *Source) Open() error {
self.session, err = sql.Open("postgres", conn)

if err != nil {
return fmt.Errorf("Could not connect to %s: %s", self.config.Host, err.Error())
return err
}

return nil
Expand Down Expand Up @@ -307,7 +306,7 @@ func (self *Source) Collection(name string) (db.Collection, error) {

// Table exists?
if table.Exists() == false {
return table, fmt.Errorf("Table %s does not exists.", name)
return table, db.ErrCollectionDoesNotExists
}

// Fetching table datatypes and mapping to internal gotypes.
Expand Down
9 changes: 4 additions & 5 deletions sqlite/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package sqlite

import (
"errors"
"fmt"
"menteslibres.net/gosexy/db"
"menteslibres.net/gosexy/db/util/sqlutil"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (self *Table) Query(terms ...interface{}) (db.Result, error) {
if queryChunks.Limit == "" {
queryChunks.Limit = fmt.Sprintf("LIMIT %d", v)
} else {
return nil, errors.New("A query can accept only one db.Limit() parameter.")
return nil, db.ErrQueryLimitParam
}
case db.Sort:
if queryChunks.Sort == "" {
Expand All @@ -74,13 +73,13 @@ func (self *Table) Query(terms ...interface{}) (db.Result, error) {
}
queryChunks.Sort = fmt.Sprintf("ORDER BY %s", strings.Join(sortChunks, ", "))
} else {
return nil, errors.New("A query can accept only one db.Sort{} parameter.")
return nil, db.ErrQuerySortParam
}
case db.Offset:
if queryChunks.Offset == "" {
queryChunks.Offset = fmt.Sprintf("OFFSET %d", v)
} else {
return nil, errors.New("A query can accept only one db.Offset() parameter.")
return nil, ErrQueryOffsetParam
}
case db.Fields:
queryChunks.Fields = append(queryChunks.Fields, v...)
Expand Down Expand Up @@ -265,7 +264,7 @@ func (self *Table) Update(selector interface{}, update interface{}) error {
selectorConds, selectorArgs := self.compileConditions(selector)

if selectorConds == "" {
return errors.New("Received no conditions.")
return db.ErrMissingConditions
}

fields, values, err := self.FieldValues(update, toInternal)
Expand Down
13 changes: 6 additions & 7 deletions sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package sqlite

import (
"database/sql"
"errors"
"fmt"
_ "github.com/xiam/gosqlite3"
"menteslibres.net/gosexy/db"
Expand Down Expand Up @@ -115,7 +114,7 @@ func (self *Source) Name() string {
func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) {

if self.session == nil {
return nil, errors.New("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand All @@ -135,7 +134,7 @@ func (self *Source) doQueryRow(terms ...interface{}) (*sql.Row, error) {
*/
func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand All @@ -155,7 +154,7 @@ func (self *Source) doQuery(terms ...interface{}) (*sql.Rows, error) {
*/
func (self *Source) doExec(terms ...interface{}) (sql.Result, error) {
if self.session == nil {
return nil, fmt.Errorf("You're currently not connected.")
return nil, db.ErrNotConnected
}

chunks := sqlCompile(terms)
Expand Down Expand Up @@ -193,13 +192,13 @@ func (self *Source) Open() error {
var err error

if self.config.Database == "" {
return fmt.Errorf("Missing database path.")
return db.ErrMissingDatabaseName
}

self.session, err = sql.Open("sqlite3", self.config.Database)

if err != nil {
return fmt.Errorf("Could not open %s: %s", self.config.Database, err.Error())
return err
}

return nil
Expand Down Expand Up @@ -282,7 +281,7 @@ func (self *Source) Collection(name string) (db.Collection, error) {

// Table exists?
if table.Exists() == false {
return table, fmt.Errorf("Table %s does not exists.", name)
return table, db.ErrCollectionDoesNotExists
}

// Fetching table datatypes and mapping to internal gotypes.
Expand Down
Loading

0 comments on commit f63b759

Please sign in to comment.