Skip to content

Commit 83b49df

Browse files
committed
Add missing error logs
1 parent 0a2d2d6 commit 83b49df

File tree

6 files changed

+84
-27
lines changed

6 files changed

+84
-27
lines changed

campaigns.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func handleGetCampaigns(c echo.Context) error {
8888

8989
err := app.Queries.QueryCampaigns.Select(&out.Results, id, pq.StringArray(status), query, pg.Offset, pg.Limit)
9090
if err != nil {
91+
app.Logger.Printf("error fetching campaigns: %v", err)
9192
return echo.NewHTTPError(http.StatusInternalServerError,
9293
fmt.Sprintf("Error fetching campaigns: %s", pqErrMsg(err)))
9394
}
@@ -112,6 +113,7 @@ func handleGetCampaigns(c echo.Context) error {
112113

113114
// Lazy load stats.
114115
if err := out.Results.LoadStats(app.Queries.GetCampaignStats); err != nil {
116+
app.Logger.Printf("error fetching campaign stats: %v", err)
115117
return echo.NewHTTPError(http.StatusInternalServerError,
116118
fmt.Sprintf("Error fetching campaign stats: %v", pqErrMsg(err)))
117119
}
@@ -148,6 +150,7 @@ func handlePreviewCampaign(c echo.Context) error {
148150
return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.")
149151
}
150152

153+
app.Logger.Printf("error fetching campaign: %v", err)
151154
return echo.NewHTTPError(http.StatusInternalServerError,
152155
fmt.Sprintf("Error fetching campaign: %s", pqErrMsg(err)))
153156
}
@@ -159,6 +162,7 @@ func handlePreviewCampaign(c echo.Context) error {
159162
// There's no subscriber. Mock one.
160163
sub = dummySubscriber
161164
} else {
165+
app.Logger.Printf("error fetching subscriber: %v", err)
162166
return echo.NewHTTPError(http.StatusInternalServerError,
163167
fmt.Sprintf("Error fetching subscriber: %s", pqErrMsg(err)))
164168
}
@@ -170,13 +174,15 @@ func handlePreviewCampaign(c echo.Context) error {
170174
}
171175

172176
if err := camp.CompileTemplate(app.Manager.TemplateFuncs(camp)); err != nil {
177+
app.Logger.Printf("error compiling template: %v", err)
173178
return echo.NewHTTPError(http.StatusBadRequest,
174179
fmt.Sprintf("Error compiling template: %v", err))
175180
}
176181

177182
// Render the message body.
178183
m := app.Manager.NewMessage(camp, &sub)
179184
if err := m.Render(); err != nil {
185+
app.Logger.Printf("error rendering message: %v", err)
180186
return echo.NewHTTPError(http.StatusBadRequest,
181187
fmt.Sprintf("Error rendering message: %v", err))
182188
}
@@ -219,7 +225,7 @@ func handleCreateCampaign(c echo.Context) error {
219225

220226
uu, err := uuid.NewV4()
221227
if err != nil {
222-
app.Logger.Println("error generating UUID: %v", err)
228+
app.Logger.Printf("error generating UUID: %v", err)
223229
return echo.NewHTTPError(http.StatusInternalServerError, "Error generating UUID")
224230
}
225231

@@ -244,6 +250,7 @@ func handleCreateCampaign(c echo.Context) error {
244250
"There aren't any subscribers in the target lists to create the campaign.")
245251
}
246252

253+
app.Logger.Printf("error creating campaign: %v", err)
247254
return echo.NewHTTPError(http.StatusInternalServerError,
248255
fmt.Sprintf("Error creating campaign: %v", pqErrMsg(err)))
249256
}
@@ -272,6 +279,7 @@ func handleUpdateCampaign(c echo.Context) error {
272279
return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.")
273280
}
274281

282+
app.Logger.Printf("error fetching campaign: %v", err)
275283
return echo.NewHTTPError(http.StatusInternalServerError,
276284
fmt.Sprintf("Error fetching campaign: %s", pqErrMsg(err)))
277285
}
@@ -305,6 +313,7 @@ func handleUpdateCampaign(c echo.Context) error {
305313
o.TemplateID,
306314
o.ListIDs)
307315
if err != nil {
316+
app.Logger.Printf("error updating campaign: %v", err)
308317
return echo.NewHTTPError(http.StatusInternalServerError,
309318
fmt.Sprintf("Error updating campaign: %s", pqErrMsg(err)))
310319
}
@@ -333,6 +342,7 @@ func handleUpdateCampaignStatus(c echo.Context) error {
333342
return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.")
334343
}
335344

