MarkAHershberger has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/260582

Change subject: Better error handling for HTMLForm
......................................................................

Better error handling for HTMLForm

* Addresses an old @todo that requested better error handling.
* Eliminates a notice when getFormFields() doesn't return an array.

Change-Id: I7c33b672a9d8516a2fe14030a914800791a913e3
---
M includes/htmlform/HTMLForm.php
1 file changed, 14 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/82/260582/1

diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php
index b0d90af..ba904f4 100644
--- a/includes/htmlform/HTMLForm.php
+++ b/includes/htmlform/HTMLForm.php
@@ -307,6 +307,11 @@
                $loadedDescriptor = array();
                $this->mFlatFields = array();
 
+               if ( ( ! is_array( $descriptor ) ) ) {
+                       wfDebugLog( __METHOD__,
+                               '$descriptor is not an array of fields.' );
+                       $descriptor = array();
+               }
                foreach ( $descriptor as $fieldname => $info ) {
                        $section = isset( $info['section'] )
                                ? $info['section']
@@ -411,18 +416,21 @@
         * @return string Name of a HTMLFormField subclass
         */
        public static function getClassFromDescriptor( $fieldname, &$descriptor 
) {
+               $err = "a 'class' or 'type' given.";
+               $class = null;
                if ( isset( $descriptor['class'] ) ) {
                        $class = $descriptor['class'];
+                       $err = "a valid class (class: '$class').  Maybe use 
'cssclass'?";
                } elseif ( isset( $descriptor['type'] ) ) {
-                       $class = static::$typeMappings[$descriptor['type']];
+                       $class = isset( 
static::$typeMappings[$descriptor['type']] )
+                               ? static::$typeMappings[$descriptor['type']] : 
null;
                        $descriptor['class'] = $class;
-               } else {
-                       $class = null;
+                       $err = "a mapping for type '{$descriptor['type']}'";
                }
 
-               if ( !$class ) {
-                       throw new MWException( "Descriptor with no class for 
$fieldname: "
-                               . print_r( $descriptor, true ) );
+               if ( !class_exists( $class ) ) {
+                       throw new MWException( "Tried to create an input form 
field" .
+                               " for '$fieldname' without $err" );
                }
 
                return $class;
@@ -448,9 +456,6 @@
                        $descriptor['parent'] = $parent;
                }
 
-               # @todo This will throw a fatal error whenever someone try to 
use
-               # 'class' to feed a CSS class instead of 'cssclass'. Would be
-               # great to avoid the fatal error and show a nice error.
                $obj = new $class( $descriptor );
 
                return $obj;

-- 
To view, visit https://gerrit.wikimedia.org/r/260582
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7c33b672a9d8516a2fe14030a914800791a913e3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <m...@nichework.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to