2023-03-21 20:58:16 +03:00
|
|
|
package imagedata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
2023-03-22 20:25:51 +03:00
|
|
|
"github.com/imgproxy/imgproxy/v3/security"
|
2023-03-21 20:58:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type httpError interface {
|
|
|
|
Timeout() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapError(err error) error {
|
|
|
|
isTimeout := false
|
|
|
|
|
2023-03-22 20:25:51 +03:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
|
|
isTimeout = true
|
|
|
|
case errors.Is(err, context.Canceled):
|
2023-03-21 20:58:16 +03:00
|
|
|
return ierrors.New(
|
|
|
|
499,
|
|
|
|
fmt.Sprintf("The image request is cancelled: %s", err),
|
|
|
|
msgSourceImageIsUnreachable,
|
|
|
|
)
|
2023-03-22 20:25:51 +03:00
|
|
|
case errors.Is(err, security.ErrSourceAddressNotAllowed), errors.Is(err, security.ErrInvalidSourceAddress):
|
|
|
|
return ierrors.New(
|
|
|
|
404,
|
|
|
|
err.Error(),
|
|
|
|
msgSourceImageIsUnreachable,
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
if httpErr, ok := err.(httpError); ok {
|
|
|
|
isTimeout = httpErr.Timeout()
|
|
|
|
}
|
2023-03-21 20:58:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isTimeout {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ierr := ierrors.New(
|
|
|
|
http.StatusGatewayTimeout,
|
|
|
|
fmt.Sprintf("The image request timed out: %s", err),
|
|
|
|
msgSourceImageIsUnreachable,
|
|
|
|
)
|
|
|
|
ierr.Unexpected = true
|
|
|
|
|
|
|
|
return ierr
|
|
|
|
}
|