Serve 400 instead of 500 when invalid thumbnail parameters are requested
authorGilles Dubuc <gilles@wikimedia.org>
Thu, 24 Nov 2016 12:14:11 +0000 (13:14 +0100)
committerGilles Dubuc <gdubuc@wikimedia.org>
Tue, 29 Nov 2016 08:50:42 +0000 (09:50 +0100)
This was requested because of 0px thumbnail requests, but there are
other cases where parameters are detected as invalid and 400 is
semantically more correct than 500 in that situation.

Bug: T147784
Change-Id: I4d24a93e655f04d8119e77798d5df5a45caaafcf

includes/media/MediaTransformOutput.php
thumb.php

index b3a555a..46b9674 100644 (file)
@@ -476,6 +476,10 @@ class MediaTransformError extends MediaTransformOutput {
        function isError() {
                return true;
        }
+
+       function getHttpStatusCode() {
+               return 500;
+       }
 }
 
 /**
@@ -490,6 +494,10 @@ class TransformParameterError extends MediaTransformError {
                        max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
                        wfMessage( 'thumbnail_invalid_params' )->text() );
        }
+
+       function getHttpStatusCode() {
+               return 400;
+       }
 }
 
 /**
@@ -511,4 +519,8 @@ class TransformTooBigImageAreaError extends MediaTransformError {
                                )->text()
                        );
        }
+
+       function getHttpStatusCode() {
+               return 400;
+       }
 }
index fca25c5..c38b89c 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -341,6 +341,7 @@ function wfStreamThumb( array $params ) {
        // Check for thumbnail generation errors...
        $msg = wfMessage( 'thumbnail_error' );
        $errorCode = 500;
+
        if ( !$thumb ) {
                $errorMsg = $errorMsg ?: $msg->rawParams( 'File::transform() returned false' )->escaped();
                if ( $errorMsg instanceof MessageSpecifier &&
@@ -350,6 +351,7 @@ function wfStreamThumb( array $params ) {
                }
        } elseif ( $thumb->isError() ) {
                $errorMsg = $thumb->getHtmlMsg();
+               $errorCode = $thumb->getHttpStatusCode();
        } elseif ( !$thumb->hasFile() ) {
                $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
        } elseif ( $thumb->fileIsSource() ) {