aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Horn2011-04-18 17:39:31 +0200
committerMax Horn2011-04-18 18:22:02 +0200
commit73f04118f3d03e25fa1139cfea2b1330f0098bd4 (patch)
treebd0d638d9fbecf5207d0ac0fd775febca9e8b3db
parente9c228564a97267bd439704b96cb659986511224 (diff)
downloadscummvm-rg350-73f04118f3d03e25fa1139cfea2b1330f0098bd4.tar.gz
scummvm-rg350-73f04118f3d03e25fa1139cfea2b1330f0098bd4.tar.bz2
scummvm-rg350-73f04118f3d03e25fa1139cfea2b1330f0098bd4.zip
COMMON: Rename Error to ErrorCode, introduce new Error class
-rw-r--r--backends/saves/default/default-saves.cpp8
-rw-r--r--base/main.cpp23
-rw-r--r--common/error.cpp83
-rw-r--r--common/error.h41
-rw-r--r--engines/agi/agi.h2
-rw-r--r--engines/agos/agos.h2
-rw-r--r--engines/kyra/kyra_v1.h2
-rw-r--r--engines/kyra/lol.cpp8
-rw-r--r--engines/kyra/saveload.cpp4
-rw-r--r--engines/lure/lure.h2
-rw-r--r--engines/parallaction/parallaction.h2
-rw-r--r--engines/parallaction/saveload.cpp2
-rw-r--r--engines/scumm/scumm.h2
-rw-r--r--engines/sky/sky.h2
-rw-r--r--engines/sword1/sword1.h2
-rw-r--r--engines/sword25/sword25.cpp6
-rw-r--r--engines/testbed/savegame.cpp6
-rw-r--r--engines/tinsel/tinsel.cpp2
-rw-r--r--engines/touche/menu.cpp4
-rw-r--r--engines/tsage/core.cpp5
-rw-r--r--gui/error.cpp4
-rw-r--r--gui/error.h2
22 files changed, 133 insertions, 81 deletions
diff --git a/backends/saves/default/default-saves.cpp b/backends/saves/default/default-saves.cpp
index 0e16f165b3..75e10cf810 100644
--- a/backends/saves/default/default-saves.cpp
+++ b/backends/saves/default/default-saves.cpp
@@ -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
diff --git a/base/main.cpp b/base/main.cpp
index 65aa0ab43f..f45067ff1d 100644
--- a/base/main.cpp
+++ b/base/main.cpp
@@ -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
diff --git a/common/error.cpp b/common/error.cpp
index 6d1e349287..d0e301b091 100644
--- a/common/error.cpp
+++ b/common/error.cpp
@@ -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
*/
-
-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") },
+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");
+
+ 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") },
-
- { kCreatingFileFailed, _s("Cannot create file") },
- { kReadingFailed, _s("Reading failed") },
- { kWritingFailed, _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 kPathDoesNotExist:
+ return _s("Path not exists");
+ case kPathNotDirectory:
+ return _s("Path not a directory");
+ case kPathNotFile:
+ return _s("Path not a file");
+
+ case kCreatingFileFailed:
+ return _s("Cannot create file");
+ case kReadingFailed:
+ return _s("Reading failed");
+ case kWritingFailed:
+ return _s("Writing data failed");
+
+ 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
diff --git a/common/error.h b/common/error.h
index 58343114a2..9d74ccac9b 100644
--- a/common/error.h
+++ b/common/error.h
@@ -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
diff --git a/engines/agi/agi.h b/engines/agi/agi.h
index 1bee78e765..aca0b32a5c 100644
--- a/engines/agi/agi.h
+++ b/engines/agi/agi.h
@@ -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();
}
diff --git a/engines/agos/agos.h b/engines/agos/agos.h
index 1d9602f639..735920e427 100644
--- a/engines/agos/agos.h
+++ b/engines/agos/agos.h
@@ -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();
}
diff --git a/engines/kyra/kyra_v1.h b/engines/kyra/kyra_v1.h
index cf51774b0f..801d31d676 100644
--- a/engines/kyra/kyra_v1.h
+++ b/engines/kyra/kyra_v1.h
@@ -247,7 +247,7 @@ protected:
Common::Error err;
registerDefaultSettings();
err = init();
- if (err != Common::kNoError)
+ if (err.getCode() != Common::kNoError)
return err;
return go();
}
diff --git a/engines/kyra/lol.cpp b/engines/kyra/lol.cpp
index 5928c40f92..78c9ed260d 100644
--- a/engines/kyra/lol.cpp
+++ b/engines/kyra/lol.cpp
@@ -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;
}
diff --git a/engines/kyra/saveload.cpp b/engines/kyra/saveload.cpp
index 44579c3377..d14001aea6 100644
--- a/engines/kyra/saveload.cpp
+++ b/engines/kyra/saveload.cpp
@@ -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;
diff --git a/engines/lure/lure.h b/engines/lure/lure.h
index 99e9e3d93e..52b785a09a 100644
--- a/engines/lure/lure.h
+++ b/engines/lure/lure.h
@@ -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();
}
diff --git a/engines/parallaction/parallaction.h b/engines/parallaction/parallaction.h
index a8a57ed2d8..c5b6b23f55 100644
--- a/engines/parallaction/parallaction.h
+++ b/engines/parallaction/parallaction.h
@@ -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();
}
diff --git a/engines/parallaction/saveload.cpp b/engines/parallaction/saveload.cpp
index b8116d6bc7..50a777f7b8 100644
--- a/engines/parallaction/saveload.cpp
+++ b/engines/parallaction/saveload.cpp
@@ -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());
}
}
diff --git a/engines/scumm/scumm.h b/engines/scumm/scumm.h
index f3af84bb04..266a2c4948 100644
--- a/engines/scumm/scumm.h
+++ b/engines/scumm/scumm.h
@@ -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();
}
diff --git a/engines/sky/sky.h b/engines/sky/sky.h
index d8ced1e569..378bba73ee 100644
--- a/engines/sky/sky.h
+++ b/engines/sky/sky.h
@@ -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();
}
diff --git a/engines/sword1/sword1.h b/engines/sword1/sword1.h
index 592d2da6f4..255299d33e 100644
--- a/engines/sword1/sword1.h
+++ b/engines/sword1/sword1.h
@@ -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();
}
diff --git a/engines/sword25/sword25.cpp b/engines/sword25/sword25.cpp
index aac21f4b55..8740e44e9c 100644
--- a/engines/sword25/sword25.cpp
+++ b/engines/sword25/sword25.cpp
@@ -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
diff --git a/engines/testbed/savegame.cpp b/engines/testbed/savegame.cpp
index b91d9fc47c..0ffd3672fa 100644
--- a/engines/testbed/savegame.cpp
+++ b/engines/testbed/savegame.cpp
@@ -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;
diff --git a/engines/tinsel/tinsel.cpp b/engines/tinsel/tinsel.cpp
index e1396f9715..d78175d00c 100644
--- a/engines/tinsel/tinsel.cpp
+++ b/engines/tinsel/tinsel.cpp
@@ -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;
}
diff --git a/engines/touche/menu.cpp b/engines/touche/menu.cpp
index 52967c25c7..eb10c61893 100644
--- a/engines/touche/menu.cpp
+++ b/engines/touche/menu.cpp
@@ -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;
}
}
diff --git a/engines/tsage/core.cpp b/engines/tsage/core.cpp
index b3b1693cc3..2d0a69f89c 100644
--- a/engines/tsage/core.cpp
+++ b/engines/tsage/core.cpp
@@ -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) {
diff --git a/gui/error.cpp b/gui/error.cpp
index 3332eb533a..f6da795d40 100644
--- a/gui/error.cpp
+++ b/gui/error.cpp
@@ -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();
}
diff --git a/gui/error.h b/gui/error.h
index a55f555bed..f048a0cd09 100644
--- a/gui/error.h
+++ b/gui/error.h
@@ -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.