mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-07 04:09:15 +02:00
fix:#41 added error if html request isn't 200
This commit is contained in:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user