Revise err handling for CSV storage constructor

NewFileCSVStorage() now returns an error besides the storage struct itself, which is
logged and returned instead of calling log.Fatal() which was causing the engine to
crash.

Fixed compilation errors by creating the CSVStorage separately and passing it as an
argument to the TpReader constructor.
This commit is contained in:
ionutboangiu
2023-05-22 07:10:07 -04:00
parent 12039b9d93
commit f2201f3e62
7 changed files with 47 additions and 16 deletions

View File

@@ -398,7 +398,11 @@ func StopStartEngine(cfgPath string, waitEngine int) (*exec.Cmd, error) {
func LoadTariffPlanFromFolder(tpPath, timezone string, dm *DataManager, disable_reverse bool,
cacheConns, schedConns []string) error {
loader, err := NewTpReader(dm.dataDB, NewFileCSVStorage(utils.CSVSep, tpPath), "",
csvStorage, err := NewFileCSVStorage(utils.CSVSep, tpPath)
if err != nil {
return utils.NewErrServerError(err)
}
loader, err := NewTpReader(dm.dataDB, csvStorage, "",
timezone, cacheConns, schedConns, false)
if err != nil {
return utils.NewErrServerError(err)

View File

@@ -100,10 +100,10 @@ func NewCSVStorage(sep rune,
}
// NewFileCSVStorage returns a csv storage that uses all files from the folder
func NewFileCSVStorage(sep rune, dataPath string) *CSVStorage {
func NewFileCSVStorage(sep rune, dataPath string) (*CSVStorage, error) {
allFoldersPath, err := getAllFolders(dataPath)
if err != nil {
log.Fatal(err)
return nil, fmt.Errorf("failed to retrieve the folders from data path '%s': %s", dataPath, err.Error())
}
destinationsPaths := appendName(allFoldersPath, utils.DestinationsCsv)
timingsPaths := appendName(allFoldersPath, utils.TimingsCsv)
@@ -146,7 +146,7 @@ func NewFileCSVStorage(sep rune, dataPath string) *CSVStorage {
chargersPaths,
dispatcherprofilesPaths,
dispatcherhostsPaths,
)
), nil
}
// NewStringCSVStorage creates a csv storage from strings

View File

@@ -63,7 +63,11 @@ var fileHandlers = map[string]func(*TPCSVImporter, string) error{
}
func (tpImp *TPCSVImporter) Run() error {
tpImp.csvr = NewFileCSVStorage(tpImp.Sep, tpImp.DirPath)
var err error
tpImp.csvr, err = NewFileCSVStorage(tpImp.Sep, tpImp.DirPath)
if err != nil {
return err
}
files, _ := os.ReadDir(tpImp.DirPath)
var withErrors bool
for _, f := range files {
@@ -71,7 +75,7 @@ func (tpImp *TPCSVImporter) Run() error {
if !hasName {
continue
}
if err := fHandler(tpImp, f.Name()); err != nil {
if err = fHandler(tpImp, f.Name()); err != nil {
withErrors = true
utils.Logger.Err(fmt.Sprintf("<TPCSVImporter> Importing file: %s, got error: %s", f.Name(), err.Error()))
}

View File

@@ -134,8 +134,11 @@ func testLoaderITRemoveLoad(t *testing.T) {
t.Error("Failed validating data: ", err.Error())
}
}*/
loader, err = NewTpReader(dataDbCsv, NewFileCSVStorage(utils.CSVSep,
path.Join(*dataDir, "tariffplans", *tpCsvScenario)), "", "",
csvStorage, err := NewFileCSVStorage(utils.CSVSep, path.Join(*dataDir, "tariffplans", *tpCsvScenario))
if err != nil {
t.Fatal(err)
}
loader, err = NewTpReader(dataDbCsv, csvStorage, "", "",
[]string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)}, nil, false)
if err != nil {
t.Error(err)
@@ -213,8 +216,11 @@ func testLoaderITLoadFromCSV(t *testing.T) {
t.Error("Failed validating data: ", err.Error())
}
}*/
loader, err = NewTpReader(dataDbCsv, NewFileCSVStorage(utils.CSVSep,
path.Join(*dataDir, "tariffplans", *tpCsvScenario)), "", "",
csvStorage, err := NewFileCSVStorage(utils.CSVSep, path.Join(*dataDir, "tariffplans", *tpCsvScenario))
if err != nil {
t.Fatal(err)
}
loader, err = NewTpReader(dataDbCsv, csvStorage, "", "",
[]string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)}, nil, false)
if err != nil {
t.Error(err)