Skip to content

Commit

Permalink
Merge pull request #8 from Clever/better-error-messages
Browse files Browse the repository at this point in the history
Better error messages
  • Loading branch information
andruwm authored Mar 22, 2023
2 parents d2d4a7b + 32b8350 commit 2448bd8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
build:
working_directory: /go/src/github.com/Clever/elblog
docker:
- image: circleci/golang:1.12-stretch
- image: circleci/golang:1.16
steps:
- checkout
- run: make test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# dependancies
vendor

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
Expand Down
41 changes: 11 additions & 30 deletions elblog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package elblog
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"strconv"
Expand Down Expand Up @@ -51,6 +52,7 @@ func Parse(b []byte) (log *Log, err error) {
adv, i int
code int64
dur float64
ip int64
tok []byte
parts [][]byte
)
Expand All @@ -62,15 +64,13 @@ func Parse(b []byte) (log *Log, err error) {
data = data[adv:]
adv, tok, err = scan(data)
if err != nil {
return
return nil, fmt.Errorf("unable to scan next token: %v", err)
}
switch i {
case 0:
log.Type = string(tok)
case 1:
if log.Time, err = time.Parse(time.RFC3339Nano, string(tok)); err != nil {
return
}
log.Time, err = time.Parse(time.RFC3339Nano, string(tok))
case 2:
log.Name = string(tok)
case 3:
Expand All @@ -81,10 +81,7 @@ func Parse(b []byte) (log *Log, err error) {
IP: net.ParseIP(string(parts[0])),
}
case 2:
ip, err := strconv.ParseInt(string(parts[1]), 10, 32)
if err != nil {
return nil, err
}
ip, err = strconv.ParseInt(string(parts[1]), 10, 32)
log.From = &net.TCPAddr{
IP: net.ParseIP(string(parts[0])),
Port: int(ip),
Expand All @@ -98,49 +95,30 @@ func Parse(b []byte) (log *Log, err error) {
IP: net.ParseIP(string(parts[0])),
}
case 2:
ip, err := strconv.ParseInt(string(parts[1]), 10, 32)
if err != nil {
return nil, err
}
ip, err = strconv.ParseInt(string(parts[1]), 10, 32)
log.To = &net.TCPAddr{
IP: net.ParseIP(string(parts[0])),
Port: int(ip),
}
}
case 5:
dur, err = strconv.ParseFloat(string(tok), 64)
if err != nil {
return
}
log.RequestProcessingTime = time.Duration(dur * 1000 * 1000 * 1000)
case 6:
dur, err = strconv.ParseFloat(string(tok), 64)
if err != nil {
return
}
log.BackendProcessingTime = time.Duration(dur * 1000 * 1000 * 1000)
case 7:
dur, err = strconv.ParseFloat(string(tok), 64)
if err != nil {
return
}
log.ResponseProcessingTime = time.Duration(dur * 1000 * 1000 * 1000)
case 8:
code, err = strconv.ParseInt(string(tok), 10, 32)
if err != nil {
return
}
log.ELBStatusCode = int(code)
case 9:
log.BackendStatusCode = string(tok)
case 10:
if log.ReceivedBytes, err = strconv.ParseInt(string(tok), 10, 32); err != nil {
return
}
log.ReceivedBytes, err = strconv.ParseInt(string(tok), 10, 32)
case 11:
if log.SentBytes, err = strconv.ParseInt(string(tok), 10, 32); err != nil {
return
}
log.SentBytes, err = strconv.ParseInt(string(tok), 10, 32)
case 12:
log.Request = string(tok)
case 13:
Expand Down Expand Up @@ -181,6 +159,9 @@ func Parse(b []byte) (log *Log, err error) {
log.OtherFields = string(data)
adv = len(data)
}
if err != nil {
return nil, fmt.Errorf("invalid field %q at index %d: %v", tok, i, err)
}
i++
}
return
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/Clever/elblog

go 1.16

0 comments on commit 2448bd8

Please sign in to comment.