feat: init improvements (#174)
This commit is contained in:
@@ -18,12 +18,16 @@ package veocr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
"github.com/volcengine/volc-sdk-golang/service/visual"
|
||||
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/document/ocr"
|
||||
)
|
||||
@@ -52,10 +56,14 @@ func (o *ocrImpl) FromBase64(ctx context.Context, b64 string) ([]string, error)
|
||||
|
||||
resp, statusCode, err := o.config.Client.OCRNormal(form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, o.handleError(fmt.Errorf("[ve_ocr][FromBase64] OCRNormal err: %w", err))
|
||||
}
|
||||
if statusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("[FromBase64] failed, status code=%d", statusCode)
|
||||
err = fmt.Errorf("[ve_ocr][FromBase64] OCRNormal failed, status code=%d", statusCode)
|
||||
if statusCode == http.StatusBadRequest {
|
||||
return nil, errorx.WrapByCode(err, errno.ErrKnowledgeNonRetryableCode)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.Data.LineTexts, nil
|
||||
@@ -67,10 +75,14 @@ func (o *ocrImpl) FromURL(ctx context.Context, url string) ([]string, error) {
|
||||
|
||||
resp, statusCode, err := o.config.Client.OCRNormal(form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, o.handleError(fmt.Errorf("[ve_ocr][FromURL] OCRNormal error: %w", err))
|
||||
}
|
||||
if statusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("[FromBase64] failed, status code=%d", statusCode)
|
||||
err = fmt.Errorf("[ve_ocr][FromURL] OCRNormal failed, status code=%d", statusCode)
|
||||
if statusCode == http.StatusBadRequest {
|
||||
return nil, errorx.WrapByCode(err, errno.ErrKnowledgeNonRetryableCode)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.Data.LineTexts, nil
|
||||
@@ -94,3 +106,21 @@ func (o *ocrImpl) newForm() url.Values {
|
||||
}
|
||||
return form
|
||||
}
|
||||
|
||||
func (o *ocrImpl) handleError(err error) error {
|
||||
var (
|
||||
apiErr = &model.APIError{}
|
||||
reqErr = &model.RequestError{}
|
||||
)
|
||||
if errors.As(err, &apiErr) {
|
||||
if apiErr.HTTPStatusCode >= http.StatusInternalServerError ||
|
||||
apiErr.HTTPStatusCode == http.StatusTooManyRequests {
|
||||
return err
|
||||
}
|
||||
} else if errors.As(err, &reqErr) {
|
||||
if reqErr.HTTPStatusCode >= http.StatusInternalServerError {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return errorx.WrapByCode(err, errno.ErrKnowledgeNonRetryableCode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user