Skip to content

Commit

Permalink
fix: errors on slog.Error usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihendry committed Sep 20, 2024
1 parent f68ee6f commit fdac318
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ go 1.22
require (
github.com/apex/gateway/v2 v2.0.0
github.com/fogleman/gg v1.3.0
github.com/go-chi/chi/v5 v5.0.12
github.com/go-chi/chi/v5 v5.1.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
golang.org/x/image v0.15.0
golang.org/x/image v0.20.0
)

require (
github.com/aws/aws-lambda-go v1.46.0 // indirect
github.com/aws/aws-lambda-go v1.47.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/apex/gateway/v2 v2.0.0/go.mod h1:y+uuK0JxdvTHZeVns501/7qklBhnDHtGU0hf
github.com/aws/aws-lambda-go v1.17.0/go.mod h1:FEwgPLE6+8wcGBTe5cJN3JWurd1Ztm9zN4jsXsjzKKw=
github.com/aws/aws-lambda-go v1.46.0 h1:UWVnvh2h2gecOlFhHQfIPQcD8pL/f7pVCutmFl+oXU8=
github.com/aws/aws-lambda-go v1.46.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -12,6 +14,8 @@ github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -34,6 +38,8 @@ github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pv
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func getLogger(logLevel string) *slog.Logger {
func main() {
server, err := NewServer("all.json")
if err != nil {
slog.Error("failed to create server", err)
slog.Error("failed to create server", "error", err)
}

slog.SetDefault(getLogger(os.Getenv("LOGLEVEL")))
Expand All @@ -91,13 +91,13 @@ func main() {
err = http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), server.router)
}

slog.Error("error listening", err)
slog.Error("error listening", "error", err)
}

func NewServer(busStopsPath string) (*Server, error) {
bs, err := loadBusJSON(busStopsPath)
if err != nil {
slog.Error("unable to load bus stops", err)
slog.Error("unable to load bus stops", "error", err)
}

srv := Server{
Expand All @@ -116,7 +116,7 @@ func NewServer(busStopsPath string) (*Server, error) {

directory, err := fs.Sub(static, "static")
if err != nil {
slog.Error("unable to load static files", err)
slog.Error("unable to load static files", "error", err)
}
fileServer := http.FileServer(http.FS(directory))
//srv.router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fileServer))
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {

t, err := template.New("").Funcs(funcs).ParseFS(static, "static/index.html")
if err != nil {
slog.Error("template failed to parse", err)
slog.Error("template failed to parse", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand All @@ -167,7 +167,7 @@ func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
if id != "" {
arriving, err = busArrivals(id)
if err != nil {
slog.Error("failed to retrieve bus timings", err)
slog.Error("failed to retrieve bus timings", "error", err)
http.Error(w, fmt.Sprintf("datamall API is returning, %s", err.Error()), http.StatusFailedDependency)
return
}
Expand All @@ -177,7 +177,7 @@ func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {

err = t.ExecuteTemplate(w, "index.html", arriving)
if err != nil {
slog.Error("template failed to parse", err)
slog.Error("template failed to parse", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func busArrivals(stopID string) (arrivals SGBusArrivals, err error) {

defer func() {
if err := res.Body.Close(); err != nil {
slog.Error("failed to close response body", err)
slog.Error("failed to close response body", "error", err)
}
}()

Expand All @@ -219,7 +219,7 @@ func busArrivals(stopID string) (arrivals SGBusArrivals, err error) {
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&arrivals)
if err != nil {
slog.Error("failed to decode response", err)
slog.Error("failed to decode response", "error", err)
return
}

Expand Down Expand Up @@ -248,9 +248,9 @@ type BusStop struct {
type BusStops []BusStop

func loadBusJSON(jsonfile string) (bs BusStops, err error) {
content, err := static.ReadFile("static/all.json")
content, err := static.ReadFile(jsonfile)
if err != nil {
slog.Error("failed to read file", err)
slog.Error("failed to read file", "error", err)
return
}
err = json.Unmarshal(content, &bs)
Expand Down

0 comments on commit fdac318

Please sign in to comment.