From 690cb1430c2e27011324c51826301a7daf728e65 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 17 Dec 2013 19:16:03 +0800 Subject: [PATCH] Show errors except RecordNotFound error by default for easy debug --- do.go | 7 +++++-- gorm_test.go | 2 +- main.go | 8 ++++++-- model.go | 7 +------ private.go | 10 ++++++++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/do.go b/do.go index 1a695915..5c1f1886 100644 --- a/do.go +++ b/do.go @@ -262,8 +262,11 @@ func (s *Do) prepareUpdateSql(include_self bool) { } if include_self { - for key, value := range s.model.columnsAndValues("update") { - sqls = append(sqls, fmt.Sprintf("%v = %v", s.quote(key), s.addToVars(value))) + data := s.model.reflectData() + if data.CanAddr() { + for key, value := range s.model.columnsAndValues("update") { + sqls = append(sqls, fmt.Sprintf("%v = %v", s.quote(key), s.addToVars(value))) + } } } diff --git a/gorm_test.go b/gorm_test.go index 31e0480d..08b3bab0 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -895,7 +895,7 @@ func TestUpdate(t *testing.T) { 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") } diff --git a/main.go b/main.go index cfda79c4..f14cab56 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ type DB struct { tagIdentifier string singularTable bool logger Logger - logMode bool + logMode int } 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 { - s.logMode = b + if b { + s.logMode = 2 + } else { + s.logMode = 1 + } return s } diff --git a/model.go b/model.go index c21a6bae..d5fcf5ff 100644 --- a/model.go +++ b/model.go @@ -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) { - if m.data == nil { - return values, true - } - data := m.reflectData() if !data.CanAddr() { - m.do.err(errors.New("Can't take address of the object")) - return + return values, true } for key, value := range values { diff --git a/private.go b/private.go index 541a0d54..f87d4cc8 100644 --- a/private.go +++ b/private.go @@ -34,7 +34,13 @@ func (s *DB) do(data interface{}) *Do { func (s *DB) err(err error) error { if err != nil { s.Error = err - s.warn(err) + if s.logMode == 0 { + if err != RecordNotFound { + go fmt.Println(err) + } + } else { + s.warn(err) + } } return err } @@ -44,7 +50,7 @@ func (s *DB) hasError() bool { } 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 { fmt.Println("logger haven't been set, using os.Stdout") s.parent.logger = default_logger