Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed May 25, 2020
1 parent 0443592 commit 8334b13
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ func (database *Database) Lookup(ipaddress string) (*IPLocation, error) {
defer db.Close()

ip := net.ParseIP(ipaddress)
if ip == nil {
return nil, errors.New("Invalid IP address")
}

err = db.Lookup(ip, &location)
if err != nil {
if err := db.Lookup(ip, &location); err != nil {
return location, err
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/joho/godotenv v1.3.0
github.com/markusthoemmes/goautoneg v0.0.0-20190713162725-c6008fefa5b1 // indirect
github.com/oschwald/maxminddb-golang v1.6.0
github.com/pieterclaerhout/go-log v1.13.0
github.com/pieterclaerhout/go-webserver/v2 v2.0.2
github.com/pieterclaerhout/go-log v1.14.0
github.com/pieterclaerhout/go-webserver/v2 v2.0.3
github.com/pkg/errors v0.9.1
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ github.com/pieterclaerhout/go-formatter v1.0.4 h1:XKFCBb9aNwazTqhtH1snru/D8E7yXQ
github.com/pieterclaerhout/go-formatter v1.0.4/go.mod h1:1okQQFUwXCLGTWXoWoh3OGchp3i7KeebwIx/ufmsjks=
github.com/pieterclaerhout/go-log v1.13.0 h1:Jx12Ou5O7olkB48mD5IdqRWiYt5AHwfuLLDlz5LnDXw=
github.com/pieterclaerhout/go-log v1.13.0/go.mod h1:Zwdhg00fN6IuEWDvktUpu/XFMC5Y+NWrXyQ1RQHRPjc=
github.com/pieterclaerhout/go-log v1.14.0 h1:TWh+YvSWdqYeejB15pxa6S8p06kDDLCUz1nfNyqOJPo=
github.com/pieterclaerhout/go-log v1.14.0/go.mod h1:Zwdhg00fN6IuEWDvktUpu/XFMC5Y+NWrXyQ1RQHRPjc=
github.com/pieterclaerhout/go-webserver/v2 v2.0.2 h1:7WBPFuNwlEvZDb7ooZRkn0Zxx+2Azrswm53K/XaxaFE=
github.com/pieterclaerhout/go-webserver/v2 v2.0.2/go.mod h1:LNTKF6X39cC9DtFA2AbHt2kOJK5TwTfB3NtzMxrpjAk=
github.com/pieterclaerhout/go-webserver/v2 v2.0.3 h1:4jqg7LiAH3jXayGiQDTRWThXQlmS/h1iDIIZyeY03CI=
github.com/pieterclaerhout/go-webserver/v2 v2.0.3/go.mod h1:lvYt/vW838Slf62NWLOECTWBaUiWCugRsxCKmb9A5Qs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
6 changes: 6 additions & 0 deletions serverapp/handle_lookup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package serverapp

import (
"errors"
"net/http"

"github.com/pieterclaerhout/go-webserver/v2/binder"
Expand All @@ -21,6 +22,11 @@ func (a *serverApp) handleLookup() http.HandlerFunc {
return
}

if req.IPAddress == "" {
respond.Error(errors.New("No IP address specified")).Write(w, r)
return
}

result, err := a.db.Lookup(req.IPAddress)
if err != nil {
respond.Error(err).Write(w, r)
Expand Down

0 comments on commit 8334b13

Please sign in to comment.