refactor(go bindings): fix backward compatibility for error andling logic

This commit is contained in:
ciricc 2025-09-14 02:04:06 +03:00
parent 01f5c6b708
commit ba990ab0a8
4 changed files with 34 additions and 3 deletions

View File

@ -11,13 +11,16 @@ import (
// ERRORS
var (
ErrUnableToLoadModel = errors.New("unable to load model")
ErrInternalAppError = errors.New("internal application error")
ErrUnableToLoadModel = errors.New("unable to load model")
// Deprecated: Use ErrModelClosed instead for checking the model is closed error
ErrInternalAppError = errors.New("internal application error")
ErrProcessingFailed = errors.New("processing failed")
ErrUnsupportedLanguage = errors.New("unsupported language")
ErrModelNotMultilingual = errors.New("model is not multilingual")
ErrUnableToCreateState = errors.New("unable to create state")
ErrModelClosed = errors.New("model has been closed")
ErrModelClosed = errors.Join(errors.New("model has been closed"), ErrInternalAppError)
)
///////////////////////////////////////////////////////////////////////////////

View File

@ -233,6 +233,11 @@ func (context *context) IsNOT(t Token) bool {
}
func (context *context) SetLanguage(lang string) error {
if context.model.whisperContext().IsClosed() {
// TODO: remove this logic after deprecating the ErrInternalAppError
return ErrModelClosed
}
if !context.model.IsMultilingual() {
return ErrModelNotMultilingual
}

View File

@ -273,9 +273,18 @@ func TestContext_Close(t *testing.T) {
err = ctx.Process([]float32{0.1, 0.2, 0.3}, nil, nil, nil)
require.ErrorIs(t, err, whisper.ErrModelClosed)
// TODO: remove this logic after deprecating the ErrInternalAppError
require.ErrorIs(t, err, whisper.ErrInternalAppError)
lang := ctx.DetectedLanguage()
require.Empty(t, lang)
_, err = ctx.NextSegment()
assert.ErrorIs(err, whisper.ErrModelClosed)
// TODO: remove this logic after deprecating the ErrInternalAppError
assert.ErrorIs(err, whisper.ErrInternalAppError)
// Multiple closes should be safe
err = ctx.Close()
require.NoError(t, err)

View File

@ -49,6 +49,20 @@ func TestNewContext(t *testing.T) {
assert.NotNil(context)
}
func TestNewContext_ClosedModel(t *testing.T) {
assert := assert.New(t)
model, err := whisper.New(ModelPath)
assert.NoError(err)
assert.NotNil(model)
assert.NoError(model.Close())
context, err := model.NewContext()
assert.ErrorIs(err, whisper.ErrInternalAppError)
assert.ErrorIs(err, whisper.ErrModelClosed)
assert.Nil(context)
}
func TestIsMultilingual(t *testing.T) {
assert := assert.New(t)