fix(plugins): enhance error handling in checkErr function

Improved the error handling logic in the checkErr function to map specific error strings to their corresponding API error constants. This change ensures that errors from plugins are correctly identified and returned, enhancing the robustness of error reporting.

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan
2025-07-09 14:32:43 -03:00
parent 9e97d0a9d9
commit 1166a0fabf
2 changed files with 135 additions and 14 deletions
+30 -6
View File
@@ -119,17 +119,41 @@ type errorResponse interface {
// checkErr returns an updated error if the response implements errorResponse and contains an error message.
// If the response is nil, it returns the original error. Otherwise, it wraps or creates an error as needed.
// It also maps error strings to their corresponding api.Err* constants.
func checkErr[T any](resp T, err error) (T, error) {
if any(resp) == nil {
return resp, err
return resp, mapAPIError(err)
}
respErr, ok := any(resp).(errorResponse)
if ok && respErr.GetError() != "" {
if err == nil {
err = errors.New(respErr.GetError())
} else {
err = fmt.Errorf("%s: %w", respErr.GetError(), err)
respErrMsg := respErr.GetError()
respErrErr := errors.New(respErrMsg)
mappedErr := mapAPIError(respErrErr)
// Check if the error was mapped to an API error (different from the temp error)
if errors.Is(mappedErr, api.ErrNotImplemented) || errors.Is(mappedErr, api.ErrNotFound) {
// Return the mapped API error instead of wrapping
return resp, mappedErr
}
// For non-API errors, use wrap the original error if it is not nil
return resp, errors.Join(respErrErr, err)
}
return resp, mapAPIError(err)
}
// mapAPIError maps error strings to their corresponding api.Err* constants.
// This is needed as errors from plugins may not be of type api.Error, due to serialization/deserialization.
func mapAPIError(err error) error {
if err == nil {
return nil
}
errStr := err.Error()
switch errStr {
case api.ErrNotImplemented.Error():
return api.ErrNotImplemented
case api.ErrNotFound.Error():
return api.ErrNotFound
default:
return err
}
return resp, err
}