Skip to content

Commit

Permalink
error handling cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychase committed Feb 1, 2022
1 parent db7571f commit 661ea9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
49 changes: 32 additions & 17 deletions cmd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"
"net/http"
"os"
"strconv"
Expand All @@ -12,7 +11,12 @@ import (
)

func awpHandlerV1(w http.ResponseWriter, req *http.Request) {
opts := ctxOptions(req.Context())
opts, err := ctxOptions(req.Context())
if err != nil {
msg := fmt.Sprintf("error: %s", err)
errorHandler(w, req, msg, http.StatusInternalServerError)
return
}

wx := aprs.Wx{
Lat: opts.latitude,
Expand Down Expand Up @@ -112,28 +116,39 @@ func awpHandlerV1(w http.ResponseWriter, req *http.Request) {
Path: aprs.Path{aprs.Addr{Call: "TCPIP", Repeated: true}},
Text: wx.String(),
}
err := f.SendIS("tcp://cwop.aprs.net:14580", -1) //BUG(medium) flag
err = f.SendIS("tcp://cwop.aprs.net:14580", -1) //BUG(medium) flag
if err != nil {
log.Printf("Upload error: %s", err) // BUG(medium) handle
msg := fmt.Sprintf("Upload error: %s", err)
errorHandler(w, req, msg, http.StatusServiceUnavailable)
return
}

w.WriteHeader(http.StatusOK)
}

// BUG(medium-high) update
func errorHandler(w http.ResponseWriter, req *http.Request, status int) {
w.WriteHeader(status)
func errorHandler(w http.ResponseWriter, req *http.Request, msg string, status int) {

// server output
stream := os.Stderr
if status == http.StatusNotFound {
fmt.Fprint(w, "404")
stream = os.Stdout
}
fmt.Fprintf(
stream,
"%v -- \"%v %v %v\" %d %s\n",
req.RemoteAddr,
req.Method,
req.URL.Path,
req.Proto,
status,
msg,
)

fmt.Println(req)
// response
w.WriteHeader(status)
fmt.Fprintf(w, "%d - %s", status, msg)
}

// BUG(medium-high) update
func catchall(w http.ResponseWriter, req *http.Request) {

if req.URL.Path != "/" {
errorHandler(w, req, http.StatusNotFound)
return
}
fmt.Fprint(w, "welcome home")
func defaultHandler(w http.ResponseWriter, req *http.Request) {
errorHandler(w, req, "not found", http.StatusNotFound)
}
8 changes: 4 additions & 4 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"context"
"log"
"errors"
"net"
)

Expand All @@ -24,11 +24,11 @@ func ctxWithOptions(ctx context.Context, opts options) context.Context {
return context.WithValue(context.Background(), key, opts)
}

func ctxOptions(ctx context.Context) options {
func ctxOptions(ctx context.Context) (options, error) {
opts, ok := ctx.Value(optionKey("options")).(options)
if !ok {
log.Printf("type assertion failure") // BUG(high) handle
return opts, errors.New("unable to return options")
}

return opts
return opts, nil
}
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func server(opts options) error {
}

// Routes
http.HandleFunc("/", catchall)
http.HandleFunc("/", defaultHandler)
http.HandleFunc("/wxigate/awp/v1", awpHandlerV1)

_, err = fmt.Printf("%s-%s %f %f listening on: %s\n", opts.callsign, opts.ssid, opts.longitude, opts.latitude, listener.Addr().String())
Expand Down

0 comments on commit 661ea9d

Please sign in to comment.