mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
refactor: error handling in various plugins to convert response.Error to Go errors
- Updated error handling in `nd_host_scheduler.go`, `nd_host_websocket.go`, `nd_host_artwork.go`, `nd_host_cache.go`, and `nd_host_subsonicapi.go` to convert string errors from responses into Go errors. - Removed redundant error checks in test data plugins for cleaner code. - Ensured consistent error handling across all plugins to improve reliability and maintainability.
This commit is contained in:
parent
cab656dbe5
commit
06e6c09882
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -83,6 +84,11 @@ func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -55,5 +56,10 @@ func CodecEncode(data []byte) (*CodecEncodeResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -55,5 +56,10 @@ func CounterCount(name string) (*CounterCountResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -55,5 +56,10 @@ func EchoEcho(message string) (*EchoEchoResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -57,5 +58,10 @@ func ListItems(name string, filter Filter) (*ListItemsResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -57,5 +58,10 @@ func MathAdd(a int32, b int32) (*MathAddResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -70,6 +71,11 @@ func MetaGet(key string) (*MetaGetResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -99,5 +105,10 @@ func MetaSet(data map[string]any) (*MetaSetResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -42,5 +43,10 @@ func PingPing() (*PingPingResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -56,5 +57,10 @@ func SearchFind(query string) (*SearchFindResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -55,5 +56,10 @@ func StoreSave(item Item) (*StoreSaveResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -57,5 +58,10 @@ func UsersGet(id *string, filter *User) (*UsersGetResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -178,10 +178,7 @@ func connectAndSubscribe(tickers []string) error {
|
||||
// Connect to WebSocket using host function
|
||||
resp, err := WebSocketConnect(coinbaseWSEndpoint, nil, connectionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("WebSocket connection error: %v", err)
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return fmt.Errorf("WebSocket connection error: %s", resp.Error)
|
||||
return fmt.Errorf("WebSocket connection error: %w", err)
|
||||
}
|
||||
pdk.Log(pdk.LogInfo, fmt.Sprintf("Connected to Coinbase WebSocket API (connection: %s)", resp.NewConnectionID))
|
||||
|
||||
@ -198,12 +195,9 @@ func connectAndSubscribe(tickers []string) error {
|
||||
}
|
||||
|
||||
// Send subscription message
|
||||
sendResp, err := WebSocketSendText(connectionID, string(subscriptionJSON))
|
||||
_, err = WebSocketSendText(connectionID, string(subscriptionJSON))
|
||||
if err != nil {
|
||||
return fmt.Errorf("WebSocket send error: %v", err)
|
||||
}
|
||||
if sendResp.Error != "" {
|
||||
return fmt.Errorf("WebSocket send error: %s", sendResp.Error)
|
||||
return fmt.Errorf("WebSocket send error: %w", err)
|
||||
}
|
||||
|
||||
pdk.Log(pdk.LogInfo, "Subscription message sent to Coinbase WebSocket API")
|
||||
@ -272,11 +266,9 @@ func NdWebsocketOnClose(input OnCloseInput) (OnCloseOutput, error) {
|
||||
pdk.Log(pdk.LogInfo, "Scheduling reconnection attempt in 5 seconds...")
|
||||
|
||||
// Schedule a one-time reconnection attempt
|
||||
resp, err := SchedulerScheduleOneTime(5, "reconnect", reconnectScheduleID)
|
||||
_, err := SchedulerScheduleOneTime(5, "reconnect", reconnectScheduleID)
|
||||
if err != nil {
|
||||
pdk.Log(pdk.LogError, fmt.Sprintf("Failed to schedule reconnection: %v", err))
|
||||
} else if resp.Error != "" {
|
||||
pdk.Log(pdk.LogError, fmt.Sprintf("Failed to schedule reconnection: %s", resp.Error))
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,11 +317,9 @@ func ndSchedulerCallback() int32 {
|
||||
pdk.Log(pdk.LogError, fmt.Sprintf("Reconnection failed: %v - will retry in 10 seconds", err))
|
||||
|
||||
// Schedule another attempt
|
||||
resp, err := SchedulerScheduleOneTime(10, "reconnect", reconnectScheduleID)
|
||||
_, err := SchedulerScheduleOneTime(10, "reconnect", reconnectScheduleID)
|
||||
if err != nil {
|
||||
pdk.Log(pdk.LogError, fmt.Sprintf("Failed to schedule retry: %v", err))
|
||||
} else if resp.Error != "" {
|
||||
pdk.Log(pdk.LogError, fmt.Sprintf("Failed to schedule retry: %s", resp.Error))
|
||||
}
|
||||
} else {
|
||||
pdk.Log(pdk.LogInfo, "Successfully reconnected!")
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -101,6 +102,11 @@ func SchedulerScheduleOneTime(delaySeconds int32, payload string, scheduleID str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -141,6 +147,11 @@ func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -176,5 +187,10 @@ func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -120,6 +121,11 @@ func WebSocketConnect(url string, headers map[string]string, connectionID string
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -157,6 +163,11 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -194,6 +205,11 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -233,5 +249,10 @@ func WebSocketCloseConnection(connectionID string, code int32, reason string) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -129,10 +129,6 @@ func getImageURL(trackID string) string {
|
||||
pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to get artwork URL: %v", err))
|
||||
return ""
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to get artwork URL: %s", resp.Error))
|
||||
return ""
|
||||
}
|
||||
|
||||
// Don't use localhost URLs
|
||||
if strings.HasPrefix(resp.Url, "http://localhost") {
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -115,6 +116,11 @@ func ArtworkGetArtistUrl(id string, size int32) (*ArtworkGetArtistUrlResponse, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -152,6 +158,11 @@ func ArtworkGetAlbumUrl(id string, size int32) (*ArtworkGetAlbumUrlResponse, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -189,6 +200,11 @@ func ArtworkGetTrackUrl(id string, size int32) (*ArtworkGetTrackUrlResponse, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -226,5 +242,10 @@ func ArtworkGetPlaylistUrl(id string, size int32) (*ArtworkGetPlaylistUrlRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -216,6 +217,11 @@ func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetString
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -252,6 +258,11 @@ func CacheGetString(key string) (*CacheGetStringResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -291,6 +302,11 @@ func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -327,6 +343,11 @@ func CacheGetInt(key string) (*CacheGetIntResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -366,6 +387,11 @@ func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -402,6 +428,11 @@ func CacheGetFloat(key string) (*CacheGetFloatResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -441,6 +472,11 @@ func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesRe
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -477,6 +513,11 @@ func CacheGetBytes(key string) (*CacheGetBytesResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -512,6 +553,11 @@ func CacheHas(key string) (*CacheHasResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -547,5 +593,10 @@ func CacheRemove(key string) (*CacheRemoveResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -101,6 +102,11 @@ func SchedulerScheduleOneTime(delaySeconds int32, payload string, scheduleID str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -141,6 +147,11 @@ func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -176,5 +187,10 @@ func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -120,6 +121,11 @@ func WebSocketConnect(url string, headers map[string]string, connectionID string
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -157,6 +163,11 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -194,6 +205,11 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -233,5 +249,10 @@ func WebSocketCloseConnection(connectionID string, code int32, reason string) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -183,13 +183,10 @@ func sendMessage(username string, opCode int, payload any) error {
|
||||
return fmt.Errorf("failed to marshal message: %w", err)
|
||||
}
|
||||
|
||||
resp, err := WebSocketSendText(username, string(b))
|
||||
_, err = WebSocketSendText(username, string(b))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send message: %w", err)
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return fmt.Errorf("failed to send message: %s", resp.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -224,17 +221,13 @@ func cleanupFailedConnection(username string) {
|
||||
pdk.Log(pdk.LogInfo, fmt.Sprintf("Cleaning up failed connection for user %s", username))
|
||||
|
||||
// Cancel the heartbeat schedule
|
||||
if resp, err := SchedulerCancelSchedule(username); err != nil {
|
||||
if _, err := SchedulerCancelSchedule(username); err != nil {
|
||||
pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to cancel heartbeat schedule for user %s: %v", username, err))
|
||||
} else if resp.Error != "" {
|
||||
pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to cancel heartbeat schedule for user %s: %s", username, resp.Error))
|
||||
}
|
||||
|
||||
// Close the WebSocket connection
|
||||
if resp, err := WebSocketCloseConnection(username, 1000, "Connection lost"); err != nil {
|
||||
if _, err := WebSocketCloseConnection(username, 1000, "Connection lost"); err != nil {
|
||||
pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to close WebSocket connection for user %s: %v", username, err))
|
||||
} else if resp.Error != "" {
|
||||
pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to close WebSocket connection for user %s: %s", username, resp.Error))
|
||||
}
|
||||
|
||||
// Clean up cache entries
|
||||
@ -269,13 +262,10 @@ func connect(username, token string) error {
|
||||
pdk.Log(pdk.LogDebug, fmt.Sprintf("Using gateway: %s", gateway))
|
||||
|
||||
// Connect to Discord Gateway
|
||||
resp, err := WebSocketConnect(gateway, nil, username)
|
||||
_, err = WebSocketConnect(gateway, nil, username)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to WebSocket: %w", err)
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return fmt.Errorf("failed to connect to WebSocket: %s", resp.Error)
|
||||
}
|
||||
|
||||
// Send identify payload
|
||||
payload := identifyPayload{
|
||||
@ -305,16 +295,12 @@ func connect(username, token string) error {
|
||||
|
||||
// disconnect closes the Discord connection for a user.
|
||||
func disconnect(username string) error {
|
||||
if resp, err := SchedulerCancelSchedule(username); err != nil {
|
||||
if _, err := SchedulerCancelSchedule(username); err != nil {
|
||||
return fmt.Errorf("failed to cancel schedule: %w", err)
|
||||
} else if resp.Error != "" {
|
||||
return fmt.Errorf("failed to cancel schedule: %s", resp.Error)
|
||||
}
|
||||
|
||||
if resp, err := WebSocketCloseConnection(username, 1000, "Navidrome disconnect"); err != nil {
|
||||
if _, err := WebSocketCloseConnection(username, 1000, "Navidrome disconnect"); err != nil {
|
||||
return fmt.Errorf("failed to close WebSocket connection: %w", err)
|
||||
} else if resp.Error != "" {
|
||||
return fmt.Errorf("failed to close WebSocket connection: %s", resp.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -337,10 +323,8 @@ func handleWebSocketMessage(connectionID, message string) error {
|
||||
if v := msg["s"]; v != nil {
|
||||
seq := int64(v.(float64))
|
||||
pdk.Log(pdk.LogTrace, fmt.Sprintf("Received sequence number for connection '%s': %d", connectionID, seq))
|
||||
if resp, err := CacheSetInt(fmt.Sprintf("discord.seq.%s", connectionID), seq, int64(heartbeatInterval*2)); err != nil {
|
||||
if _, err := CacheSetInt(fmt.Sprintf("discord.seq.%s", connectionID), seq, int64(heartbeatInterval*2)); err != nil {
|
||||
return fmt.Errorf("failed to store sequence number for user %s: %w", connectionID, err)
|
||||
} else if resp.Error != "" {
|
||||
return fmt.Errorf("failed to store sequence number for user %s: %s", connectionID, resp.Error)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -115,6 +116,11 @@ func ArtworkGetArtistUrl(id string, size int32) (*ArtworkGetArtistUrlResponse, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -152,6 +158,11 @@ func ArtworkGetAlbumUrl(id string, size int32) (*ArtworkGetAlbumUrlResponse, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -189,6 +200,11 @@ func ArtworkGetTrackUrl(id string, size int32) (*ArtworkGetTrackUrlResponse, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -226,5 +242,10 @@ func ArtworkGetPlaylistUrl(id string, size int32) (*ArtworkGetPlaylistUrlRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -216,6 +217,11 @@ func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetString
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -252,6 +258,11 @@ func CacheGetString(key string) (*CacheGetStringResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -291,6 +302,11 @@ func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -327,6 +343,11 @@ func CacheGetInt(key string) (*CacheGetIntResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -366,6 +387,11 @@ func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -402,6 +428,11 @@ func CacheGetFloat(key string) (*CacheGetFloatResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -441,6 +472,11 @@ func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesRe
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -477,6 +513,11 @@ func CacheGetBytes(key string) (*CacheGetBytesResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -512,6 +553,11 @@ func CacheHas(key string) (*CacheHasResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -547,5 +593,10 @@ func CacheRemove(key string) (*CacheRemoveResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -101,6 +102,11 @@ func SchedulerScheduleOneTime(delaySeconds int32, payload string, scheduleID str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -141,6 +147,11 @@ func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -176,5 +187,10 @@ func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -59,5 +60,10 @@ func SubsonicAPICall(uri string) (*SubsonicAPICallResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -120,6 +121,11 @@ func WebSocketConnect(url string, headers map[string]string, connectionID string
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -157,6 +163,11 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -194,6 +205,11 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -233,5 +249,10 @@ func WebSocketCloseConnection(connectionID string, code int32, reason string) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
16
plugins/testdata/fake-artwork/main.go
vendored
16
plugins/testdata/fake-artwork/main.go
vendored
@ -80,10 +80,6 @@ func ndTestArtwork() int32 {
|
||||
resp, e := ArtworkGetArtistUrl(input.ID, input.Size)
|
||||
if e != nil {
|
||||
err = e
|
||||
} else if resp.Error != "" {
|
||||
errStr := resp.Error
|
||||
pdk.OutputJSON(TestOutput{Error: &errStr})
|
||||
return 0
|
||||
} else {
|
||||
url = resp.Url
|
||||
}
|
||||
@ -91,10 +87,6 @@ func ndTestArtwork() int32 {
|
||||
resp, e := ArtworkGetAlbumUrl(input.ID, input.Size)
|
||||
if e != nil {
|
||||
err = e
|
||||
} else if resp.Error != "" {
|
||||
errStr := resp.Error
|
||||
pdk.OutputJSON(TestOutput{Error: &errStr})
|
||||
return 0
|
||||
} else {
|
||||
url = resp.Url
|
||||
}
|
||||
@ -102,10 +94,6 @@ func ndTestArtwork() int32 {
|
||||
resp, e := ArtworkGetTrackUrl(input.ID, input.Size)
|
||||
if e != nil {
|
||||
err = e
|
||||
} else if resp.Error != "" {
|
||||
errStr := resp.Error
|
||||
pdk.OutputJSON(TestOutput{Error: &errStr})
|
||||
return 0
|
||||
} else {
|
||||
url = resp.Url
|
||||
}
|
||||
@ -113,10 +101,6 @@ func ndTestArtwork() int32 {
|
||||
resp, e := ArtworkGetPlaylistUrl(input.ID, input.Size)
|
||||
if e != nil {
|
||||
err = e
|
||||
} else if resp.Error != "" {
|
||||
errStr := resp.Error
|
||||
pdk.OutputJSON(TestOutput{Error: &errStr})
|
||||
return 0
|
||||
} else {
|
||||
url = resp.Url
|
||||
}
|
||||
|
||||
21
plugins/testdata/fake-artwork/nd_host_artwork.go
vendored
21
plugins/testdata/fake-artwork/nd_host_artwork.go
vendored
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -115,6 +116,11 @@ func ArtworkGetArtistUrl(id string, size int32) (*ArtworkGetArtistUrlResponse, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -152,6 +158,11 @@ func ArtworkGetAlbumUrl(id string, size int32) (*ArtworkGetAlbumUrlResponse, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -189,6 +200,11 @@ func ArtworkGetTrackUrl(id string, size int32) (*ArtworkGetTrackUrlResponse, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -226,5 +242,10 @@ func ArtworkGetPlaylistUrl(id string, size int32) (*ArtworkGetPlaylistUrlRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
5
plugins/testdata/fake-scheduler/main.go
vendored
5
plugins/testdata/fake-scheduler/main.go
vendored
@ -68,14 +68,11 @@ func NdSchedulerCallback(input SchedulerCallbackInput) (SchedulerCallbackOutput,
|
||||
}
|
||||
case len(input.Payload) > 19 && input.Payload[:19] == "schedule-duplicate:":
|
||||
duplicateID := input.Payload[19:]
|
||||
resp, err := SchedulerScheduleOneTime(60, "duplicate-attempt", duplicateID)
|
||||
_, err := SchedulerScheduleOneTime(60, "duplicate-attempt", duplicateID)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
return SchedulerCallbackOutput{Error: &errStr}, nil
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return SchedulerCallbackOutput{Error: &resp.Error}, nil
|
||||
}
|
||||
}
|
||||
return SchedulerCallbackOutput{}, nil
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -101,6 +102,11 @@ func SchedulerScheduleOneTime(delaySeconds int32, payload string, scheduleID str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -141,6 +147,11 @@ func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -176,5 +187,10 @@ func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -66,12 +66,6 @@ func callSubsonicAPIExport() int32 {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Check for error in response
|
||||
if response.Error != "" {
|
||||
pdk.SetErrorString("SubsonicAPI error: " + response.Error)
|
||||
return 1
|
||||
}
|
||||
|
||||
// Return the response
|
||||
pdk.OutputString(response.ResponseJSON)
|
||||
return 0
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -59,5 +60,10 @@ func SubsonicAPICall(uri string) (*SubsonicAPICallResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
12
plugins/testdata/fake-websocket/main.go
vendored
12
plugins/testdata/fake-websocket/main.go
vendored
@ -81,28 +81,20 @@ func ndWebSocketOnTextMessage() int32 {
|
||||
|
||||
switch input.Message {
|
||||
case "echo":
|
||||
resp, err := WebSocketSendText(input.ConnectionID, "echo:"+input.Message)
|
||||
_, err := WebSocketSendText(input.ConnectionID, "echo:"+input.Message)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(OnTextMessageOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(OnTextMessageOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
|
||||
case "close":
|
||||
resp, err := WebSocketCloseConnection(input.ConnectionID, 1000, "closed by plugin")
|
||||
_, err := WebSocketCloseConnection(input.ConnectionID, 1000, "closed by plugin")
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(OnTextMessageOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(OnTextMessageOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
|
||||
case "fail":
|
||||
errStr := "intentional test failure"
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -120,6 +121,11 @@ func WebSocketConnect(url string, headers map[string]string, connectionID string
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -157,6 +163,11 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -194,6 +205,11 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -233,5 +249,10 @@ func WebSocketCloseConnection(connectionID string, code int32, reason string) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
50
plugins/testdata/fake_cache_plugin/main.go
vendored
50
plugins/testdata/fake_cache_plugin/main.go
vendored
@ -81,16 +81,12 @@ func ndTestCache() int32 {
|
||||
|
||||
switch input.Operation {
|
||||
case "set_string":
|
||||
resp, err := CacheSetString(input.Key, input.StringVal, input.TTLSeconds)
|
||||
_, err := CacheSetString(input.Key, input.StringVal, input.TTLSeconds)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{})
|
||||
return 0
|
||||
|
||||
@ -101,24 +97,16 @@ func ndTestCache() int32 {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{StringVal: resp.Value, Exists: resp.Exists})
|
||||
return 0
|
||||
|
||||
case "set_int":
|
||||
resp, err := CacheSetInt(input.Key, input.IntVal, input.TTLSeconds)
|
||||
_, err := CacheSetInt(input.Key, input.IntVal, input.TTLSeconds)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{})
|
||||
return 0
|
||||
|
||||
@ -129,24 +117,16 @@ func ndTestCache() int32 {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{IntVal: resp.Value, Exists: resp.Exists})
|
||||
return 0
|
||||
|
||||
case "set_float":
|
||||
resp, err := CacheSetFloat(input.Key, input.FloatVal, input.TTLSeconds)
|
||||
_, err := CacheSetFloat(input.Key, input.FloatVal, input.TTLSeconds)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{})
|
||||
return 0
|
||||
|
||||
@ -157,24 +137,16 @@ func ndTestCache() int32 {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{FloatVal: resp.Value, Exists: resp.Exists})
|
||||
return 0
|
||||
|
||||
case "set_bytes":
|
||||
resp, err := CacheSetBytes(input.Key, input.BytesVal, input.TTLSeconds)
|
||||
_, err := CacheSetBytes(input.Key, input.BytesVal, input.TTLSeconds)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{})
|
||||
return 0
|
||||
|
||||
@ -185,10 +157,6 @@ func ndTestCache() int32 {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{BytesVal: resp.Value, Exists: resp.Exists})
|
||||
return 0
|
||||
|
||||
@ -199,24 +167,16 @@ func ndTestCache() int32 {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{Exists: resp.Exists})
|
||||
return 0
|
||||
|
||||
case "remove":
|
||||
resp, err := CacheRemove(input.Key)
|
||||
_, err := CacheRemove(input.Key)
|
||||
if err != nil {
|
||||
errStr := err.Error()
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
|
||||
return 0
|
||||
}
|
||||
if resp.Error != "" {
|
||||
pdk.OutputJSON(TestCacheOutput{Error: &resp.Error})
|
||||
return 0
|
||||
}
|
||||
pdk.OutputJSON(TestCacheOutput{})
|
||||
return 0
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
@ -216,6 +217,11 @@ func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetString
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -252,6 +258,11 @@ func CacheGetString(key string) (*CacheGetStringResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -291,6 +302,11 @@ func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntRespons
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -327,6 +343,11 @@ func CacheGetInt(key string) (*CacheGetIntResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -366,6 +387,11 @@ func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatR
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -402,6 +428,11 @@ func CacheGetFloat(key string) (*CacheGetFloatResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -441,6 +472,11 @@ func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesRe
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -477,6 +513,11 @@ func CacheGetBytes(key string) (*CacheGetBytesResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -512,6 +553,11 @@ func CacheHas(key string) (*CacheHasResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
@ -547,5 +593,10 @@ func CacheRemove(key string) (*CacheRemoveResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert Error field to Go error
|
||||
if response.Error != "" {
|
||||
return nil, errors.New(response.Error)
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user