Skip to content

Commit

Permalink
Handle errors when uploading files
Browse files Browse the repository at this point in the history
  • Loading branch information
wolveix committed Jun 26, 2024
1 parent 31d96d5 commit a5170af
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,22 @@ func (c *UserContext) CreateFiles(uploadToPath string, filePaths ...string) erro
var err error
filePath, err = filepath.Abs(filePath)
if err != nil {
continue
return fmt.Errorf("failed to resolve file: %w", err)
}

file, _ := os.Open(filePath)
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

part, _ := writer.CreateFormFile("file"+cast.ToString(counter), filepath.Base(file.Name()))
part, err := writer.CreateFormFile("file"+cast.ToString(counter), filepath.Base(file.Name()))
if err != nil {
return fmt.Errorf("failed to create file in form: %w", err)
}

if _, err = io.Copy(part, file); err != nil {
return fmt.Errorf("failed to create file: %v", err)
return fmt.Errorf("failed to create file: %w", err)
}
}

Expand Down

0 comments on commit a5170af

Please sign in to comment.