r74059 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74058‎ | r74059 | r74060 >
Date:07:01, 1 October 2010
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
Changes for 0.4 - work on error handling
Modified paths:
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/ValidationError.php (modified) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ValidationError.php
@@ -54,4 +54,15 @@
5555 $this->tags = $tags;
5656 }
5757
 58+ /**
 59+ * Returns the error message describing the error.
 60+ *
 61+ * @since 0.4
 62+ *
 63+ * @return string
 64+ */
 65+ public function getMessage() {
 66+ return $this->message;
 67+ }
 68+
5869 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -156,18 +156,30 @@
157157
158158 $this->validator->validateParameters();
159159
160 - if ( $this->validator->hasFatalError() ) {
161 - // TODO
162 - $output = 'Demo: fatal error';
 160+ $fatalError = $this->validator->hasFatalError();
 161+
 162+ if ( $fatalError === false ) {
 163+ $output = $this->render( $this->validator->getParameterValues() );
163164 }
164165 else {
165 - $output = $this->render( $this->validator->getParameterValues() );
 166+ $output = $this->renderError( $fatalError );
166167 }
167168
168169 return $output;
169170 }
170171
171172 /**
 173+ * Creates and returns the output when a fatal error prevent regular rendering.
 174+ *
 175+ * @since 0.4
 176+ *
 177+ * @return string
 178+ */
 179+ protected function renderError( ValidationError $error ) {
 180+ return wfMsgExt( 'validator-error', 'parsemag', $error->getMessage() );
 181+ }
 182+
 183+ /**
172184 * Returns an array containing the parameter info.
173185 * Override in deriving classes to add parameter info.
174186 *
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -441,8 +441,8 @@
442442 protected function doValidation() {
443443 if ( $this->setCount == 0 ) {
444444 if ( $this->isRequired() ) {
445 - // TODO: fatal error
446 - $success = false;
 445+ // This should not occur, so thorw an exception.
 446+ throw new Exception( 'Attempted to validate a required parameter without first setting a value.' );
447447 }
448448 else {
449449 $success = true;
Index: trunk/extensions/Validator/includes/Validator.php
@@ -243,8 +243,14 @@
244244
245245 $setUservalue = $this->attemptToSetUserValue( $parameter );
246246
 247+ // If the parameter is required but not provided, register a fatal error and stop processing.
247248 if ( !$setUservalue && $parameter->isRequired() ) {
248 - // TODO: FATAL
 249+ $this->registerNewError(
 250+ wfMsgExt( 'validator_error_required_missing', 'parsemag', $paramName ),
 251+ array(),
 252+ ValidationError::SEVERITY_CRITICAL
 253+ );
 254+ break;
249255 }
250256 else {
251257 $validationSucceeded = $parameter->validate();
@@ -375,22 +381,20 @@
376382 }
377383
378384 /**
379 - * Returns wether there are any fatal errors. Fatal errors are either missing or invalid required parameters,
380 - * or simply any sort of error when the validation level is equal to (or bigger then) Validator_ERRORS_STRICT.
 385+ * Returns false when there are no fatal errors or an ValidationError when one is found.
 386+ * Fatal errors are either missing or invalid required parameters, or simply any sort of
 387+ * error when the validation level is equal to (or bigger then) Validator_ERRORS_STRICT.
381388 *
382 - * @return boolean
 389+ * @return mixed false or ValidationError
383390 */
384391 public function hasFatalError() {
385 - $has = false;
386 -
387392 foreach ( $this->errors as $error ) {
388393 if ( $error->severity >= ValidationError::SEVERITY_CRITICAL ) {
389 - $has = true;
390 - break;
 394+ return $error;
391395 }
392396 }
393397
394 - return $has;
 398+ return false;
395399 }
396400
397401 }
\ No newline at end of file
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -21,8 +21,9 @@
2222 'validator_warning_parameters' => 'There {{PLURAL:$1|is an error|are errors}} in your syntax.',
2323
2424 // General errors
 25+ 'validator-error' => '<b>Error</b>: $1',
2526 'validator_error_unknown_argument' => '$1 is not a valid parameter.',
26 - 'validator_error_required_missing' => 'The required parameter $1 is not provided.',
 27+ 'validator_error_required_missing' => 'The required parameter "$1" is not provided.',
2728 'validator-error-override-argument' => 'Tried to override parameter $1 (value: $2) with value "$3"',
2829
2930 // Criteria errors for single values

Comments

#Comment by Siebrand (talk | contribs)   07:23, 1 October 2010

Could is be an idea to use the CSS class "error" here, instead of bolding it?

#Comment by Jeroen De Dauw (talk | contribs)   09:15, 1 October 2010

Sure. What html element do I need for that? Also, should I be putting that in the message?

#Comment by Siebrand (talk | contribs)   10:59, 1 October 2010

I'd say <span class="error">Error: $1</span> and keep the span code out of the message (which will then just be "Error: $1").

#Comment by Jeroen De Dauw (talk | contribs)   11:13, 1 October 2010

Then I can't put emphasis on the "Error" part like done now, but it would allow proper escaping... Is there any "good" way of having any sort of markup in a message? Wikitext maybe?

#Comment by Raymond (talk | contribs)   11:17, 1 October 2010

For consistency with most other error messages I suggest to use Siebrands suggestion. Why do you want to emphasis the "Error" part only?

#Comment by Jeroen De Dauw (talk | contribs)   11:21, 1 October 2010

Because it makes it more obvious that's it's an error. I don't really care that much though, as it's just layout anyway, but the same markup-in-message situation occurs at other places as well, so that's why I want to know.

#Comment by Raymond (talk | contribs)   11:39, 1 October 2010

Or maybe more obviously: Wrap complete error message in <span class="errorbox";>...</span>:

Furthermore I think that single messages instead of lego are easier to translate:

'validator-warning' => 'Warning: There are non critical errors in your syntax.', (or similar)
'validator-error' => 'Error: There are errors in your syntax.',
'validator-fatal-error' => 'Fatal error: There are fatal errors in your syntax.',
'validator-warning-adittional-errors' => '... and multiple more issues.',
#Comment by Jeroen De Dauw (talk | contribs)   12:46, 1 October 2010

There are currently about a dozen specific error messages. It seems sort of silly to go create 3 versions of each. Or is this not silly from a translators point of view?

#Comment by Raymond (talk | contribs)   13:22, 1 October 2010

Maybe I misread the function renderErrors(). Aren't the above 3 messages the generic types (warning, error, faral error), are they? And you add the specific errors in a bulleted list?

If it is more complex pls ignore my comments.

#Comment by Jeroen De Dauw (talk | contribs)   13:26, 1 October 2010

ATM it is pretty much like you describe, but the code is (very) incomplete - the idea is to have it like I described.

Status & tagging log