Add better errors if the sqlite db file is not writable

This commit is contained in:
kolaente 2020-06-23 11:21:42 +02:00
parent 7b31301f09
commit caee123f9d
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 9 additions and 1 deletions

View file

@ -23,6 +23,7 @@ import (
"fmt"
xrc "gitea.com/xorm/xorm-redis-cache"
"net/url"
"os"
"strconv"
"strings"
"time"
@ -166,6 +167,13 @@ func initSqliteEngine() (engine *xorm.Engine, err error) {
path = "./db.db"
}
// Try opening the db file to return a better error message if that does not work
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0)
if err != nil {
return nil, fmt.Errorf("could not open database file [uid=%d, gid=%d]: %s", os.Getuid(), os.Getgid(), err)
}
_ = file.Close() // We directly close the file because we only want to check if it is writable. It will be reopened lazily later by xorm.
return xorm.NewEngine("sqlite3", path)
}

View file

@ -42,7 +42,7 @@ func initMigration(x *xorm.Engine) *xormigrate.Xormigrate {
var err error
x, err = db.CreateDBEngine()
if err != nil {
log.Criticalf("Could not connect to db: %v", err.Error())
log.Fatalf("Could not connect to db: %v", err.Error())
return nil
}
}