Skip to content

Commit

Permalink
print error message on gateway for bad request lora errors (#3154)
Browse files Browse the repository at this point in the history
This commit updates the AI error handling behavoir so that BadRequest errors are forwarded to the user.

Co-authored-by: Rick Staa <[email protected]>
  • Loading branch information
eliteprox and rickstaa authored Sep 23, 2024
1 parent ffb1922 commit 71a2dcf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/golang/protobuf v1.5.4
github.com/jaypipes/ghw v0.10.0
github.com/jaypipes/pcidb v1.0.0
github.com/livepeer/ai-worker v0.5.0
github.com/livepeer/ai-worker v0.6.0
github.com/livepeer/go-tools v0.3.6-0.20240130205227-92479de8531b
github.com/livepeer/livepeer-data v0.7.5-0.20231004073737-06f1f383fb18
github.com/livepeer/lpms v0.0.0-20240819180416-f87352959b85
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,8 @@ github.com/libp2p/go-netroute v0.2.0 h1:0FpsbsvuSnAhXFnCY0VLFbJOzaK0VnP0r1QT/o4n
github.com/libp2p/go-netroute v0.2.0/go.mod h1:Vio7LTzZ+6hoT4CMZi5/6CpY3Snzh2vgZhWgxMNwlQI=
github.com/libp2p/go-openssl v0.1.0 h1:LBkKEcUv6vtZIQLVTegAil8jbNpJErQ9AnT+bWV+Ooo=
github.com/libp2p/go-openssl v0.1.0/go.mod h1:OiOxwPpL3n4xlenjx2h7AwSGaFSC/KZvf6gNdOBQMtc=
github.com/livepeer/ai-worker v0.5.0 h1:dgO6j9QVFPOq9omIcgB1YmgVSlhV94BMb6QO4WUocX8=
github.com/livepeer/ai-worker v0.5.0/go.mod h1:91lMzkzVuwR9kZ0EzXwf+7yVhLaNVmYAfmBtn7t3cQA=
github.com/livepeer/ai-worker v0.6.0 h1:sGldUavfbTXPQDKc1a80/zgK8G1VdYRAxiuFTP0YyOU=
github.com/livepeer/ai-worker v0.6.0/go.mod h1:91lMzkzVuwR9kZ0EzXwf+7yVhLaNVmYAfmBtn7t3cQA=
github.com/livepeer/go-tools v0.3.6-0.20240130205227-92479de8531b h1:VQcnrqtCA2UROp7q8ljkh2XA/u0KRgVv0S1xoUvOweE=
github.com/livepeer/go-tools v0.3.6-0.20240130205227-92479de8531b/go.mod h1:hwJ5DKhl+pTanFWl+EUpw1H7ukPO/H+MFpgA7jjshzw=
github.com/livepeer/joy4 v0.1.2-0.20191121080656-b2fea45cbded h1:ZQlvR5RB4nfT+cOQee+WqmaDOgGtP2oDMhcVvR4L0yA=
Expand Down
37 changes: 28 additions & 9 deletions server/ai_mediaserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ func (ls *LivepeerServer) TextToImage() http.Handler {
start := time.Now()
resp, err := processTextToImage(ctx, params, req)
if err != nil {
var e *ServiceUnavailableError
if errors.As(err, &e) {
var serviceUnavailableErr *ServiceUnavailableError
var badRequestErr *BadRequestError
if errors.As(err, &serviceUnavailableErr) {
respondJsonError(ctx, w, err, http.StatusServiceUnavailable)
return
}
if errors.As(err, &badRequestErr) {
respondJsonError(ctx, w, err, http.StatusBadRequest)
return
}
respondJsonError(ctx, w, err, http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -146,11 +151,16 @@ func (ls *LivepeerServer) ImageToImage() http.Handler {
start := time.Now()
resp, err := processImageToImage(ctx, params, req)
if err != nil {
var e *ServiceUnavailableError
if errors.As(err, &e) {
var serviceUnavailableErr *ServiceUnavailableError
var badRequestErr *BadRequestError
if errors.As(err, &serviceUnavailableErr) {
respondJsonError(ctx, w, err, http.StatusServiceUnavailable)
return
}
if errors.As(err, &badRequestErr) {
respondJsonError(ctx, w, err, http.StatusBadRequest)
return
}
respondJsonError(ctx, w, err, http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -202,12 +212,16 @@ func (ls *LivepeerServer) ImageToVideo() http.Handler {

resp, err := processImageToVideo(ctx, params, req)
if err != nil {
var e *ServiceUnavailableError
if errors.As(err, &e) {
var serviceUnavailableErr *ServiceUnavailableError
var badRequestErr *BadRequestError
if errors.As(err, &serviceUnavailableErr) {
respondJsonError(ctx, w, err, http.StatusServiceUnavailable)
return
}

if errors.As(err, &badRequestErr) {
respondJsonError(ctx, w, err, http.StatusBadRequest)
return
}
respondJsonError(ctx, w, err, http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -304,11 +318,16 @@ func (ls *LivepeerServer) Upscale() http.Handler {
start := time.Now()
resp, err := processUpscale(ctx, params, req)
if err != nil {
var e *ServiceUnavailableError
if errors.As(err, &e) {
var serviceUnavailableErr *ServiceUnavailableError
var badRequestErr *BadRequestError
if errors.As(err, &serviceUnavailableErr) {
respondJsonError(ctx, w, err, http.StatusServiceUnavailable)
return
}
if errors.As(err, &badRequestErr) {
respondJsonError(ctx, w, err, http.StatusBadRequest)
return
}
respondJsonError(ctx, w, err, http.StatusInternalServerError)
return
}
Expand Down
24 changes: 24 additions & 0 deletions server/ai_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ func (e *BadRequestError) Error() string {
return e.err.Error()
}

// parseBadRequestError checks if the error is a bad request error and returns a BadRequestError.
func parseBadRequestError(err error) *BadRequestError {
if err == nil {
return nil
}

const errorCode = "returned 400"
if !strings.Contains(err.Error(), errorCode) {
return nil
}

parts := strings.SplitN(err.Error(), errorCode, 2)
detail := strings.TrimSpace(parts[1])
if detail == "" {
detail = "bad request"
}

return &BadRequestError{err: errors.New(detail)}
}

type aiRequestParams struct {
node *core.LivepeerNode
os drivers.OSSession
Expand Down Expand Up @@ -897,6 +917,10 @@ func processAIRequest(ctx context.Context, params aiRequestParams, req interface
if errors.Is(err, common.ErrAudioDurationCalculation) {
return nil, &BadRequestError{err}
}

if badRequestErr := parseBadRequestError(err); badRequestErr != nil {
return nil, badRequestErr
}
}

if resp == nil {
Expand Down

0 comments on commit 71a2dcf

Please sign in to comment.