aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2011-04-18 17:39:31 +0200
committerMax Horn2011-04-18 18:22:02 +0200
commit73f04118f3d03e25fa1139cfea2b1330f0098bd4 (patch)
treebd0d638d9fbecf5207d0ac0fd775febca9e8b3db /common
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
Diffstat (limited to 'common')
-rw-r--r--common/error.cpp83
-rw-r--r--common/error.h41
2 files changed, 85 insertions, 39 deletions
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