Browse Source

Respond with 422 on error during image loading

DarthSim 2 years ago
parent
commit
b3905c0cd3
2 changed files with 10 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 9 1
      vips/vips.go

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@
 
 ### Change
 - Preserve GIF's bit-per-sample.
+- Respond with 422 on error during image loading.
 
 ## [3.17.0] - 2023-05-10
 ### Add

+ 9 - 1
vips/vips.go

@@ -11,6 +11,7 @@ import (
 	"math"
 	"os"
 	"runtime"
+	"strings"
 	"sync"
 	"unsafe"
 
@@ -152,7 +153,14 @@ func Cleanup() {
 
 func Error() error {
 	defer C.vips_error_clear()
-	return ierrors.NewUnexpected(C.GoString(C.vips_error_buffer()), 1)
+
+	errstr := strings.TrimSpace(C.GoString(C.vips_error_buffer()))
+
+	if strings.Contains(errstr, "load_buffer: ") {
+		return ierrors.New(422, errstr, "Broken or unsupported image")
+	}
+
+	return ierrors.NewUnexpected(errstr, 1)
 }
 
 func hasOperation(name string) bool {