| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
/** @file fcml_errors.hpp |
| 21 |
* C++ wrapper for the FCML errors handling. |
| 22 |
* |
| 23 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 24 |
* This project is released under the GNU Lesser General Public License. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef FCML_ERRORS_HPP_ |
| 28 |
#define FCML_ERRORS_HPP_ |
| 29 |
|
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include "fcml_types.h" |
| 33 |
#include "fcml_errors.h" |
| 34 |
#include "fcml_common.hpp" |
| 35 |
|
| 36 |
namespace fcml { |
| 37 |
|
| 38 |
/** |
| 39 |
* Contains an error message together with error level and error code. |
| 40 |
* @since 1.1.0 |
| 41 |
*/ |
| 42 |
class ErrorInfo { |
| 43 |
public: |
| 44 |
|
| 45 |
/** Error level. |
| 46 |
* @since 1.1.0 |
| 47 |
*/ |
| 48 |
enum ErrorLevel { |
| 49 |
EL_ERROR, |
| 50 |
EL_WARN |
| 51 |
}; |
| 52 |
|
| 53 |
public: |
| 54 |
|
| 55 |
/** |
| 56 |
* Default constructor. |
| 57 |
* @since 1.1.0 |
| 58 |
*/ |
| 59 |
ErrorInfo() : |
| 60 |
_code( FCML_CEH_GEC_NO_ERROR ), |
| 61 |
_level( EL_ERROR ) { |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Creates an error for given message and optional error code and level. |
| 66 |
* @param message The error message. |
| 67 |
* @param code The error code. |
| 68 |
* @param level The error level. |
| 69 |
* @since 1.1.0 |
| 70 |
*/ |
| 71 |
ErrorInfo( const fcml_cstring &message, fcml_ceh_error code = FCML_CEH_GEC_NO_ERROR, ErrorLevel level = EL_ERROR ) : |
| 72 |
_message( message ), |
| 73 |
_code( code ), |
| 74 |
_level( level ) { |
| 75 |
} |
| 76 |
|
| 77 |
public: |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets error code. |
| 81 |
* @return The error code. |
| 82 |
* @since 1.1.0 |
| 83 |
*/ |
| 84 |
fcml_ceh_error getCode() const { |
| 85 |
return _code; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Sets a new error code. |
| 90 |
* @param code The new error code. |
| 91 |
* @since 1.1.0 |
| 92 |
*/ |
| 93 |
void setCode(fcml_ceh_error code) { |
| 94 |
_code = code; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Gets error level. |
| 99 |
* @return The error level. |
| 100 |
* @since 1.1.0 |
| 101 |
*/ |
| 102 |
ErrorLevel getLevel() const { |
| 103 |
return _level; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Sets error level. |
| 108 |
* @param level The error level. |
| 109 |
* @since 1.1.0 |
| 110 |
*/ |
| 111 |
void setLevel(ErrorLevel level) { |
| 112 |
_level = level; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Gets error message. |
| 117 |
* @return The error message. |
| 118 |
* @since 1.1.0 |
| 119 |
*/ |
| 120 |
const fcml_cstring& getMessage() const { |
| 121 |
return _message; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Sets error message. |
| 126 |
* @param message The error message. |
| 127 |
* @since 1.1.0 |
| 128 |
*/ |
| 129 |
void setMessage(const fcml_cstring& message) { |
| 130 |
_message = message; |
| 131 |
} |
| 132 |
|
| 133 |
private: |
| 134 |
|
| 135 |
/** Error message. */ |
| 136 |
fcml_cstring _message; |
| 137 |
/** Error code */ |
| 138 |
fcml_ceh_error _code; |
| 139 |
/** Error level. */ |
| 140 |
ErrorLevel _level; |
| 141 |
|
| 142 |
}; |
| 143 |
|
| 144 |
/** |
| 145 |
* Wraps multiple errors into one component. |
| 146 |
* @since 1.1.0 |
| 147 |
*/ |
| 148 |
class ErrorContainer { |
| 149 |
public: |
| 150 |
|
| 151 |
/** |
| 152 |
* Default constructor. |
| 153 |
* @since 1.1.0 |
| 154 |
*/ |
| 155 |
ErrorContainer() : |
| 156 |
_isWarn(false), |
| 157 |
_isError(false) { |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Gets number of errors in the container. |
| 162 |
* @return The number of errors. |
| 163 |
* @since 1.1.0 |
| 164 |
*/ |
| 165 |
fcml_usize getSize() const { |
| 166 |
return static_cast<fcml_usize>( _errorInfos.size() ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Gets an error at given index. |
| 171 |
* @param index The error index. |
| 172 |
* @return The error at given index. |
| 173 |
* @throw BadArgumentException If index is out of bound. |
| 174 |
* @since 1.1.0 |
| 175 |
*/ |
| 176 |
const ErrorInfo& operator[]( fcml_usize index ) const { |
| 177 |
checkVectorAccess( index ); |
| 178 |
return _errorInfos[index]; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Gets an error at given index. |
| 183 |
* @param index The error index. |
| 184 |
* @return The error at given index. |
| 185 |
* @since 1.1.0 |
| 186 |
*/ |
| 187 |
ErrorInfo& operator[]( fcml_usize index ) { |
| 188 |
checkVectorAccess( index ); |
| 189 |
return _errorInfos[index]; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Adds a new error into the container. |
| 194 |
* @param errorInfo The new error to add. |
| 195 |
* @since 1.1.0 |
| 196 |
*/ |
| 197 |
void addErrorInfo( const ErrorInfo &errorInfo ) { |
| 198 |
_errorInfos.push_back(errorInfo); |
| 199 |
switch( errorInfo.getLevel() ) { |
| 200 |
case ErrorInfo::EL_ERROR: |
| 201 |
_isError = true; |
| 202 |
break; |
| 203 |
case ErrorInfo::EL_WARN: |
| 204 |
_isWarn = true; |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns true if there is any error in the container. |
| 211 |
* @return True if there is any error in the container. |
| 212 |
* @since 1.1.0 |
| 213 |
*/ |
| 214 |
bool isError() const { |
| 215 |
return _isError; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns true if there is any warning in the container. |
| 220 |
* @return True if there is any warning in the container. |
| 221 |
* @since 1.1.0 |
| 222 |
*/ |
| 223 |
bool isWarn() const { |
| 224 |
return _isWarn; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Gets true if there is any error or warning in the container. |
| 229 |
* @return True if there is any error or warning in the container. |
| 230 |
* @since 1.1.0 |
| 231 |
*/ |
| 232 |
bool isEmpty() const { |
| 233 |
return _errorInfos.empty(); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns the first error from the container. |
| 238 |
* @return The first error. |
| 239 |
* @throw IllegalStateException Container is empty. |
| 240 |
* @since 1.1.0 |
| 241 |
*/ |
| 242 |
const ErrorInfo& getFirstError() const { |
| 243 |
if( isEmpty() ) { |
| 244 |
throw IllegalStateException( FCML_TEXT( "Container is empty." ) ); |
| 245 |
} |
| 246 |
return _errorInfos[0]; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the first error from the container. |
| 251 |
* @return The first error. |
| 252 |
* @throw IllegalStateException Container is empty. |
| 253 |
* @since 1.1.0 |
| 254 |
*/ |
| 255 |
ErrorInfo& getFirstError() { |
| 256 |
if( isEmpty() ) { |
| 257 |
throw IllegalStateException( FCML_TEXT( "Container is empty." ) ); |
| 258 |
} |
| 259 |
return _errorInfos[0]; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Returns the first error message from the container. |
| 264 |
* @return The first error message. |
| 265 |
* @since 1.1.0 |
| 266 |
*/ |
| 267 |
fcml_cstring getFirstErrorMessage() const { |
| 268 |
fcml_cstring message; |
| 269 |
try { |
| 270 |
const ErrorInfo &errorInfo = getFirstError(); |
| 271 |
message = errorInfo.getMessage(); |
| 272 |
} catch( IllegalStateException &exc ) { |
| 273 |
} |
| 274 |
return message; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Prepares an error message basing on the first error in the container. |
| 279 |
* @param message A prefix for the destination error message. |
| 280 |
* @return Error message. |
| 281 |
* @since 1.1.0 |
| 282 |
*/ |
| 283 |
fcml_cstring prepareErrorMessage( const fcml_cstring &message ) const { |
| 284 |
const fcml_cstring errorMessage = getFirstErrorMessage(); |
| 285 |
if( errorMessage.empty() ) { |
| 286 |
return message + FCML_TEXT('.'); |
| 287 |
} else { |
| 288 |
return message + FCML_TEXT(": ") + errorMessage; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Cleans all errors and warnings. |
| 294 |
* @since 1.1.0 |
| 295 |
*/ |
| 296 |
void clean() { |
| 297 |
_errorInfos.clear(); |
| 298 |
_isWarn = false; |
| 299 |
_isError = false; |
| 300 |
} |
| 301 |
|
| 302 |
private: |
| 303 |
|
| 304 |
/** |
| 305 |
* Checks bounds of the index. |
| 306 |
* @param index The index to check. |
| 307 |
* @throw BadArgumentException Index out of bound. |
| 308 |
* @since 1.1.0 |
| 309 |
*/ |
| 310 |
void checkVectorAccess( fcml_usize index ) const { |
| 311 |
if( index >= _errorInfos.size() ) { |
| 312 |
throw BadArgumentException( FCML_TEXT( "Index exceeds the allowed number of error info structures." ) ); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
private: |
| 317 |
/** Error messages. */ |
| 318 |
std::vector<ErrorInfo> _errorInfos; |
| 319 |
/* Sets if there is any warning in the container. */ |
| 320 |
bool _isWarn; |
| 321 |
/* Sets if there is any error in the container. */ |
| 322 |
bool _isError; |
| 323 |
}; |
| 324 |
|
| 325 |
/** |
| 326 |
* Types converter. |
| 327 |
* @since 1.1.0 |
| 328 |
*/ |
| 329 |
class ErrorTypeConverter { |
| 330 |
public: |
| 331 |
|
| 332 |
static void convert( const fcml_st_ceh_error_container &src, ErrorContainer &dest ) { |
| 333 |
fcml_st_ceh_error_info *error = src.errors; |
| 334 |
while( error ) { |
| 335 |
ErrorInfo::ErrorLevel level = ( error->level == FCML_EN_CEH_EL_ERROR ) ? ErrorInfo::EL_ERROR : ErrorInfo::EL_WARN; |
| 336 |
dest.addErrorInfo( ErrorInfo( error->message, error->code, level ) ); |
| 337 |
error = error->next_error; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
}; |
| 342 |
|
| 343 |
/** |
| 344 |
* Base class for all exceptions that are aware of ErrorContainer. |
| 345 |
* @since 1.1.0 |
| 346 |
*/ |
| 347 |
class ErrorContainerAwareException: public BaseException { |
| 348 |
public: |
| 349 |
|
| 350 |
/** |
| 351 |
* Creates an error container aware exception instance and sets basic information for it. |
| 352 |
* @param msg An error message. |
| 353 |
* @param errorContainer An error container for exception. |
| 354 |
* @param error An optional FCML error code. |
| 355 |
* @since 1.1.0 |
| 356 |
*/ |
| 357 |
ErrorContainerAwareException( const fcml_cstring &msg, const ErrorContainer &errorContainer, fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR ) : |
| 358 |
BaseException( msg, error ), _errorContainer( errorContainer ) { |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Gets a reference to an error container associated with the exception. |
| 363 |
* @return The error container. |
| 364 |
* @since 1.1.0 |
| 365 |
*/ |
| 366 |
const ErrorContainer& getErrorContainer() const { |
| 367 |
return _errorContainer; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Sets a new error container for the exception. |
| 372 |
* @param errorContainer The new error container to be set for the exception. |
| 373 |
* @since 1.1.0 |
| 374 |
*/ |
| 375 |
void setErrorContainer(const ErrorContainer& errorContainer) { |
| 376 |
_errorContainer = errorContainer; |
| 377 |
} |
| 378 |
|
| 379 |
private: |
| 380 |
|
| 381 |
/** A container for errors. */ |
| 382 |
ErrorContainer _errorContainer; |
| 383 |
|
| 384 |
}; |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
#endif // FCML_ERRORS_HPP_ |