r74751 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74750‎ | r74751 | r74752 >
Date:23:55, 13 October 2010
Author:neilk
Status:ok
Tags:
Comment:
raise better errors if stashing fails
Modified paths:
  • /branches/uploadwizard/phase3/includes/upload/SessionStash.php (modified) (history)

Diff [purge]

Index: branches/uploadwizard/phase3/includes/upload/SessionStash.php
@@ -90,13 +90,15 @@
9191 }
9292
9393 /**
94 - * Stash a file in a temp directory and record that we did this in the session, along with other parameters.
 94+ * Stash a file in a temp directory and record that we did this in the session, along with other metadata.
9595 * We store data in a flat key-val namespace because that's how UploadBase did it. This also means we have to
9696 * ensure that the key-val pairs in $data do not overwrite other required fields.
9797 *
9898 * @param {String} $path: path to file you want stashed
99 - * @param {Array} $data: optional, other data you want added to the session. Do not use 'mTempPath', 'mFileProps', 'mFileSize', or 'version' as keys here
100 - * @param {String} $key: optional, unique key for this session. Used for directory hashing when storing, otherwise not important
 99+ * @param {Array} $data: optional, other data you want associated with the file. Do not use 'mTempPath', 'mFileProps', 'mFileSize', or 'version' as keys here
 100+ * @param {String} $key: optional, unique key for this file in this session. Used for directory hashing when storing, otherwise not important
 101+ * @throws SessionStashBadPathException
 102+ * @throws SessionStashFileException
101103 * @return {null|SessionStashFile} file, or null on failure
102104 */
103105 public function stashFile( $path, $data = array(), $key = null ) {
@@ -112,9 +114,21 @@
113115 }
114116
115117 // if not already in a temporary area, put it there
116 - $status = $this->repo->storeTemp( basename($path), $path );
117 - if( !$status->isOK() ) {
118 - return null;
 118+ $status = $this->repo->storeTemp( basename( $path ), $path );
 119+ if( ! $status->isOK() ) {
 120+ // It is a convention in MediaWiki to only return one error per API exception, even if multiple errors
 121+ // are available. We use reset() to pick the "first" thing that was wrong, preferring errors to warnings.
 122+ // This is a bit lame, as we may have more info in the $status and we're throwing it away, but to fix it means
 123+ // redesigning API errors significantly.
 124+ // $status->value just contains the virtual URL (if anything) which is probably useless to the caller
 125+ $error = reset( $status->getErrorsArray() );
 126+ if ( ! count( $error ) ) {
 127+ $error = reset( $status->getWarningsArray() );
 128+ if ( ! count( $error ) ) {
 129+ $error = array( 'unknown', 'no error recorded' );
 130+ }
 131+ }
 132+ throw new SessionStashFileException( "error storing file in '$path': " . join( '; ', $error ) );
119133 }
120134 $stashPath = $status->value;
121135

Status & tagging log