Show errors except RecordNotFound error by default for easy debug

This commit is contained in:
Jinzhu 2013-12-17 19:16:03 +08:00
parent dfc4194162
commit 690cb1430c
5 changed files with 21 additions and 13 deletions

3
do.go
View File

@ -262,10 +262,13 @@ func (s *Do) prepareUpdateSql(include_self bool) {
} }
if include_self { if include_self {
data := s.model.reflectData()
if data.CanAddr() {
for key, value := range s.model.columnsAndValues("update") { for key, value := range s.model.columnsAndValues("update") {
sqls = append(sqls, fmt.Sprintf("%v = %v", s.quote(key), s.addToVars(value))) sqls = append(sqls, fmt.Sprintf("%v = %v", s.quote(key), s.addToVars(value)))
} }
} }
}
s.setSql(fmt.Sprintf( s.setSql(fmt.Sprintf(
"UPDATE %v SET %v %v", "UPDATE %v SET %v %v",

View File

@ -895,7 +895,7 @@ func TestUpdate(t *testing.T) {
t.Errorf("Product 456 should be changed to 789") t.Errorf("Product 456 should be changed to 789")
} }
if db.Model(&product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil { if db.Model(product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
t.Error("No error should raise when update with CamelCase") t.Error("No error should raise when update with CamelCase")
} }

View File

@ -16,7 +16,7 @@ type DB struct {
tagIdentifier string tagIdentifier string
singularTable bool singularTable bool
logger Logger logger Logger
logMode bool logMode int
} }
func Open(driver, source string) (db DB, err error) { func Open(driver, source string) (db DB, err error) {
@ -40,7 +40,11 @@ func (s *DB) SetLogger(l Logger) {
} }
func (s *DB) LogMode(b bool) *DB { func (s *DB) LogMode(b bool) *DB {
s.logMode = b if b {
s.logMode = 2
} else {
s.logMode = 1
}
return s return s
} }

View File

@ -127,14 +127,9 @@ func (m *Model) columnsHasValue(operation string) (fields []*Field) {
} }
func (m *Model) updatedColumnsAndValues(values map[string]interface{}, ignore_protected_attrs bool) (results map[string]interface{}, any_updated bool) { func (m *Model) updatedColumnsAndValues(values map[string]interface{}, ignore_protected_attrs bool) (results map[string]interface{}, any_updated bool) {
if m.data == nil {
return values, true
}
data := m.reflectData() data := m.reflectData()
if !data.CanAddr() { if !data.CanAddr() {
m.do.err(errors.New("Can't take address of the object")) return values, true
return
} }
for key, value := range values { for key, value := range values {

View File

@ -34,8 +34,14 @@ func (s *DB) do(data interface{}) *Do {
func (s *DB) err(err error) error { func (s *DB) err(err error) error {
if err != nil { if err != nil {
s.Error = err s.Error = err
if s.logMode == 0 {
if err != RecordNotFound {
go fmt.Println(err)
}
} else {
s.warn(err) s.warn(err)
} }
}
return err return err
} }
@ -44,7 +50,7 @@ func (s *DB) hasError() bool {
} }
func (s *DB) print(level string, v ...interface{}) { func (s *DB) print(level string, v ...interface{}) {
if s.logMode || level == "debug" { if s.logMode == 2 || level == "debug" {
if _, ok := s.parent.logger.(Logger); !ok { if _, ok := s.parent.logger.(Logger); !ok {
fmt.Println("logger haven't been set, using os.Stdout") fmt.Println("logger haven't been set, using os.Stdout")
s.parent.logger = default_logger s.parent.logger = default_logger