fix error handling

This commit is contained in:
2023-03-29 14:59:04 +02:00
parent 0e572f0aa4
commit 50d25fe7b7
14 changed files with 96 additions and 48 deletions

View File

@@ -71,6 +71,9 @@ func AuthValidateOnetimeKey(c *fiber.Ctx, key string, db *Db) (bool, error) {
}
sess, err := Sessionstore.Get(c)
if err != nil {
return false, errors.New("Could not retrieve session from Sessionstore: " + err.Error())
}
// store the result into the session, the 'formid' key tells the
// upload handler that the apicontext it sees is in fact a form id

View File

@@ -58,10 +58,6 @@ func DeleteExpiredUploads(conf *cfg.Config, db *Db) error {
return err
})
if err != nil {
Log("DB error: %s", err.Error())
}
return err
}
@@ -74,7 +70,9 @@ func BackgroundCleaner(conf *cfg.Config, db *Db) chan bool {
for {
select {
case <-ticker.C:
DeleteExpiredUploads(conf, db)
if err := DeleteExpiredUploads(conf, db); err != nil {
Log("Failed to delete eypired uploads: %s", err.Error())
}
case <-done:
ticker.Stop()
return

View File

@@ -112,6 +112,10 @@ func TestDboperation(t *testing.T) {
if tt.upload.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
if err != nil {
t.Errorf("Could not parse time: " + err.Error())
}
tt.upload.Created = common.Timestamp{Time: ts}
// create new upload db object
@@ -162,7 +166,7 @@ func TestDboperation(t *testing.T) {
}
// fetch again, shall return empty
response, err = db.Get(tt.context, tt.id, common.TypeUpload)
_, err = db.Get(tt.context, tt.id, common.TypeUpload)
if err == nil {
t.Errorf("Could fetch upload object again although we deleted it")
}
@@ -171,6 +175,9 @@ func TestDboperation(t *testing.T) {
if tt.form.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
if err != nil {
t.Errorf("Could not parse time: " + err.Error())
}
tt.form.Created = common.Timestamp{Time: ts}
// create new form db object
@@ -221,7 +228,7 @@ func TestDboperation(t *testing.T) {
}
// fetch again, shall return empty
response, err = db.Get(tt.context, tt.id, common.TypeForm)
_, err = db.Get(tt.context, tt.id, common.TypeForm)
if err == nil {
t.Errorf("Could fetch form object again although we deleted it")
}

View File

@@ -114,17 +114,23 @@ func ZipDir(directory, zipfilename string) error {
var wg sync.WaitGroup
wg.Add(1)
failure := make(chan string)
// don't chdir the server itself
go func() {
defer wg.Done()
os.Chdir(directory)
if err := os.Chdir(directory); err != nil {
failure <- "Failed to changedir: " + err.Error()
return
}
newDir, err := os.Getwd()
if err != nil {
failure <- "Failed to get cwd: " + err.Error()
}
if newDir != directory {
err = errors.New("Failed to changedir!")
return
failure <- "Failed to changedir!"
}
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
@@ -171,9 +177,19 @@ func ZipDir(directory, zipfilename string) error {
_, err = io.Copy(headerWriter, f)
return err
})
if err != nil {
failure <- "Failed to zip directory: " + err.Error()
}
}()
wg.Wait()
goterr := <-failure
if goterr != "" {
return errors.New(goterr)
}
return err
}

View File

@@ -82,7 +82,11 @@ func FormCreate(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
Log("Form created with API-Context %s", entry.Context)
// we do this in the background to not thwart the server
go db.Insert(id, entry)
go func() {
if err := db.Insert(id, entry); err != nil {
Log("Failed to insert: " + err.Error())
}
}()
// everything went well so far
res := &common.Response{Forms: []*common.Form{entry}}

View File

@@ -53,7 +53,10 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
var returnUrl string
var formdata Meta
os.MkdirAll(filepath.Join(cfg.StorageDir, id), os.ModePerm)
if err := os.MkdirAll(filepath.Join(cfg.StorageDir, id), os.ModePerm); err != nil {
return JsonStatus(c, fiber.StatusInternalServerError,
"Unable to initialize directories: "+err.Error())
}
// fetch auxiliary form data
form, err := c.MultipartForm()
@@ -114,7 +117,11 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
Log("Uploaded with API-Context %s", entry.Context)
// we do this in the background to not thwart the server
go db.Insert(id, entry)
go func() {
if err := db.Insert(id, entry); err != nil {
Log("Failed to insert: " + err.Error())
}
}()
// everything went well so far
res := &common.Response{Uploads: []*common.Upload{entry}}
@@ -131,7 +138,9 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
if err == nil {
if len(r.Forms) == 1 {
if r.Forms[0].Expire == "asap" {
db.Delete(apicontext, formid)
if err := db.Delete(apicontext, formid); err != nil {
Log("Failed to delete formid %s: %s", formid, err.Error())
}
}
// email notification to form creator
@@ -184,7 +193,11 @@ func UploadFetch(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) err
if _, err := os.Stat(filename); err != nil {
// db entry is there, but file isn't (anymore?)
go db.Delete(apicontext, id)
go func() {
if err := db.Delete(apicontext, id); err != nil {
Log("Unable to delete entry id %s: %s", id, err.Error())
}
}()
return fiber.NewError(404, "No download with that id could be found!")
}
@@ -192,12 +205,14 @@ func UploadFetch(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) err
err = c.Download(filename, file)
if len(shallExpire) > 0 {
if shallExpire[0] == true {
if shallExpire[0] {
go func() {
// check if we need to delete the file now and do it in the background
if upload.Expire == "asap" {
cleanup(filepath.Join(cfg.StorageDir, id))
db.Delete(apicontext, id)
if err := db.Delete(apicontext, id); err != nil {
Log("Unable to delete entry id %s: %s", id, err.Error())
}
}
}()
}