COMMON: Rename Error to ErrorCode, introduce new Error class

This commit is contained in:
Max Horn 2011-04-18 17:39:31 +02:00
parent e9c228564a
commit 73f04118f3
22 changed files with 131 additions and 79 deletions

View File

@ -60,7 +60,7 @@ void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
Common::StringArray DefaultSaveFileManager::listSavefiles(const Common::String &pattern) {
Common::String savePathName = getSavePath();
checkPath(Common::FSNode(savePathName));
if (getError() != Common::kNoError)
if (getError().getCode() != Common::kNoError)
return Common::StringArray();
// recreate FSNode since checkPath may have changed/created the directory
@ -84,7 +84,7 @@ Common::InSaveFile *DefaultSaveFileManager::openForLoading(const Common::String
// Ensure that the savepath is valid. If not, generate an appropriate error.
Common::String savePathName = getSavePath();
checkPath(Common::FSNode(savePathName));
if (getError() != Common::kNoError)
if (getError().getCode() != Common::kNoError)
return 0;
// recreate FSNode since checkPath may have changed/created the directory
@ -104,7 +104,7 @@ Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const Common::String
// Ensure that the savepath is valid. If not, generate an appropriate error.
Common::String savePathName = getSavePath();
checkPath(Common::FSNode(savePathName));
if (getError() != Common::kNoError)
if (getError().getCode() != Common::kNoError)
return 0;
// recreate FSNode since checkPath may have changed/created the directory
@ -121,7 +121,7 @@ Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const Common::String
bool DefaultSaveFileManager::removeSavefile(const Common::String &filename) {
Common::String savePathName = getSavePath();
checkPath(Common::FSNode(savePathName));
if (getError() != Common::kNoError)
if (getError().getCode() != Common::kNoError)
return false;
// recreate FSNode since checkPath may have changed/created the directory

View File

@ -134,21 +134,17 @@ static Common::Error runGame(const EnginePlugin *plugin, OSystem &system, const
err = Common::kInvalidPathError;
// Create the game engine
if (err == Common::kNoError)
if (err.getCode() == Common::kNoError)
err = (*plugin)->createInstance(&system, &engine);
// Check for errors
if (!engine || err != Common::kNoError) {
// TODO: An errorDialog for this and engine related errors is displayed already in the scummvm_main function
// Is a separate dialog here still required?
//GUI::displayErrorDialog("ScummVM could not find any game in the specified directory!");
const char *errMsg = _(Common::errorToString(err));
if (!engine || err.getCode() != Common::kNoError) {
// Print a warning; note that scummvm_main will also
// display an error dialog, so we don't have to do this here.
warning("%s failed to instantiate engine: %s (target '%s', path '%s')",
plugin->getName(),
errMsg,
err.getDesc().c_str(),
ConfMan.getActiveDomainName().c_str(),
dir.getPath().c_str()
);
@ -355,8 +351,9 @@ extern "C" int scummvm_main(int argc, const char * const argv[]) {
Common::Error res;
// TODO: deal with settings that require plugins to be loaded
if ((res = Base::processSettings(command, settings)) != Common::kArgumentNotProcessed)
return res;
res = Base::processSettings(command, settings);
if (res.getCode() != Common::kArgumentNotProcessed)
return res.getCode();
// Init the backend. Must take place after all config data (including
// the command line params) was read.
@ -430,14 +427,14 @@ extern "C" int scummvm_main(int argc, const char * const argv[]) {
#endif
// Did an error occur ?
if (result != Common::kNoError) {
if (result.getCode() != Common::kNoError) {
// Shows an informative error dialog if starting the selected game failed.
GUI::displayErrorDialog(result, _("Error running game:"));
}
// Quit unless an error occurred, or Return to launcher was requested
#ifndef FORCE_RTL
if (result == 0 && !g_system->getEventManager()->shouldRTL())
if (result.getCode() == Common::kNoError && !g_system->getEventManager()->shouldRTL())
break;
#endif
// Reset RTL flag in case we want to load another engine

View File

@ -31,44 +31,61 @@
namespace Common {
/**
* Error Table: Maps error codes to their default descriptions
* Maps an error code to equivalent string description.
*
* @param errorCode error code to be converted
* @return a pointer to string description of the error
*/
static String errorToString(ErrorCode errorCode) {
switch (errorCode) {
case kNoError:
return _s("No error");
case kInvalidPathError:
return _s("Invalid Path");
case kNoGameDataFoundError:
return _s("Game Data not found");
case kUnsupportedGameidError:
return _s("Game Id not supported");
case kUnsupportedColorMode:
return _s("Unsupported Color Mode");
struct ErrorMessage {
Error error;
const char *errMsg;
};
static const ErrorMessage _errMsgTable[] = {
{ kInvalidPathError, _s("Invalid Path") },
{ kNoGameDataFoundError, _s("Game Data not found") },
{ kUnsupportedGameidError, _s("Game Id not supported") },
{ kUnsupportedColorMode, _s("Unsupported Color Mode") },
{ kReadPermissionDenied, _s("Read permission denied") },
{ kWritePermissionDenied, _s("Write permission denied") },
case kReadPermissionDenied:
return _s("Read permission denied");
case kWritePermissionDenied:
return _s("Write permission denied");
// The following three overlap a bit with kInvalidPathError and each other. Which to keep?
{ kPathDoesNotExist, _s("Path not exists") },
{ kPathNotDirectory, _s("Path not a directory") },
{ kPathNotFile, _s("Path not a file") },
case kPathDoesNotExist:
return _s("Path not exists");
case kPathNotDirectory:
return _s("Path not a directory");
case kPathNotFile:
return _s("Path not a file");
{ kCreatingFileFailed, _s("Cannot create file") },
{ kReadingFailed, _s("Reading failed") },
{ kWritingFailed, _s("Writing data failed") },
case kCreatingFileFailed:
return _s("Cannot create file");
case kReadingFailed:
return _s("Reading failed");
case kWritingFailed:
return _s("Writing data failed");
{ kUnknownError, _s("Unknown Error") }
};
const char *errorToString(Error error) {
for (int i = 0; i < ARRAYSIZE(_errMsgTable); i++) {
if (error == _errMsgTable[i].error) {
return _errMsgTable[i].errMsg;
}
case kUnknownError:
case kPluginNotFound:
case kPluginNotSupportSaves:
case kNoSavesError:
case kArgumentNotProcessed:
default:
return _s("Unknown Error");
}
return _("Unknown Error");
}
Error::Error(ErrorCode code)
: _code(code), _desc(errorToString(code)) {
}
Error::Error(ErrorCode code, const String &desc)
: _code(code), _desc(errorToString(code) + ": " + desc) {
}
} // End of namespace Common

View File

@ -26,6 +26,8 @@
#ifndef COMMON_ERROR_H
#define COMMON_ERROR_H
#include "common/str.h"
namespace Common {
/**
@ -43,7 +45,7 @@ namespace Common {
* kPathInvalidError would be correct, but these would not be: kInvalidPath,
* kPathInvalid, kPathIsInvalid, kInvalidPathError
*/
enum Error {
enum ErrorCode {
kNoError = 0, ///< No error occurred
kInvalidPathError, ///< Engine initialization: Invalid game path was passed
kNoGameDataFoundError, ///< Engine initialization: No game data was found in the specified location
@ -73,12 +75,39 @@ enum Error {
};
/**
* Maps an error code to equivalent string description.
*
* @param error error code to be converted
* @return a pointer to string description of the error
* An Error instance pairs an error code with string description providing more
* details about the error. For every error code, a default description is
* provided, but it is possible to optionally augment that description with
* extra information when creating a new Error instance.
*/
const char *errorToString(Error error);
class Error {
protected:
ErrorCode _code;
String _desc;
public:
/**
* Construct a new Error with the specified error code and the default
* error message.
*/
Error(ErrorCode code = kUnknownError);
/**
* Construct a new Error with the specified error code and an augmented
* error message. Specifically, the provided extra text is appended
* to the default message, with ": " inserted in between.
*/
Error(ErrorCode code, const String &extra);
/**
* Get the description of this error.
*/
const String &getDesc() const { return _desc; }
/**
* Get the error code of this error.
*/
ErrorCode getCode() const { return _code; }
};
} // End of namespace Common

View File

@ -727,7 +727,7 @@ protected:
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -186,7 +186,7 @@ class AGOSEngine : public Engine {
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -247,7 +247,7 @@ protected:
Common::Error err;
registerDefaultSettings();
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -591,7 +591,9 @@ Common::Error LoLEngine::go() {
if (action == 0) {
startupNew();
} else if (_gameToLoad != -1) {
if (loadGameState(_gameToLoad) != Common::kNoError)
// FIXME: Instead of throwing away the error returned by
// loadGameState, we should use it / augment it.
if (loadGameState(_gameToLoad).getCode() != Common::kNoError)
error("Couldn't load game slot %d on startup", _gameToLoad);
_gameToLoad = -1;
}
@ -918,7 +920,9 @@ void LoLEngine::runLoop() {
while (!shouldQuit() && _runFlag) {
if (_gameToLoad != -1) {
if (loadGameState(_gameToLoad) != Common::kNoError)
// FIXME: Instead of throwing away the error returned by
// loadGameState, we should use it / augment it.
if (loadGameState(_gameToLoad).getCode() != Common::kNoError)
error("Couldn't load game slot %d", _gameToLoad);
_gameToLoad = -1;
}

View File

@ -257,7 +257,9 @@ void KyraEngine_v1::checkAutosave() {
}
void KyraEngine_v1::loadGameStateCheck(int slot) {
if (loadGameState(slot) != Common::kNoError) {
// FIXME: Instead of throwing away the error returned by
// loadGameState, we should use it / augment it.
if (loadGameState(slot).getCode() != Common::kNoError) {
const char *filename = getSavegameFilename(slot);
Common::String errorMessage = "Could not load savegame: '";
errorMessage += filename;

View File

@ -98,7 +98,7 @@ public:
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -268,7 +268,7 @@ public:
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -313,7 +313,7 @@ void SaveLoad_ns::renameOldSavefiles() {
if (_saveFileMan->renameSavefile(oldName, newName)) {
success++;
} else {
warning("Error %i (%s) occurred while renaming %s to %s", _saveFileMan->getError(),
warning("Error %i (%s) occurred while renaming %s to %s", _saveFileMan->getError().getCode(),
_saveFileMan->getErrorDesc().c_str(), oldName.c_str(), newName.c_str());
}
}

View File

@ -463,7 +463,7 @@ public:
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -106,7 +106,7 @@ protected:
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -100,7 +100,7 @@ protected:
virtual Common::Error run() {
Common::Error err;
err = init();
if (err != Common::kNoError)
if (err.getCode() != Common::kNoError)
return err;
return go();
}

View File

@ -73,10 +73,10 @@ Sword25Engine::~Sword25Engine() {
Common::Error Sword25Engine::run() {
// Engine initialisation
Common::Error errorCode = appStart();
if (errorCode != Common::kNoError) {
Common::Error error = appStart();
if (error.getCode() != Common::kNoError) {
appEnd();
return errorCode;
return error;
}
// Run the game

View File

@ -138,9 +138,9 @@ TestExitStatus SaveGametests::testListingSavefile() {
Common::Error error = saveFileMan->getError();
if (error != Common::kNoError) {
if (error.getCode() != Common::kNoError) {
// Abort. Some Error in writing files
Testsuite::logDetailedPrintf("Error while creating savefiles: %s\n", Common::errorToString(error));
Testsuite::logDetailedPrintf("Error while creating savefiles: %s\n", error.getDesc().c_str());
return kTestFailed;
}
@ -177,7 +177,7 @@ TestExitStatus SaveGametests::testErrorMessages() {
readAndVerifyData("tBedSomeNonExistentSaveFile.0", "File doesn't exists!");
Common::Error error = saveFileMan->getError();
if (error == Common::kNoError) {
if (error.getCode() == Common::kNoError) {
// blunder! how come?
Testsuite::logDetailedPrintf("SaveFileMan.getError() failed\n");
return kTestFailed;

View File

@ -972,7 +972,7 @@ Common::Error TinselEngine::run() {
// errors when loading the save state.
if (ConfMan.hasKey("save_slot")) {
if (loadGameState(ConfMan.getInt("save_slot")) == Common::kNoError)
if (loadGameState(ConfMan.getInt("save_slot")).getCode() == Common::kNoError)
loadingFromGMM = true;
}

View File

@ -331,14 +331,14 @@ void ToucheEngine::handleMenuAction(void *menu, int actionId) {
break;
case kActionPerformSaveLoad:
if (menuData->mode == kMenuLoadStateMode) {
if (loadGameState(_saveLoadCurrentSlot) == Common::kNoError) {
if (loadGameState(_saveLoadCurrentSlot).getCode() == Common::kNoError) {
menuData->quit = true;
}
} else if (menuData->mode == kMenuSaveStateMode) {
_system->setFeatureState(OSystem::kFeatureVirtualKeyboard, false);
const char *description = menuData->saveLoadDescriptionsTable[_saveLoadCurrentSlot];
if (strlen(description) > 0) {
if (saveGameState(_saveLoadCurrentSlot, description) == Common::kNoError) {
if (saveGameState(_saveLoadCurrentSlot, description).getCode() == Common::kNoError) {
menuData->quit = true;
}
}

View File

@ -3547,7 +3547,10 @@ void SceneHandler::dispatch() {
if (_saveGameSlot != -1) {
int saveSlot = _saveGameSlot;
_saveGameSlot = -1;
if (_saver->save(saveSlot, _saveName) != Common::kNoError)
Common::Error err = _saver->save(saveSlot, _saveName);
// FIXME: Make use of the description string in err to enhance
// the error reported to the user.
if (err.getCode() != Common::kNoError)
GUIErrorMessage(SAVE_ERROR_MSG);
}
if (_loadGameSlot != -1) {

View File

@ -36,10 +36,10 @@ void displayErrorDialog(const char *text) {
alert.runModal();
}
void displayErrorDialog(Common::Error error, const char *extraText) {
void displayErrorDialog(const Common::Error &error, const char *extraText) {
Common::String errorText(extraText);
errorText += " ";
errorText += _(Common::errorToString(error));
errorText += _(error.getDesc());
GUI::MessageDialog alert(errorText);
alert.runModal();
}

View File

@ -36,7 +36,7 @@ namespace GUI {
* @param error error code
* @param extraText extra text to be displayed in addition to default string description(optional)
*/
void displayErrorDialog(Common::Error error, const char *extraText = "");
void displayErrorDialog(const Common::Error &error, const char *extraText = "");
/**
* Displays an error dialog for a given message.