345+
app.Logger.Printf("error fetching campaign: %v", err)
336346
return echo.NewHTTPError(http.StatusInternalServerError,
337347
fmt.Sprintf("Error fetching campaign: %s", pqErrMsg(err)))
338348
}
@@ -377,8 +387,9 @@ func handleUpdateCampaignStatus(c echo.Context) error {
377387

378388
res, err := app.Queries.UpdateCampaignStatus.Exec(cm.ID, o.Status)
379389
if err != nil {
390+
app.Logger.Printf("error updating campaign status: %v", err)
380391
return echo.NewHTTPError(http.StatusInternalServerError,
381-
fmt.Sprintf("Error updating campaign: %s", pqErrMsg(err)))
392+
fmt.Sprintf("Error updating campaign status: %s", pqErrMsg(err)))
382393
}
383394

384395
if n, _ := res.RowsAffected(); n == 0 {
@@ -406,6 +417,7 @@ func handleDeleteCampaign(c echo.Context) error {
406417
return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.")
407418
}
408419

420+
app.Logger.Printf("error fetching campaign: %v", err)
409421
return echo.NewHTTPError(http.StatusInternalServerError,
410422
fmt.Sprintf("Error fetching campaign: %s", pqErrMsg(err)))
411423
}
@@ -418,6 +430,7 @@ func handleDeleteCampaign(c echo.Context) error {
418430
}
419431

420432
if _, err := app.Queries.DeleteCampaign.Exec(cm.ID); err != nil {
433+
app.Logger.Printf("error deleting campaign: %v", err)
421434
return echo.NewHTTPError(http.StatusInternalServerError,
422435
fmt.Sprintf("Error deleting campaign: %v", pqErrMsg(err)))
423436
}
@@ -437,6 +450,7 @@ func handleGetRunningCampaignStats(c echo.Context) error {
437450
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
438451
}
439452

