From 4c3e224db668140a7995ae43058a801ccdbd37db Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 18 Apr 2023 13:52:07 -0700 Subject: [PATCH] update json error methods --- client/client.go | 7 ++++++- client/history.go | 14 +++++++++++++- client/types/types.go | 10 +++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index c7732e5..841b827 100644 --- a/client/client.go +++ b/client/client.go @@ -1,8 +1,13 @@ package client +import "errors" + const apiEndpoint = "https://api.elevenlabs.io" -var ErrUnauthorized error +var ( + ErrUnauthorized = errors.New("unauthorized") + ErrUnspecified = errors.New("unspecified error") +) type Client struct { apiKey string diff --git a/client/history.go b/client/history.go index d945330..22b67f5 100644 --- a/client/history.go +++ b/client/history.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -26,13 +27,24 @@ func (c Client) HistoryDelete(ctx context.Context, historyItemId string) (bool, switch res.StatusCode { case 422: + ve := types.ValidationError{} + defer res.Body.Close() + jerr := json.NewDecoder(res.Body).Decode(&ve) + if jerr != nil { + err = errors.Join(err, jerr) + } else { + err = errors.Join(err, ve) + } return false, err case 401: return false, ErrUnauthorized case 200: if err != nil { - return false, nil + return false, err } + return true, nil + default: + return false, errors.Join(err, ErrUnspecified) } return true, nil } diff --git a/client/types/types.go b/client/types/types.go index 832c980..0cbe518 100644 --- a/client/types/types.go +++ b/client/types/types.go @@ -1,6 +1,9 @@ package types -import "os" +import ( + "fmt" + "os" +) type AddVoiceResponse struct { VoiceID string `json:"voice_id"` @@ -143,6 +146,11 @@ type ValidationError struct { Msg string `json:"msg"` Type_ string `json:"type"` } + +func (ve ValidationError) Error() string { + return fmt.Sprintf("%s %s: ", ve.Type_, ve.Msg) +} + type VerificationAttemptResponseModel struct { Text string `json:"text"` DateUnix int32 `json:"date_unix"`