fix api error response handling

This commit is contained in:
Adora Laura Kalb 2024-07-26 12:16:59 +02:00
parent 289a69ffeb
commit 1da8976657
Signed by: adoralaura
SSH key fingerprint: SHA256:3XrkbR8ikAZJVtYfaUliX1MhmJYVAe/ocIb/MiDHBJ8
3 changed files with 17 additions and 1 deletions

View file

@ -7,9 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [1.0.1] - 2024-07-26
### Fixed
- better error handling when API responds with an error
## [1.0.0] - 2024-07-26 ## [1.0.0] - 2024-07-26
First release of `mailcow-admin-aliases` First release of `mailcow-admin-aliases`
[unreleased]: https://code.lila.network/adoralaura/mailcow-admin-aliases/compare/1.0.0...HEAD [unreleased]: https://code.lila.network/adoralaura/mailcow-admin-aliases/compare/1.0.1...HEAD
[1.0.1]: https://code.lila.network/adoralaura/mailcow-admin-aliases/compare/1.0.0...1.0.1
[1.0.0]: https://code.lila.network/adoralaura/mailcow-admin-aliases/releases/tag/1.0.0 [1.0.0]: https://code.lila.network/adoralaura/mailcow-admin-aliases/releases/tag/1.0.0

View file

@ -7,6 +7,7 @@ import (
"io" "io"
"log/slog" "log/slog"
"net/http" "net/http"
"os"
"time" "time"
"code.lila.network/adoralaura/mailcow-admin-aliases/internal/configuration" "code.lila.network/adoralaura/mailcow-admin-aliases/internal/configuration"
@ -64,6 +65,11 @@ func LoadAliases(cfg configuration.Config) ([]MailcowAlias, []string, error) {
slog.Debug("alias response received", "status", res.Status, "status-code", res.StatusCode) slog.Debug("alias response received", "status", res.Status, "status-code", res.StatusCode)
if res.StatusCode != 200 {
slog.Error("API request unsuccessful", "status", res.Status, "status-code", res.StatusCode)
os.Exit(1)
}
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return []MailcowAlias{}, []string{}, fmt.Errorf("failed to read alias request body: %w", err) return []MailcowAlias{}, []string{}, fmt.Errorf("failed to read alias request body: %w", err)

View file

@ -6,6 +6,7 @@ import (
"io" "io"
"log/slog" "log/slog"
"net/http" "net/http"
"os"
"time" "time"
"code.lila.network/adoralaura/mailcow-admin-aliases/internal/configuration" "code.lila.network/adoralaura/mailcow-admin-aliases/internal/configuration"
@ -37,6 +38,11 @@ func LoadDomains(cfg configuration.Config) ([]MailcowDomain, error) {
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 {
slog.Error("API request unsuccessful", "status", res.Status, "status-code", res.StatusCode)
os.Exit(1)
}
slog.Debug("domain response received", "status", res.Status, "status-code", res.StatusCode) slog.Debug("domain response received", "status", res.Status, "status-code", res.StatusCode)
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)