Commit d2a408e4 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

revamping error-handling && commenting code

parent 2a43c0fd
...@@ -27,6 +27,6 @@ func Chandler(w http.ResponseWriter, r *http.Request) { ...@@ -27,6 +27,6 @@ func Chandler(w http.ResponseWriter, r *http.Request) {
} }
} else { } else {
// need to check error for content when setting header (pkg.CorrectHeader) // need to check error for content when setting header (pkg.CorrectHeader)
http.Error(w, "An error occurred", pkg.CorrectHeader(err)) pkg.HttpError(w, err)
} }
} }
...@@ -3,7 +3,6 @@ package handler ...@@ -3,7 +3,6 @@ package handler
import ( import (
"assignment-1/pkg" "assignment-1/pkg"
"encoding/json" "encoding/json"
"fmt"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"log" "log"
"net/http" "net/http"
...@@ -20,7 +19,6 @@ func Shandler(w http.ResponseWriter, r *http.Request) { ...@@ -20,7 +19,6 @@ func Shandler(w http.ResponseWriter, r *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {
w.WriteHeader(pkg.CorrectHeader(err)) pkg.HttpError(w, err)
_, _ = fmt.Fprintln(w, "An error occurred")
} }
} }
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time"
) )
// allowing access to "overloaded" functions // allowing access to "overloaded" functions
...@@ -36,30 +38,47 @@ func (s *Specie) unmarshal(resp *http.Response) error { ...@@ -36,30 +38,47 @@ func (s *Specie) unmarshal(resp *http.Response) error {
// parses httpresponse as a string used later for parsing // parses httpresponse as a string used later for parsing
func getBody(url string, m mashup) error { func getBody(url string, m mashup) error {
resp, err := http.Get(url)
// creates a client to allow for timeout
client := http.Client{
Timeout: 4 * time.Second,
}
// sends a http request
resp, err := client.Get(url)
if err != nil { if err != nil {
return err return err
} }
// decodes the http response & closes the body
err = unfold(m, resp) err = unfold(m, resp)
if err != nil { if err != nil {
return err return err
} }
// sends appropriate error
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%d", resp.StatusCode) return fmt.Errorf("%d", resp.StatusCode)
} }
// all went well
return nil return nil
} }
// func for returning correct http.Head response code // func for returning correct http.Head response code
func CorrectHeader(err error) int { func HttpError(w http.ResponseWriter, err error) {
// if timeout happened
if strings.Contains(err.Error(), "Client.Timeout exceeded") {
http.Error(w, "Request timeout", http.StatusGatewayTimeout)
return // return if error is already displayed
}
switch err.Error() { switch err.Error() {
case "400", "404": case "400", "404":
return http.StatusBadGateway http.Error(w, "Not found", http.StatusBadGateway)
case "408":
return http.StatusGatewayTimeout
default: default:
return http.StatusInternalServerError http.Error(w, "Error occurred", http.StatusInternalServerError)
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment