Skip to content

Commit 9ff1ea1

Browse files
committed
Adds catches for errors when sending report
1 parent 4483616 commit 9ff1ea1

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

reportserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ func SendReport(stop chan bool) {
135135
logger.Printf("Could not remove archive '%s'.", ArchiveDestination())
136136
}
137137
if err := zipper.ZipDir(ArchiveOrigin(), ArchiveDestination()); err != nil {
138+
logger.Printf("error archiving the reports directory.\n%s", err.Error())
138139
return
139140
}
140141
reportPath := env.GetReportServerUrl()
141142
err := sender.SendArchive(reportPath, ArchiveDestination())
142143
if err != nil {
143-
logger.Printf(fmt.Sprintf("Could not send the archive from '%s' to '%s'\n %s", ArchiveDestination(), reportPath, err))
144+
logger.Printf(fmt.Sprintf("Could not send the archive from '%s' to '%s'\n%s", ArchiveDestination(), reportPath, err.Error()))
144145
} else {
145-
fmt.Printf("Successfully sent html-report to reportserver => %s", reportPath+"/report.html\n")
146+
fmt.Printf("Successfully sent html-report to reportserver => %s", reportPath+"/report.html")
146147
}
147148
}
148149

sender/sender.go

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,34 @@ func SendArchive(url, filePath string) (err error) {
1515
writer := multipart.NewWriter(&b)
1616
// Add your image file
1717
file, err := os.Open(filePath)
18+
defer func() {
19+
err := file.Close()
20+
if err != nil {
21+
return
22+
}
23+
}()
1824
if err != nil {
19-
return
25+
return fmt.Errorf("error opening the file '%s'", filePath)
2026
}
21-
fi, err := file.Stat()
27+
fileStat, err := file.Stat()
2228
if err != nil {
23-
return err
29+
return fmt.Errorf("could not get file information")
2430
}
25-
26-
defer file.Close()
27-
28-
fw, err := writer.CreateFormFile("file", fi.Name())
31+
formWriter, err := writer.CreateFormFile("file", fileStat.Name())
2932
if err != nil {
30-
return err
33+
return fmt.Errorf("error create a form writer")
3134
}
32-
if _, err = io.Copy(fw, file); err != nil {
33-
return err
35+
if _, err = io.Copy(formWriter, file); err != nil {
36+
return fmt.Errorf("error copying file '%s' to form writer", file.Name())
3437
}
35-
36-
// Add the other fields
37-
38-
//if fw, err = w.CreateFormField("key"); err != nil {
39-
// return
40-
//}
41-
//if _, err = fw.Write([]byte("KEY")); err != nil {
42-
// return
43-
//}
44-
38+
// add auto unzip param to request
4539
_ = writer.WriteField("unzip", "true")
4640

47-
// Don't forget to close the multipart writer.
41+
// close the multipart writer.
4842
// If you don't close it, your request will be missing the terminating boundary.
4943
if err = writer.Close(); err != nil {
5044
return err
5145
}
52-
5346
// Now that you have a form, you can submit it to your handler.
5447
req, err := http.NewRequest("POST", url, &b)
5548
if err != nil {
@@ -67,16 +60,7 @@ func SendArchive(url, filePath string) (err error) {
6760

6861
// Check the response
6962
if res.StatusCode != http.StatusOK {
70-
err = fmt.Errorf("bad status: %s", res.Status)
63+
return fmt.Errorf("bad status: %s", res.Status)
7164
}
72-
73-
RemoveArchive(filePath)
74-
7565
return
7666
}
77-
78-
func RemoveArchive(filePath string) {
79-
if err := os.Remove(filePath); err != nil {
80-
return
81-
}
82-
}

zipper/zipper.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package zipper
22

33
import (
44
"archive/zip"
5+
"fmt"
56
"io"
67
"os"
78
"path/filepath"
@@ -12,14 +13,14 @@ func ZipDir(source, target string) error {
1213

1314
// check if sourceDir exists
1415
if _, err := os.Stat(source); os.IsNotExist(err) {
15-
return err
16+
return fmt.Errorf("source directory '%s' does not exist", source)
1617
}
17-
18+
// get absolute path of source directory
1819
source, err := filepath.Abs(source)
1920
if err != nil {
20-
return err
21+
return fmt.Errorf("could not get absolute path for the source directory '%s'", source)
2122
}
22-
23+
// create a zip file at target
2324
zipFile, err := os.Create(target)
2425
defer func() {
2526
err := zipFile.Close()
@@ -28,32 +29,29 @@ func ZipDir(source, target string) error {
2829
}
2930
}()
3031
if err != nil {
31-
return err
32+
return fmt.Errorf("could not create the target archive at '%s'", target)
3233
}
33-
34+
// create a new writer for writing into zip
3435
archive := zip.NewWriter(zipFile)
3536
defer func() {
3637
err := archive.Close()
3738
if err != nil {
3839
return
3940
}
4041
}()
41-
42+
// list of files to ignore when adding to zip
4243
ignoreFiles := []string{
4344
"html-report",
4445
".DS_Store",
45-
"report.html",
4646
}
47-
4847
err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
4948
if err != nil {
5049
return err
5150
}
52-
51+
// check file isn't in the ignored list
5352
if contains(ignoreFiles, info.Name()) {
5453
return nil
5554
}
56-
5755
header, err := zip.FileInfoHeader(info)
5856
if err != nil {
5957
return err
@@ -70,7 +68,7 @@ func ZipDir(source, target string) error {
7068
if err != nil {
7169
return err
7270
}
73-
71+
// skip if it's a directory
7472
if info.IsDir() {
7573
return nil
7674
}

zipper/zipper_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package zipper
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestContains(t *testing.T) {
8+
test := []string{
9+
"hello",
10+
"world",
11+
}
12+
exists := contains(test, "hello")
13+
if !exists {
14+
t.Errorf("contains was incorrect")
15+
}
16+
exists = contains(test, "invalid")
17+
if exists {
18+
t.Errorf("contains was incorrect")
19+
}
20+
}

0 commit comments

Comments
 (0)