diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-10-25 14:06:42 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-10-25 14:06:42 -0400 |
| commit | afa4bc6c01297af78885bf0562e2dae7ff83605b (patch) | |
| tree | 97a1149d5646cf9b1c7aa2892d6e849c589219cc /web/vastai/error.go | |
| parent | 797eec6d94e193ae684db977179ea4a422b2499f (diff) | |
| download | x-afa4bc6c01297af78885bf0562e2dae7ff83605b.tar.xz x-afa4bc6c01297af78885bf0562e2dae7ff83605b.zip | |
cmd: add amano and stealthmountain
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'web/vastai/error.go')
| -rw-r--r-- | web/vastai/error.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/web/vastai/error.go b/web/vastai/error.go new file mode 100644 index 0000000..89487a7 --- /dev/null +++ b/web/vastai/error.go @@ -0,0 +1,32 @@ +package vastai + +import ( + "encoding/json" + "fmt" + "net/http" +) + +type Error struct { + Success bool `json:"success"` + ErrorKind string `json:"error"` + Msg string `json:"msg"` + StatusCode int `json:"status_code"` +} + +func (e Error) Error() string { + return fmt.Sprintf("%s (%d): %s", e.ErrorKind, e.StatusCode, e.Msg) +} + +func NewError(resp *http.Response) error { + var e Error + + dec := json.NewDecoder(resp.Body) + defer resp.Body.Close() + if err := dec.Decode(&e); err != nil { + return fmt.Errorf("vastai: can't decode json while handling error: %w", err) + } + + e.StatusCode = resp.StatusCode + + return e +} |
