package artifacts
import (
+ "errors"
"io/ioutil"
+ "log"
"os"
"path/filepath"
"strconv"
err = s.downloader.Download(artifact.URI, path, ch)
if err != nil {
- s.db.SetStatus(weles.ArtifactStatusChange{
+ err2 := s.db.SetStatus(weles.ArtifactStatusChange{
Path: path,
NewStatus: weles.ArtifactStatusFAILED,
})
- return "", err
+ if err2 != nil {
+ return "", errors.New(
+ "failed to download artifact: " + err.Error() +
+ " and failed to set artifacts status to failed: " + err2.Error())
+ }
+ return "", errors.New("failed to download artifact: " + err.Error())
}
return path, nil
}
if err != nil {
return "", err
}
- defer f.Close()
+
+ defer func() {
+ if err = f.Close(); err != nil {
+ log.Println("failed to close file")
+ //TODO: aalexanderr log
+ }
+ }()
return weles.ArtifactPath(f.Name()), err
}
// about status change.
func (s *Storage) listenToChanges() {
for change := range s.notifier {
- // TODO handle errors returned by SetStatus
- s.db.SetStatus(change)
+ // Error handled in SetStatus function.
+ _ = s.db.SetStatus(change) //nolint: gas, gosec
}
}
import (
"database/sql"
"errors"
+ "log"
"strings"
"git.tizen.org/tools/weles"
var err error
aDB.handler, err = sql.Open("sqlite3", dbPath)
if err != nil {
- return err
+ return errors.New(dbOpenFail + err.Error())
}
aDB.dbmap = &gorp.DbMap{Db: aDB.handler, Dialect: gorp.SqliteDialect{}}
// Thats why it's done with the use prepareQuery.
trans, err := aDB.dbmap.Begin()
if err != nil {
- return nil, weles.ListInfo{},
- errors.New("Failed to open transaction while filtering " + err.Error())
+ return nil, weles.ListInfo{}, errors.New(whileFilter + dbTransOpenFail + err.Error())
}
queryForTotal, argsForTotal := prepareQuery(filter, sorter, paginator, true, false, 0)
queryForRemaining, argsForRemaining := prepareQuery(filter, sorter, paginator, false, true, 0)
rr, err = aDB.dbmap.SelectInt(queryForRemaining, argsForRemaining...)
if err != nil {
- return nil, weles.ListInfo{}, errors.New("Failed to get remaining records " + err.Error())
+ return nil, weles.ListInfo{}, errors.New(whileFilter + dbRemainingFail + err.Error())
}
tr, err = aDB.dbmap.SelectInt(queryForTotal, argsForTotal...)
if err != nil {
- return nil, weles.ListInfo{}, errors.New("Failed to get total records " + err.Error())
+ return nil, weles.ListInfo{}, errors.New(whileFilter + dbTotalFail + err.Error())
}
// TODO: refactor this file. below is to ignore pagination object when pagination is turned off.
if paginator.Limit == 0 {
queryForData, argsForData := prepareQuery(filter, sorter, paginator, false, false, offset)
_, err = aDB.dbmap.Select(&results, queryForData, argsForData...)
if err != nil {
- return nil, weles.ListInfo{}, err
+ return nil, weles.ListInfo{}, errors.New(whileFilter + dbArtifactInfoFail + err.Error())
}
if err := trans.Commit(); err != nil {
- return nil, weles.ListInfo{},
- errors.New("Failed to commit transaction while filtering " + err.Error())
+ return nil,
+ weles.ListInfo{},
+ errors.New(whileFilter + dbTransCommitFail + err.Error())
}
return results,
return results, nil
}
-// getID fetches ID of an artifact with provided path.
-func (aDB *ArtifactDB) getID(path weles.ArtifactPath) (int64, error) {
- res, err := aDB.dbmap.SelectInt("select ID from artifacts where Path=?", path)
- if err != nil {
- return 0, err
- }
- return res, nil
-}
-
// SetStatus changes artifact's status in ArtifactDB.
func (aDB *ArtifactDB) SetStatus(change weles.ArtifactStatusChange) error {
ai, err := aDB.SelectPath(change.Path)
if err != nil {
- return err
+ log.Println("failed to retrieve artifact based on its path: " + err.Error())
+ return err //TODO: aalexanderr - log error and continue
}
- id, err := aDB.getID(ai.Path)
- if err != nil {
- return err
- }
- ai.ID = id
-
ai.Status = change.NewStatus
- _, err = aDB.dbmap.Update(&ai)
+ if _, err = aDB.dbmap.Update(&ai); err != nil {
+ log.Println("failed to update database" + err.Error())
+ // TODO: aalexanderr - log critical, stop weles gracefully
+ }
return err
}
/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// ArtifactDB's Select().
ErrUnsupportedQueryType = errors.New("unsupported argument type")
)
+
+const (
+ dbOpenFail = "failed to open artifacts database: "
+ whileFilter = "while filtering, "
+ dbTransOpenFail = "failed to open transaction with artifacts database: "
+ dbTransCommitFail = "failed to commit transaction to artifacts database: "
+ dbRemainingFail = "failed to get remaining records count: "
+ dbTotalFail = "failed to get total records count: "
+ dbArtifactInfoFail = "failed to get ArtifactInfo records: "
+)
import (
"fmt"
"io"
+ "log"
"net/http"
"os"
"sync"
if err != nil {
return err
}
- defer resp.Body.Close()
+ defer func() {
+ if erro := resp.Body.Close(); erro != nil {
+ log.Println("failed to close response body after downloading file from: "+string(URI),
+ erro.Error())
+ }
+ }()
if resp.StatusCode != http.StatusOK {
- return fmt.Errorf("server error %v %v", URI, resp.Status)
+ return fmt.Errorf("while downloading: %v server returned %v status code, expected 200 ",
+ URI, resp.Status)
}
file, err := os.Create(string(path))
if err != nil {
return err
}
- defer file.Close()
+
+ defer func() {
+ if erro := file.Close(); erro != nil {
+ log.Println("failed to close file: " + string(path) + " " + erro.Error())
+ }
+ }()
_, err = io.Copy(file, resp.Body)
return err
err := d.getData(URI, path)
if err != nil {
- os.Remove(string(path))
+ if err = os.Remove(string(path)); err != nil {
+ log.Println("failed to remove an artifact: ", path, " due to: "+err.Error())
+ }
change.NewStatus = weles.ArtifactStatusFAILED
} else {
change.NewStatus = weles.ArtifactStatusREADY