Improve error handling with xerrors package
- Replace custom error handling with xerrors package - Enhance error descriptions for better debugging - Add text utilities for string processing - Update error tests to use standard errors package - Add String() method to GeminiError 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/antanst/go_errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GeminiError is used to represent
|
// GeminiError is used to represent
|
||||||
@@ -20,6 +19,10 @@ func (e *GeminiError) Error() string {
|
|||||||
return fmt.Sprintf("gemini error: code %d %s", e.Code, e.Msg)
|
return fmt.Sprintf("gemini error: code %d %s", e.Code, e.Msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *GeminiError) String() string {
|
||||||
|
return e.Error()
|
||||||
|
}
|
||||||
|
|
||||||
// NewGeminiError creates a new GeminiError based on the status code and header.
|
// NewGeminiError creates a new GeminiError based on the status code and header.
|
||||||
// Status codes are based on the Gemini protocol specification:
|
// Status codes are based on the Gemini protocol specification:
|
||||||
// - 1x: Input required
|
// - 1x: Input required
|
||||||
@@ -57,5 +60,5 @@ func IsGeminiError(err error) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var asError *GeminiError
|
var asError *GeminiError
|
||||||
return go_errors.As(err, &asError)
|
return errors.As(err, &asError)
|
||||||
}
|
}
|
||||||
|
|||||||
36
gemini/errors_test.go
Normal file
36
gemini/errors_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package gemini
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestErrGemini(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
err := NewGeminiError(50, "50 server error")
|
||||||
|
if !errors.As(err, new(*GeminiError)) {
|
||||||
|
t.Errorf("TestErrGemini fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrGeminiWrapped(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
err := NewGeminiError(50, "50 server error")
|
||||||
|
errWrapped := fmt.Errorf("%w wrapped", err)
|
||||||
|
if !errors.As(errWrapped, new(*GeminiError)) {
|
||||||
|
t.Errorf("TestErrGeminiWrapped fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsGeminiError(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
err1 := NewGeminiError(50, "50 server error")
|
||||||
|
if !IsGeminiError(err1) {
|
||||||
|
t.Errorf("TestGeminiError fail #1")
|
||||||
|
}
|
||||||
|
wrappedErr1 := fmt.Errorf("wrapped %w", err1)
|
||||||
|
if !IsGeminiError(wrappedErr1) {
|
||||||
|
t.Errorf("TestGeminiError fail #2")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user