Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For Mentor Review | output-and-error-handling | Prince Wilson #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions projects/output-and-error-handling/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/CodeYourFuture/immersive-go-course/projects/output-and-error-handling

go 1.22.4
98 changes: 98 additions & 0 deletions projects/output-and-error-handling/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
)

// Global constants
const (
delayLimit = 5
url = "http://localhost:8080"
)

func main() {

// Fetch weather updates
update, err := getWeatherUpdate(url)

// Handle errors and
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v ", err)
os.Exit(1)
} else if update == "" {
fmt.Fprint(os.Stderr, "error: no updates for today ")
os.Exit(1)
}

// Prints weather update
fmt.Fprintln(os.Stdout, update)

}

// Function to fetch weather updates
func getWeatherUpdate(url string) (string, error) {

// HTTP GET request
resp, err := http.Get(url)

if err != nil {
return "", errors.New("sorry, could not get weather updates from " + url)
}

defer resp.Body.Close()

// Handle different HTTP status codes
switch resp.StatusCode {
case http.StatusOK:
return parseBody(resp)
case http.StatusTooManyRequests:
return "", retryServer(resp)
default:
return "", errors.New("an unexpected error occured, try later ")
}

}

// Function to parse response body
func parseBody(request *http.Response) (string, error) {

body, err := io.ReadAll(request.Body)

if err != nil {
return "", errors.New("could not get the weather response ")
}

weatherUpdate := string(body)

return weatherUpdate, nil

}

// Function to handle retry logic based on Retry-After header
func retryServer(request *http.Response) error {

const retryHeader = "Retry-After"
retryTime := request.Header.Get(retryHeader)

delay, err := strconv.Atoi(retryTime)

if err != nil {
return errors.New("service temporarily unavailable, retry later ")
}

// if delay within limit, retry after waiting
if delay <= delayLimit {
fmt.Fprintln(os.Stderr, "retrying, it might a take a while.....")
time.Sleep(time.Duration(delay) * time.Second)
getWeatherUpdate(url) //Retry fetching weather updates
return nil
}

return nil
}