453+
app.Logger.Printf("error fetching campaign stats: %v", err)
440454
return echo.NewHTTPError(http.StatusInternalServerError,
441455
fmt.Sprintf("Error fetching campaign stats: %s", pqErrMsg(err)))
442456
} else if len(out) == 0 {
@@ -496,6 +510,7 @@ func handleTestCampaign(c echo.Context) error {
496510
}
497511
var subs models.Subscribers
498512
if err := app.Queries.GetSubscribersByEmails.Select(&subs, req.SubscriberEmails); err != nil {
513+
app.Logger.Printf("error fetching subscribers: %v", err)
499514
return echo.NewHTTPError(http.StatusInternalServerError,
500515
fmt.Sprintf("Error fetching subscribers: %s", pqErrMsg(err)))
501516
} else if len(subs) == 0 {
@@ -508,6 +523,8 @@ func handleTestCampaign(c echo.Context) error {
508523
if err == sql.ErrNoRows {
509524
return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.")
510525
}
526+
527+
app.Logger.Printf("error fetching campaign: %v", err)
511528
return echo.NewHTTPError(http.StatusInternalServerError,
512529
fmt.Sprintf("Error fetching campaign: %s", pqErrMsg(err)))
513530
}
@@ -522,7 +539,8 @@ func handleTestCampaign(c echo.Context) error {
522539
for _, s := range subs {
523540
sub := s
524541
if err := sendTestMessage(&sub, &camp, app); err != nil {
525-
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error sending test: %v", err))
542+
return echo.NewHTTPError(http.StatusBadRequest,
543+
fmt.Sprintf("Error sending test: %v", err))
526544
}
527545
}
528546

@@ -532,12 +550,14 @@ func handleTestCampaign(c echo.Context) error {
532550
// sendTestMessage takes a campaign and a subsriber and sends out a sample campaign message.
533551
func sendTestMessage(sub *models.Subscriber, camp *models.Campaign, app *App) error {
534552
if err := camp.CompileTemplate(app.Manager.TemplateFuncs(camp)); err != nil {
553+
app.Logger.Printf("error compiling template: %v", err)
535554
return fmt.Errorf("Error compiling template: %v", err)
536555
}
537556

538557
// Render the message body.
539558
m := app.Manager.NewMessage(camp, sub)
540559
if err := m.Render(); err != nil {
560+
app.Logger.Printf("error rendering message: %v", err)
541561
return echo.NewHTTPError(http.StatusBadRequest,
542562
fmt.Sprintf("Error rendering message: %v", err))
543563
}

lists.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func handleGetLists(c echo.Context) error {
3939

4040
err := app.Queries.GetLists.Select(&out.Results, listID, pg.Offset, pg.Limit)
4141
if err != nil {
42+
app.Logger.Printf("error fetching lists: %v", err)
4243
return echo.NewHTTPError(http.StatusInternalServerError,
4344
fmt.Sprintf("Error fetching lists: %s", pqErrMsg(err)))
4445
}
@@ -64,7 +65,6 @@ func handleGetLists(c echo.Context) error {
6465
out.Total = out.Results[0].Total
6566
out.Page = pg.Page
6667
out.PerPage = pg.PerPage
67-
6868
return c.JSON(http.StatusOK, okResp{out})
6969
}
7070

@@ -87,7 +87,7 @@ func handleCreateList(c echo.Context) error {
8787

8888
uu, err := uuid.NewV4()
8989
if err != nil {
90-
app.Logger.Println("error generating UUID: %v", err)
90+
app.Logger.Printf("error generating UUID: %v", err)
9191
return echo.NewHTTPError(http.StatusInternalServerError, "Error generating UUID")
9292
}
9393

@@ -100,6 +100,7 @@ func handleCreateList(c echo.Context) error {
100100
o.Type,
101101
o.Optin,
102102
pq.StringArray(normalizeTags(o.Tags))); err != nil {
103+
app.Logger.Printf("error creating list: %v", err)
103104
return echo.NewHTTPError(http.StatusInternalServerError,
104105
fmt.Sprintf("Error creating list: %s", pqErrMsg(err)))
105106
}
@@ -127,8 +128,10 @@ func handleUpdateList(c echo.Context) error {
127128
return err
128129
}
129130

130-
res, err := app.Queries.UpdateList.Exec(id, o.Name, o.Type, o.Optin, pq.StringArray(normalizeTags(o.Tags)))
131+
res, err := app.Queries.UpdateList.Exec(id,
132+
o.Name, o.Type, o.Optin, pq.StringArray(normalizeTags(o.Tags)))
131133
if err != nil {
134+
app.Logger.Printf("error updating list: %v", err)
132135
return echo.NewHTTPError(http.StatusBadRequest,
133136
fmt.Sprintf("Error updating list: %s", pqErrMsg(err)))
134137
}
@@ -163,8 +166,9 @@ func handleDeleteLists(c echo.Context) error {
163166
}
164167

165168
if _, err := app.Queries.DeleteLists.Exec(ids); err != nil {
169+
app.Logger.Printf("error deleting lists: %v", err)
166170
return echo.NewHTTPError(http.StatusInternalServerError,
167-
fmt.Sprintf("Delete failed: %v", err))
171+
fmt.Sprintf("Error deleting: %v", err))
168172
}
169173

170174
return c.JSON(http.StatusOK, okResp{true})

media.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import (
1010
"github.com/labstack/echo"
1111
)
1212

13-
var imageMimes = []string{"image/jpg", "image/jpeg", "image/png", "image/svg", "image/gif"}
14-
1513
const (
1614
thumbPrefix = "thumb_"
1715
thumbnailSize = 90
1816
)
1917

18+
// imageMimes is the list of image types allowed to be uploaded.
19+
var imageMimes = []string{
20+
"image/jpg",
21+
"image/jpeg",
22+
"image/png",
23+
"image/svg",
24+
"image/gif"}
25+
2026
// handleUploadMedia handles media file uploads.
2127
func handleUploadMedia(c echo.Context) error {
2228
var (
@@ -28,25 +34,29 @@ func handleUploadMedia(c echo.Context) error {
2834
return echo.NewHTTPError(http.StatusBadRequest,
2935
fmt.Sprintf("Invalid file uploaded: %v", err))
3036
}
37+
3138
// Validate MIME type with the list of allowed types.
3239
var typ = file.Header.Get("Content-type")
33-
ok := validateMIME(typ, imageMimes)
34-
if !ok {
40+
if ok := validateMIME(typ, imageMimes); !ok {
3541
return echo.NewHTTPError(http.StatusBadRequest,
3642
fmt.Sprintf("Unsupported file type (%s) uploaded.", typ))
3743
}
44+
3845
// Generate filename
3946
fName := generateFileName(file.Filename)
47+
4048
// Read file contents in memory
4149
src, err := file.Open()
4250
if err != nil {
4351
return echo.NewHTTPError(http.StatusBadRequest,
4452
fmt.Sprintf("Error reading file: %s", err))
4553
}
4654
defer src.Close()
55+
4756
// Upload the file.
4857
fName, err = app.Media.Put(fName, typ, src)
4958
if err != nil {
59+
app.Logger.Printf("error uploading file: %v", err)
5060
cleanUp = true
5161
return echo.NewHTTPError(http.StatusInternalServerError,
5262
fmt.Sprintf("Error uploading file: %s", err))
@@ -65,27 +75,30 @@ func handleUploadMedia(c echo.Context) error {
6575
thumbFile, err := createThumbnail(file)
6676
if err != nil {
6777
cleanUp = true
78+
app.Logger.Printf("error resizing image: %v", err)
6879
return echo.NewHTTPError(http.StatusInternalServerError,
69-
fmt.Sprintf("Error opening image for resizing: %s", err))
80+
fmt.Sprintf("Error resizing image: %s", err))
7081
}
7182

7283
// Upload thumbnail.
7384
thumbfName, err := app.Media.Put(thumbPrefix+fName, typ, thumbFile)
7485
if err != nil {
7586
cleanUp = true
87+
app.Logger.Printf("error saving thumbnail: %v", err)
7688
return echo.NewHTTPError(http.StatusInternalServerError,
7789
fmt.Sprintf("Error saving thumbnail: %s", err))
7890
}
7991

8092
uu, err := uuid.NewV4()
8193
if err != nil {
82-
app.Logger.Println("error generating UUID: %v", err)
94+
app.Logger.Printf("error generating UUID: %v", err)
8395
return echo.NewHTTPError(http.StatusInternalServerError, "Error generating UUID")
8496
}
8597

8698
// Write to the DB.
8799
if _, err := app.Queries.InsertMedia.Exec(uu, fName, thumbfName, 0, 0); err != nil {
88100
cleanUp = true
101+
app.Logger.Printf("error inserting uploaded file to db: %v", err)
89102
return echo.NewHTTPError(http.StatusInternalServerError,
90103
fmt.Sprintf("Error saving uploaded file to db: %s", pqErrMsg(err)))
91104
}
@@ -131,6 +144,5 @@ func handleDeleteMedia(c echo.Context) error {
131144

132145
app.Media.Delete(m.Filename)
133146
app.Media.Delete(thumbPrefix + m.Filename)
134-
135147
return c.JSON(http.StatusOK, okResp{true})
136148
}

public.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"database/sql"
56
"html/template"
67
"image"
78
"image/png"
@@ -113,6 +114,7 @@ func handleSubscriptionPage(c echo.Context) error {
113114
makeMsgTpl("Error", "",
114115
`Error processing request. Please retry.`))
115116
}
117+
116118
return c.Render(http.StatusOK, tplMessage,
117119
makeMsgTpl("Unsubscribed", "",
118120
`You have been successfully unsubscribed.`))
@@ -232,7 +234,10 @@ func handleLinkRedirect(c echo.Context) error {
232234

233235
var url string
234236
if err := app.Queries.RegisterLinkClick.Get(&url, linkUUID, campUUID, subUUID); err != nil {
235-
app.Logger.Printf("error fetching redirect link: %s", err)
237+
if err != sql.ErrNoRows {
238+
app.Logger.Printf("error fetching redirect link: %s", err)
239+
}
240+
236241
return c.Render(http.StatusInternalServerError, tplMessage,
237242
makeMsgTpl("Error opening link", "",
238243
"There was an error opening the link. Please try later."))
@@ -269,8 +274,7 @@ func handleSelfExportSubscriberData(c echo.Context) error {
269274
// Is export allowed?
270275
if !app.Constants.Privacy.AllowExport {
271276
return c.Render(http.StatusBadRequest, tplMessage,
272-
makeMsgTpl("Invalid request", "",
273-
"The feature is not available."))
277+
makeMsgTpl("Invalid request", "", "The feature is not available."))
274278
}
275279

276280
// Get the subscriber's data. A single query that gets the profile,
@@ -287,7 +291,8 @@ func handleSelfExportSubscriberData(c echo.Context) error {
287291
// Send the data out to the subscriber as an atachment.
288292
var msg bytes.Buffer
289293
if err := app.NotifTpls.ExecuteTemplate(&msg, notifSubscriberData, data); err != nil {
290-
app.Logger.Printf("error compiling notification template '%s': %v", notifSubscriberData, err)
294+
app.Logger.Printf("error compiling notification template '%s': %v",
295+
notifSubscriberData, err)
291296
return c.Render(http.StatusInternalServerError, tplMessage,
292297
makeMsgTpl("Error preparing data", "",
293298
"There was an error preparing your data. Please try later."))

subimporter/importer.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ func (im *Importer) getStatus() string {
178178
im.RLock()
179179
status := im.status.Status
180180
im.RUnlock()
181-
182181
return status
183182
}
184183

@@ -190,7 +189,6 @@ func (im *Importer) isDone() bool {
190189
s = false
191190
}
192191
im.RUnlock()
193-
194192
return s
195193
}
196194

0 commit comments

Comments
 (0)