Skip to content
Snippets Groups Projects
Commit 31883b10 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

added status codes as strings diag

parent f785ee22
No related branches found
No related tags found
No related merge requests found
package pkg package pkg
import ( import (
"fmt" "strconv"
"strings"
"time" "time"
) )
...@@ -16,8 +17,8 @@ func init() { ...@@ -16,8 +17,8 @@ func init() {
// struct for returning requested data // struct for returning requested data
type Diag struct { type Diag struct {
Gbif int `json:"gbif,omitempty"` Gbif string `json:"gbif,omitempty"`
Restcountries int `json:"restcountries,omitempty"` Restcountries string `json:"restcountries,omitempty"`
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
Uptime int `json:"uptime,omitempty"` Uptime int `json:"uptime,omitempty"`
} }
...@@ -27,12 +28,22 @@ func GetDiag(d* Diag) error { ...@@ -27,12 +28,22 @@ func GetDiag(d* Diag) error {
// gets status of GBIF api, returns appropriate error // gets status of GBIF api, returns appropriate error
if err := getGbifStatus(d); err != nil{ if err := getGbifStatus(d); err != nil{
return fmt.Errorf("error occured while contacting GBIF API: %s", err) // if it was a timeout caused by the http.client
if strings.Contains(err.Error(), "error occured while contacting") {
d.Gbif = "503: Request timed out"
} else {
return err
}
} }
// gets status of restcountries api, returns appropriate error // gets status of restcountries api, returns appropriate error
if err := getRestStatus(d); err != nil { if err := getRestStatus(d); err != nil {
return fmt.Errorf("error occured while contacting Restcountries API: %s", err) // if it was a timeout caused by the http.client
if strings.Contains(err.Error(), "error occured while contacting") {
d.Gbif = "503: Request timed out"
} else {
return err
}
} }
d.Version = "v1" d.Version = "v1"
...@@ -48,7 +59,8 @@ func getGbifStatus(d *Diag) error { ...@@ -48,7 +59,8 @@ func getGbifStatus(d *Diag) error {
if err != nil { if err != nil {
return err return err
} }
d.Gbif = resp.StatusCode defer resp.Body.Close()
d.Gbif = strconv.Itoa(resp.StatusCode)
return nil return nil
} }
...@@ -58,7 +70,8 @@ func getRestStatus(d *Diag) error { ...@@ -58,7 +70,8 @@ func getRestStatus(d *Diag) error {
if err != nil { if err != nil {
return err return err
} }
d.Restcountries = resp.StatusCode defer resp.Body.Close()
d.Restcountries = strconv.Itoa(resp.StatusCode)
return nil return nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment