mirror of
https://github.com/taigrr/elevenlabs.git
synced 2026-04-02 03:08:57 -07:00
update json error methods
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
const apiEndpoint = "https://api.elevenlabs.io"
|
const apiEndpoint = "https://api.elevenlabs.io"
|
||||||
|
|
||||||
var ErrUnauthorized error
|
var (
|
||||||
|
ErrUnauthorized = errors.New("unauthorized")
|
||||||
|
ErrUnspecified = errors.New("unspecified error")
|
||||||
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
apiKey string
|
apiKey string
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -26,13 +27,24 @@ func (c Client) HistoryDelete(ctx context.Context, historyItemId string) (bool,
|
|||||||
|
|
||||||
switch res.StatusCode {
|
switch res.StatusCode {
|
||||||
case 422:
|
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
|
return false, err
|
||||||
case 401:
|
case 401:
|
||||||
return false, ErrUnauthorized
|
return false, ErrUnauthorized
|
||||||
case 200:
|
case 200:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, nil
|
return false, err
|
||||||
}
|
}
|
||||||
|
return true, nil
|
||||||
|
default:
|
||||||
|
return false, errors.Join(err, ErrUnspecified)
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type AddVoiceResponse struct {
|
type AddVoiceResponse struct {
|
||||||
VoiceID string `json:"voice_id"`
|
VoiceID string `json:"voice_id"`
|
||||||
@@ -143,6 +146,11 @@ type ValidationError struct {
|
|||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
Type_ string `json:"type"`
|
Type_ string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ve ValidationError) Error() string {
|
||||||
|
return fmt.Sprintf("%s %s: ", ve.Type_, ve.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
type VerificationAttemptResponseModel struct {
|
type VerificationAttemptResponseModel struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
DateUnix int32 `json:"date_unix"`
|
DateUnix int32 `json:"date_unix"`
|
||||||
|
|||||||
Reference in New Issue
Block a user