Improve Error Handling in GetOJS Function
- Enhanced the GetOJS function to include error handling for reading the response body and parsing CSV records, ensuring that errors are properly returned instead of being ignored. - Added a check to ensure that the CSV records contain data, returning a meaningful error if the calendar is empty, which improves robustness and clarity in error reporting.
This commit is contained in:
+13
-2
@@ -27,9 +27,20 @@ func GetOJS(baseURL string, year int, date string) (string, error) {
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// read CSV
|
// read CSV
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return ojs, err
|
||||||
|
}
|
||||||
|
|
||||||
reader := csv.NewReader(bytes.NewReader(body))
|
reader := csv.NewReader(bytes.NewReader(body))
|
||||||
records, _ := reader.ReadAll()
|
records, err := reader.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
return ojs, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(records) < 2 {
|
||||||
|
return ojs, fmt.Errorf("ojs calendar is empty")
|
||||||
|
}
|
||||||
|
|
||||||
for _, row := range records[1:] {
|
for _, row := range records[1:] {
|
||||||
rowDate := strings.TrimSpace(row[1])
|
rowDate := strings.TrimSpace(row[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user