Better DB error handling
Cette révision appartient à :
Parent
c5e226c7a6
révision
c46c386981
6 fichiers modifiés avec 62 ajouts et 31 suppressions
5
main.go
5
main.go
|
@ -55,7 +55,10 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.CheckError(err)
|
log.CheckError(err)
|
||||||
}
|
}
|
||||||
db.ORM, _ = db.GormInit(conf)
|
db.ORM, err = db.GormInit(conf)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err.Error())
|
||||||
|
}
|
||||||
initI18N()
|
initI18N()
|
||||||
go signals.Handle()
|
go signals.Handle()
|
||||||
if len(config.TorrentFileStorage) > 0 {
|
if len(config.TorrentFileStorage) > 0 {
|
||||||
|
|
|
@ -104,6 +104,10 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
Date: time.Now(),
|
Date: time.Now(),
|
||||||
Filesize: 0,
|
Filesize: 0,
|
||||||
Description: string(b.Description)}
|
Description: string(b.Description)}
|
||||||
db.ORM.Create(&torrent)
|
err = db.ORM.Create(&torrent).Error
|
||||||
|
if err != nil {
|
||||||
|
util.SendError(w, err, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Printf("%+v\n", torrent)
|
fmt.Printf("%+v\n", torrent)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
"github.com/ewhal/nyaa/service/captcha"
|
"github.com/ewhal/nyaa/service/captcha"
|
||||||
"github.com/ewhal/nyaa/service/user"
|
"github.com/ewhal/nyaa/service/user"
|
||||||
|
"github.com/ewhal/nyaa/util"
|
||||||
"github.com/ewhal/nyaa/util/languages"
|
"github.com/ewhal/nyaa/util/languages"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
@ -42,7 +43,11 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
|
Filesize: uploadForm.Filesize, // FIXME: should set to NULL instead of 0
|
||||||
Description: uploadForm.Description,
|
Description: uploadForm.Description,
|
||||||
UploaderId: user.Id}
|
UploaderId: user.Id}
|
||||||
db.ORM.Create(&torrent)
|
err = db.ORM.Create(&torrent).Error
|
||||||
|
if err != nil {
|
||||||
|
util.SendError(w, err, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Printf("%+v\n", torrent)
|
fmt.Printf("%+v\n", torrent)
|
||||||
url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.Id), 10))
|
url, err := Router.Get("view_torrent").URL("id", strconv.FormatUint(uint64(torrent.Id), 10))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
"github.com/ewhal/nyaa/service/captcha"
|
"github.com/ewhal/nyaa/service/captcha"
|
||||||
"github.com/ewhal/nyaa/service/torrent"
|
"github.com/ewhal/nyaa/service/torrent"
|
||||||
|
"github.com/ewhal/nyaa/util"
|
||||||
"github.com/ewhal/nyaa/util/languages"
|
"github.com/ewhal/nyaa/util/languages"
|
||||||
"github.com/ewhal/nyaa/util/log"
|
"github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
@ -47,11 +48,15 @@ func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
idNum_, err := strconv.Atoi(id)
|
idNum_, err := strconv.Atoi(id)
|
||||||
var idNum uint = uint(idNum_)
|
var idNum uint = uint(idNum_)
|
||||||
var userId uint = 0
|
var userId uint = 0
|
||||||
if (currentUser.Id > 0) {
|
if currentUser.Id > 0 {
|
||||||
userId = currentUser.Id
|
userId = currentUser.Id
|
||||||
}
|
}
|
||||||
comment := model.Comment{TorrentId: idNum, UserId: userId, Content: content, CreatedAt: time.Now()}
|
comment := model.Comment{TorrentId: idNum, UserId: userId, Content: content, CreatedAt: time.Now()}
|
||||||
db.ORM.Create(&comment)
|
err = db.ORM.Create(&comment).Error
|
||||||
|
if err != nil {
|
||||||
|
util.SendError(w, err, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
url, err := Router.Get("view_torrent").URL("id", id)
|
url, err := Router.Get("view_torrent").URL("id", id)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -23,48 +23,61 @@ type WhereParams struct {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// don't need raw SQL once we get MySQL
|
// don't need raw SQL once we get MySQL
|
||||||
func GetFeeds() []model.Feed {
|
func GetFeeds() (result []model.Feed, err error) {
|
||||||
result := make([]model.Feed, 0, 50)
|
result = make([]model.Feed, 0, 50)
|
||||||
rows, err := db.ORM.DB().
|
rows, err := db.ORM.DB().
|
||||||
Query(
|
Query(
|
||||||
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
|
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
|
||||||
"ORDER BY `timestamp` desc LIMIT 50")
|
"ORDER BY `timestamp` desc LIMIT 50")
|
||||||
if err == nil {
|
if err != nil {
|
||||||
for rows.Next() {
|
return nil, err
|
||||||
item := model.Feed{}
|
|
||||||
rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
|
|
||||||
magnet := util.InfoHashToMagnet(strings.TrimSpace(item.Hash), item.Name, config.Trackers...)
|
|
||||||
item.Magnet = magnet
|
|
||||||
// memory hog
|
|
||||||
result = append(result, item)
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
}
|
}
|
||||||
return result
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
item := model.Feed{}
|
||||||
|
err = rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
magnet := util.InfoHashToMagnet(strings.TrimSpace(item.Hash), item.Name, config.Trackers...)
|
||||||
|
item.Magnet = magnet
|
||||||
|
// memory hog
|
||||||
|
result = append(result, item)
|
||||||
|
}
|
||||||
|
err = rows.Err()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTorrentById(id string) (model.Torrents, error) {
|
func GetTorrentById(id string) (torrent model.Torrents, err error) {
|
||||||
var torrent model.Torrents
|
|
||||||
id_int, err := strconv.Atoi(id)
|
id_int, err := strconv.Atoi(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return torrent, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := db.ORM.Where("torrent_id = ?", id).Preload("Comments")
|
tmp := db.ORM.Where("torrent_id = ?", id).Preload("Comments")
|
||||||
|
err = tmp.Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if id_int <= config.LastOldTorrentId {
|
if id_int <= config.LastOldTorrentId {
|
||||||
// only preload old comments if they could actually exist
|
// only preload old comments if they could actually exist
|
||||||
tmp = tmp.Preload("OldComments")
|
tmp = tmp.Preload("OldComments")
|
||||||
}
|
}
|
||||||
if tmp.Find(&torrent).RecordNotFound() {
|
if tmp.Find(&torrent).RecordNotFound() {
|
||||||
return torrent, errors.New("Article is not found.")
|
err = errors.New("Article is not found.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// .Preload("Comments.User") doesn't work
|
// .Preload("Comments.User") doesn't work
|
||||||
for i := range torrent.Comments {
|
for i := range torrent.Comments {
|
||||||
torrent.Comments[i].User = new(model.User)
|
torrent.Comments[i].User = new(model.User)
|
||||||
db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User)
|
err = db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return torrent, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, err error) {
|
func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, err error) {
|
||||||
|
@ -77,7 +90,9 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (torrents []model.Torrents, count int, err error) {
|
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (
|
||||||
|
torrents []model.Torrents, count int, err error,
|
||||||
|
) {
|
||||||
var conditionArray []string
|
var conditionArray []string
|
||||||
if strings.HasPrefix(orderBy, "filesize") {
|
if strings.HasPrefix(orderBy, "filesize") {
|
||||||
// torrents w/ NULL filesize fuck up the sorting on postgres
|
// torrents w/ NULL filesize fuck up the sorting on postgres
|
||||||
|
|
|
@ -4,14 +4,13 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/securecookie"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
formStruct "github.com/ewhal/nyaa/service/user/form"
|
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/db"
|
"github.com/ewhal/nyaa/db"
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
|
formStruct "github.com/ewhal/nyaa/service/user/form"
|
||||||
"github.com/ewhal/nyaa/util/log"
|
"github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/ewhal/nyaa/util/modelHelper"
|
"github.com/ewhal/nyaa/util/modelHelper"
|
||||||
|
"github.com/gorilla/securecookie"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cookieHandler = securecookie.New(
|
var cookieHandler = securecookie.New(
|
||||||
|
@ -149,6 +148,6 @@ func CurrentUser(r *http.Request) (model.User, error) {
|
||||||
if db.ORM.Where("api_token = ?", token).First(&user).RecordNotFound() {
|
if db.ORM.Where("api_token = ?", token).First(&user).RecordNotFound() {
|
||||||
return user, errors.New("User is not found.")
|
return user, errors.New("User is not found.")
|
||||||
}
|
}
|
||||||
db.ORM.Model(&user)
|
err = db.ORM.Model(&user).Error
|
||||||
return user, nil
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket