fix:#41 added error if html request isn't 200

This commit is contained in:
Elmar Kresse
2024-06-24 10:23:04 +02:00
parent 9f3d2294dd
commit 2ff37ea441
6 changed files with 125 additions and 32 deletions

View File

@@ -17,8 +17,10 @@
package fetch
import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"time"
)
@@ -26,18 +28,27 @@ import (
// getPlanHTML Get the HTML document from the specified URL
func GetHTML(url string) (string, error) {
// Create HTTP client with timeout of 5 seconds
client := http.Client{
Timeout: 30 * time.Second,
}
return GetHTMLWithClient(url, &client)
}
func GetHTMLWithClient(url string, client *http.Client) (string, error) {
// Send GET request
response, err := client.Get(url)
if err != nil {
fmt.Printf("Error occurred while making the request: %s\n", err.Error())
slog.Error("Error occurred while fetching the HTML document:", "error", err)
return "", err
}
if response.StatusCode != 200 {
slog.Warn("While fetching the HTML document, the server responded with status code: ", "status", response.StatusCode)
return "", errors.New(fmt.Sprintf("Server responded with status code: %d", response.StatusCode))
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {