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

added better errorhandling (replaced bad switch-statement)

parent bff44291
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
// creates http client for ensuring timely timeouts // creates http client for ensuring timely timeouts
var client = http.Client{ var client = http.Client{
Timeout: 15 * time.Second, Timeout: 10 * time.Second,
} }
// allowing access to "overloaded" functions // allowing access to "overloaded" functions
...@@ -58,7 +58,7 @@ func getBody(url string, m mashup) error { ...@@ -58,7 +58,7 @@ func getBody(url string, m mashup) error {
// sends appropriate error // sends appropriate error
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%d", resp.StatusCode) return fmt.Errorf("bad status code: %d", resp.StatusCode)
} }
// all went well // all went well
...@@ -70,17 +70,17 @@ func HttpError(w http.ResponseWriter, err error) { ...@@ -70,17 +70,17 @@ func HttpError(w http.ResponseWriter, err error) {
// if timeout happened // if timeout happened
if strings.Contains(err.Error(), "Client.Timeout exceeded") { if strings.Contains(err.Error(), "Client.Timeout exceeded") {
http.Error(w, "Request timeout", http.StatusGatewayTimeout) http.Error(w, "Request to API timed out", http.StatusGatewayTimeout)
return // return if error is already displayed return // return if error is already displayed
} }
switch err.Error() { // if a custom error was sent (caused by api response code)
case "400": if strings.Contains(err.Error(), "bad status code:") {
http.Error(w, "Bad Request: " + err.Error(), http.StatusBadRequest) http.Error(w, "Request to API timed out", http.StatusGatewayTimeout)
case "404": return // return if error is already displayed
http.Error(w, "Not found: " + err.Error(), http.StatusNotFound)
default:
http.Error(w, "Error occurred", http.StatusInternalServerError)
} }
// Default response in case of decoding errors or other errors
http.Error(w, "Internal server error", 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