Skip to content

Commit 01f5954

Browse files
committed
all: fix errors found by nilaway
1 parent 5957ca4 commit 01f5954

File tree

15 files changed

+127
-34
lines changed

15 files changed

+127
-34
lines changed

batchErrors.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ func (b *Batch) Error(field string, err error, values ...interface{}) error {
8787
return err
8888
}
8989
be := BatchError{
90-
BatchNumber: b.Header.BatchNumber,
91-
BatchType: b.Header.StandardEntryClassCode,
92-
FieldName: field,
93-
Err: err,
90+
FieldName: field,
91+
Err: err,
92+
}
93+
if b != nil {
94+
be.BatchNumber = b.Header.BatchNumber
95+
be.BatchType = b.Header.StandardEntryClassCode
9496
}
9597
// only the first value counts
9698
if len(values) > 0 {

cmd/achcli/describe.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func dumpFiles(paths []string, validateOpts *ach.ValidateOpts) error {
1919
if err != nil {
2020
fmt.Printf("WARN: problem reading %s:\n %v\n\n", paths[i], err)
2121
}
22-
files[i] = f
22+
if f != nil {
23+
files[i] = f
24+
}
2325
}
2426

2527
if *flagMerge {
@@ -38,7 +40,9 @@ func dumpFiles(paths []string, validateOpts *ach.ValidateOpts) error {
3840
if err != nil {
3941
fmt.Printf("ERROR: problem flattening file: %v\n", err)
4042
}
41-
files[i] = file
43+
if file != nil {
44+
files[i] = file
45+
}
4246
}
4347
}
4448

cmd/writeACH/main.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ func write(path string) {
6767

6868
f, err := os.Create(path)
6969
if err != nil {
70-
fmt.Printf("%T: %s", err, err)
70+
fmt.Printf("%T: %v\n", err, err)
71+
return
7172
}
7273

7374
// To create a file
@@ -91,7 +92,11 @@ func write(path string) {
9192
bh.EffectiveEntryDate = time.Now().AddDate(0, 0, 1).Format("060102")
9293
bh.ODFIIdentification = "121042882"
9394

94-
batch, _ := ach.NewBatch(bh)
95+
batch, err := ach.NewBatch(bh)
96+
if err != nil {
97+
fmt.Printf("%T: %v\n", err, err)
98+
return
99+
}
95100

96101
// Create Entry
97102
entrySeq := 0
@@ -121,7 +126,8 @@ func write(path string) {
121126

122127
// Create the batch.
123128
if err := batch.Create(); err != nil {
124-
fmt.Printf("%T: %s", err, err)
129+
fmt.Printf("%T: %v\n", err, err)
130+
return
125131
}
126132

127133
// Add batch to the file
@@ -131,30 +137,35 @@ func write(path string) {
131137
// ensure we have a validated file structure
132138
if file.Validate(); err != nil {
133139
fmt.Printf("Could not validate entire file: %v", err)
140+
return
134141
}
135142

136143
// Create the file
137144
if err := file.Create(); err != nil {
138-
fmt.Printf("%T: %s", err, err)
145+
fmt.Printf("%T: %v\n", err, err)
146+
return
139147
}
140148

141149
// Write to a file
142150
if *flagJson {
143151
// Write in JSON format
144152
if err := json.NewEncoder(f).Encode(file); err != nil {
145-
fmt.Printf("%T: %s", err, err)
153+
fmt.Printf("%T: %v\n", err, err)
154+
return
146155
}
147156
} else {
148157
// Write in ACH plain text format
149158
w := ach.NewWriter(f)
150159
if err := w.Write(file); err != nil {
151-
fmt.Printf("%T: %s", err, err)
160+
fmt.Printf("%T: %v\n", err, err)
161+
return
152162
}
153163
w.Flush()
154164
}
155165

156166
if err := f.Close(); err != nil {
157-
fmt.Println(err.Error())
167+
fmt.Printf("%T: %v\n", err, err)
168+
return
158169
}
159170

160171
fmt.Printf("Wrote %s\n", path)

examples/http/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ func main() {
104104
req.Header.Set("Content-Type", "application/json")
105105

106106
resp, err := http.DefaultClient.Do(req)
107-
if err != nil {
107+
if resp == nil || err != nil {
108108
log.Fatal(err)
109109
}
110-
defer resp.Body.Close()
111-
110+
if resp != nil && resp.Body != nil {
111+
defer resp.Body.Close()
112+
}
112113
if resp.StatusCode == 200 {
113114
log.Printf("File created!")
114115
} else {

file.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ func (f *File) Create() error {
544544

545545
// AddBatch appends a Batch to the ach.File
546546
func (f *File) AddBatch(batch Batcher) []Batcher {
547+
if batch == nil {
548+
return f.Batches
549+
}
547550
if batch.Category() == CategoryNOC {
548551
f.NotificationOfChange = append(f.NotificationOfChange, batch)
549552
}
@@ -984,7 +987,10 @@ func (f *File) SegmentFile(_ *SegmentFileConfiguration) (*File, *File, error) {
984987
}
985988

986989
if f.Batches != nil {
987-
f.segmentFileBatches(creditFile, debitFile)
990+
err := f.segmentFileBatches(creditFile, debitFile)
991+
if err != nil {
992+
return nil, nil, err
993+
}
988994
}
989995

990996
if f.IATBatches != nil {
@@ -1013,7 +1019,7 @@ func (f *File) SegmentFile(_ *SegmentFileConfiguration) (*File, *File, error) {
10131019
return creditFile, debitFile, nil
10141020
}
10151021

1016-
func (f *File) segmentFileBatches(creditFile, debitFile *File) {
1022+
func (f *File) segmentFileBatches(creditFile, debitFile *File) error {
10171023
for _, batch := range f.Batches {
10181024
bh := batch.GetHeader()
10191025

@@ -1030,7 +1036,10 @@ func (f *File) segmentFileBatches(creditFile, debitFile *File) {
10301036

10311037
entries := batch.GetADVEntries()
10321038
for _, entry := range entries {
1033-
segmentFileBatchAddADVEntry(creditBatch, debitBatch, entry)
1039+
err := segmentFileBatchAddADVEntry(creditBatch, debitBatch, entry)
1040+
if err != nil {
1041+
return err
1042+
}
10341043
}
10351044
// Add the Entry to its Batch
10361045
if creditBatch != nil && len(creditBatch.GetADVEntries()) > 0 {
@@ -1054,7 +1063,10 @@ func (f *File) segmentFileBatches(creditFile, debitFile *File) {
10541063

10551064
entries := batch.GetEntries()
10561065
for _, entry := range entries {
1057-
segmentFileBatchAddEntry(creditBatch, debitBatch, entry)
1066+
err := segmentFileBatchAddEntry(creditBatch, debitBatch, entry)
1067+
if err != nil {
1068+
return err
1069+
}
10581070
}
10591071

10601072
if creditBatch != nil && len(creditBatch.GetEntries()) > 0 {
@@ -1072,6 +1084,7 @@ func (f *File) segmentFileBatches(creditFile, debitFile *File) {
10721084
}
10731085
}
10741086
}
1087+
return nil
10751088
}
10761089

10771090
// segmentFileIATBatches segments IAT batches debits and credits into debit and credit files
@@ -1175,29 +1188,45 @@ func (f *File) addFileHeaderData(file *File) *File {
11751188

11761189
// segmentFileBatchAddEntry adds entries to batches in a segmented file
11771190
// Applies to All SEC Codes except ADV (Automated Accounting Advice)
1178-
func segmentFileBatchAddEntry(creditBatch, debitBatch Batcher, entry *EntryDetail) {
1191+
func segmentFileBatchAddEntry(creditBatch, debitBatch Batcher, entry *EntryDetail) error {
11791192
switch entry.TransactionCode {
11801193
case CheckingCredit, CheckingReturnNOCCredit, CheckingPrenoteCredit, CheckingZeroDollarRemittanceCredit,
11811194
SavingsCredit, SavingsReturnNOCCredit, SavingsPrenoteCredit, SavingsZeroDollarRemittanceCredit,
11821195
GLCredit, GLReturnNOCCredit, GLPrenoteCredit, GLZeroDollarRemittanceCredit,
11831196
LoanCredit, LoanReturnNOCCredit, LoanPrenoteCredit, LoanZeroDollarRemittanceCredit:
1197+
if creditBatch == nil {
1198+
return errors.New("missing creditBatch")
1199+
}
11841200
creditBatch.AddEntry(entry)
1201+
11851202
case CheckingDebit, CheckingReturnNOCDebit, CheckingPrenoteDebit, CheckingZeroDollarRemittanceDebit,
11861203
SavingsDebit, SavingsReturnNOCDebit, SavingsPrenoteDebit, SavingsZeroDollarRemittanceDebit,
11871204
GLDebit, GLReturnNOCDebit, GLPrenoteDebit, GLZeroDollarRemittanceDebit,
11881205
LoanDebit, LoanReturnNOCDebit:
1206+
if debitBatch == nil {
1207+
return errors.New("missing debitBatch")
1208+
}
11891209
debitBatch.AddEntry(entry)
11901210
}
1211+
return nil
11911212
}
11921213

11931214
// segmentFileBatchAddADVEntry adds entries to batches in a segment file for SEC Code ADV (Automated Accounting Advice)
1194-
func segmentFileBatchAddADVEntry(creditBatch Batcher, debitBatch Batcher, entry *ADVEntryDetail) {
1215+
func segmentFileBatchAddADVEntry(creditBatch Batcher, debitBatch Batcher, entry *ADVEntryDetail) error {
11951216
switch entry.TransactionCode {
11961217
case CreditForDebitsOriginated, CreditForCreditsReceived, CreditForCreditsRejected, CreditSummary:
1218+
if creditBatch == nil {
1219+
return errors.New("missing creditBatch")
1220+
}
11971221
creditBatch.AddADVEntry(entry)
1222+
11981223
case DebitForCreditsOriginated, DebitForDebitsReceived, DebitForDebitsRejectedBatches, DebitSummary:
1224+
if debitBatch == nil {
1225+
return errors.New("missing debitBatch")
1226+
}
11991227
debitBatch.AddADVEntry(entry)
12001228
}
1229+
return nil
12011230
}
12021231

12031232
// Validates that the batch numbers are ascending

iterator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func (i *Iterator) NextEntry() (*BatchHeader, *EntryDetail, error) {
110110
if err != nil {
111111
return nil, nil, fmt.Errorf("faking batch for line %d failed: %w", i.reader.lineNum, err)
112112
}
113+
if i.reader.currentBatch == nil {
114+
return nil, nil, fmt.Errorf("failed to create %s batch: %v", bh.StandardEntryClassCode, err)
115+
}
113116
if err := i.reader.readLine(line); err != nil {
114117
return nil, nil, fmt.Errorf("reading line %d with fake BatchHeader failed: %w", i.reader.lineNum, err)
115118
}

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ifeq ($(OS),Windows_NT)
2828
else
2929
@wget -O lint-project.sh https://raw.githubusercontent.com/moov-io/infra/master/go/lint-project.sh
3030
@chmod +x ./lint-project.sh
31-
GOLANGCI_LINTERS=prealloc GOLANGCI_SKIP_DIR=test EXPERIMENTAL=shuffle \
31+
GOLANGCI_LINTERS=prealloc GOLANGCI_SKIP_DIR=test EXPERIMENTAL=nilaway,shuffle \
3232
GOCYCLO_LIMIT=26 COVER_THRESHOLD=90.0 \
3333
GOOS=js GOARCH=wasm time ./lint-project.sh
3434
endif

merge.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ach
1919

2020
import (
21+
"fmt"
2122
"time"
2223
)
2324

@@ -229,7 +230,10 @@ func (fs *mergableFiles) findOutfile(f *File) *File {
229230
}
230231

231232
func mergeEntries(b1, b2 Batcher) (Batcher, error) {
232-
b, _ := NewBatch(b1.GetHeader())
233+
b, err := NewBatch(b1.GetHeader())
234+
if err != nil {
235+
return nil, fmt.Errorf("mergeEntries: %w", err)
236+
}
233237
entries := sortEntriesByTraceNumber(append(b1.GetEntries(), b2.GetEntries()...))
234238
for i := range entries {
235239
b.AddEntry(entries[i])

reader.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,22 +575,30 @@ func (r *Reader) parseAddenda() error {
575575

576576
// parseADVAddenda takes the input record string and create an Addenda99 appended to the last ADVEntryDetail
577577
func (r *Reader) parseADVAddenda() error {
578+
if r.currentBatch == nil {
579+
return ErrFileAddendaOutsideBatch
580+
}
578581
if len(r.currentBatch.GetADVEntries()) == 0 {
579582
return ErrFileAddendaOutsideEntry
580583
}
584+
581585
entryIndex := len(r.currentBatch.GetADVEntries()) - 1
582586
entry := r.currentBatch.GetADVEntries()[entryIndex]
583587

584588
if entry.AddendaRecordIndicator != 1 {
585589
return r.parseError(r.currentBatch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator))
586590
}
591+
587592
addenda99 := NewAddenda99()
588593
addenda99.Parse(r.line)
594+
589595
if err := maybeValidate(addenda99, r.File.validateOpts); err != nil {
590596
return r.parseError(err)
591597
}
598+
592599
r.currentBatch.GetADVEntries()[entryIndex].Category = CategoryReturn
593600
r.currentBatch.GetADVEntries()[entryIndex].Addenda99 = addenda99
601+
594602
return nil
595603
}
596604

server/files.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ func decodeCreateFileRequest(_ context.Context, request *http.Request) (interfac
161161
}
162162

163163
for _, name := range validationNames {
164-
input := request.URL.Query().Get(name)
164+
q := request.URL.Query()
165+
if q == nil {
166+
continue
167+
}
168+
input := q.Get(name)
165169
if input == "" {
166170
continue
167171
}
@@ -237,7 +241,9 @@ func decodeCreateFileRequest(_ context.Context, request *http.Request) (interfac
237241
// Set the fileID from the request
238242
fileID, ok := mux.Vars(request)["fileID"]
239243
if ok && fileID != "" && fileID != "create" {
240-
req.File.ID = fileID
244+
if req.File != nil {
245+
req.File.ID = fileID
246+
}
241247
}
242248

243249
return req, nil

server/repository.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ func (r *repositoryInMemory) StoreBatch(fileID string, batch ach.Batcher) error
133133
}
134134

135135
// Add the batch to the file
136-
r.files[fileID].AddBatch(batch)
136+
if f := r.files[fileID]; f != nil {
137+
f.AddBatch(batch)
138+
}
137139

138140
return nil
139141
}
@@ -203,7 +205,11 @@ func (r *repositoryInMemory) cleanupOldFiles() {
203205
tooOldStr := tooOld.Format("060102") // YYMMDD
204206

205207
for i := range r.files {
206-
if r.files[i].Header.FileCreationDate < tooOldStr {
208+
file := r.files[i]
209+
if file == nil {
210+
continue
211+
}
212+
if file.Header.FileCreationDate < tooOldStr {
207213
removed++
208214
delete(r.files, i)
209215
}

server/routing.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ func marshalStructWithError(in interface{}, w http.ResponseWriter) error {
242242
for i := 0; i < v.NumField(); i++ {
243243
name := v.Type().Field(i).Name
244244
value := v.Field(i).Interface()
245-
245+
if value == nil {
246+
continue
247+
}
246248
if err, ok := value.(error); ok {
247249
out["error"] = err.Error()
248250
} else {

test/ach-adv-write-multipleBatches/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ func main() {
4747
bh.ODFIIdentification = "121042882"
4848
bh.OriginatorStatusCode = 0
4949

50-
batch, _ := ach.NewBatch(bh)
50+
batch, err := ach.NewBatch(bh)
51+
if err != nil {
52+
log.Fatalf("%T: %v", err, err)
53+
}
5154

5255
// Create Entry
5356
entrySeq := 0

test/server-example/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/moov-io/ach/server"
2828

2929
"github.com/go-kit/log"
30+
"github.com/stretchr/testify/require"
3031
)
3132

3233
func TestServer__CreateFile(t *testing.T) {
@@ -55,7 +56,10 @@ func TestServer__CreateFile(t *testing.T) {
5556
if err != nil {
5657
t.Fatal(err)
5758
}
58-
defer resp.Body.Close()
59+
if resp != nil && resp.Body != nil {
60+
defer resp.Body.Close()
61+
}
62+
require.NotNil(t, resp)
5963

6064
if resp.StatusCode != http.StatusOK {
6165
t.Errorf("got %d HTTP status code", resp.StatusCode)

0 commit comments

Comments
 (0)