fcml  1.2.2
fcml_errors.hpp
Go to the documentation of this file.
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 
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 
42 class ErrorInfo {
43 public:
44 
48  enum ErrorLevel {
49  EL_ERROR,
50  EL_WARN
51  };
52 
53 public:
54 
60  _code( FCML_CEH_GEC_NO_ERROR ),
61  _level( EL_ERROR ) {
62  }
63 
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 
85  return _code;
86  }
87 
93  void setCode(fcml_ceh_error code) {
94  _code = code;
95  }
96 
103  return _level;
104  }
105 
111  void setLevel(ErrorLevel level) {
112  _level = level;
113  }
114 
120  const fcml_cstring& getMessage() const {
121  return _message;
122  }
123 
129  void setMessage(const fcml_cstring& message) {
130  _message = message;
131  }
132 
133 private:
134 
136  fcml_cstring _message;
138  fcml_ceh_error _code;
140  ErrorLevel _level;
141 
142 };
143 
149 public:
150 
156  _isWarn(false),
157  _isError(false) {
158  }
159 
165  fcml_usize getSize() const {
166  return static_cast<fcml_usize>( _errorInfos.size() );
167  }
168 
176  const ErrorInfo& operator[]( fcml_usize index ) const {
177  checkVectorAccess( index );
178  return _errorInfos[index];
179  }
180 
187  ErrorInfo& operator[]( fcml_usize index ) {
188  checkVectorAccess( index );
189  return _errorInfos[index];
190  }
191 
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 
214  bool isError() const {
215  return _isError;
216  }
217 
223  bool isWarn() const {
224  return _isWarn;
225  }
226 
232  bool isEmpty() const {
233  return _errorInfos.empty();
234  }
235 
242  const ErrorInfo& getFirstError() const {
243  if( isEmpty() ) {
244  throw IllegalStateException( FCML_TEXT( "Container is empty." ) );
245  }
246  return _errorInfos[0];
247  }
248 
256  if( isEmpty() ) {
257  throw IllegalStateException( FCML_TEXT( "Container is empty." ) );
258  }
259  return _errorInfos[0];
260  }
261 
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 
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 
296  void clean() {
297  _errorInfos.clear();
298  _isWarn = false;
299  _isError = false;
300  }
301 
302 private:
303 
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:
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 
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 
348 public:
349 
358  BaseException( msg, error ), _errorContainer( errorContainer ) {
359  }
360 
367  return _errorContainer;
368  }
369 
375  void setErrorContainer(const ErrorContainer& errorContainer) {
376  _errorContainer = errorContainer;
377  }
378 
379 private:
380 
382  ErrorContainer _errorContainer;
383 
384 };
385 
386 }
387 
388 #endif // FCML_ERRORS_HPP_
void addErrorInfo(const ErrorInfo &errorInfo)
Adds a new error into the container.
Definition: fcml_errors.hpp:197
bool isEmpty() const
Gets true if there is any error or warning in the container.
Definition: fcml_errors.hpp:232
void setCode(fcml_ceh_error code)
Sets a new error code.
Definition: fcml_errors.hpp:93
struct fcml_st_ceh_error_info * next_error
Next error/warning on the list.
Definition: fcml_errors.h:170
C++ wrappers common classes.
ErrorInfo & getFirstError()
Returns the first error from the container.
Definition: fcml_errors.hpp:255
Illegal state exception.
Definition: fcml_common.hpp:253
std::basic_string< fcml_char > fcml_cstring
By using this type definition here, it will be definitely much easier to support UNICODE in future re...
Definition: fcml_common.hpp:53
fcml_usize getSize() const
Gets number of errors in the container.
Definition: fcml_errors.hpp:165
const ErrorContainer & getErrorContainer() const
Gets a reference to an error container associated with the exception.
Definition: fcml_errors.hpp:366
void setMessage(const fcml_cstring &message)
Sets error message.
Definition: fcml_errors.hpp:129
ErrorContainer()
Default constructor.
Definition: fcml_errors.hpp:155
Container for all collected errors and warnings.
Definition: fcml_errors.h:180
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
const ErrorInfo & operator[](fcml_usize index) const
Gets an error at given index.
Definition: fcml_errors.hpp:176
Base class for all exceptions that are aware of ErrorContainer.
Definition: fcml_errors.hpp:347
const fcml_cstring & getMessage() const
Gets error message.
Definition: fcml_errors.hpp:120
ErrorInfo & operator[](fcml_usize index)
Gets an error at given index.
Definition: fcml_errors.hpp:187
ErrorLevel
Error level.
Definition: fcml_errors.hpp:48
Definition: fcml_assembler.hpp:39
fcml_cstring prepareErrorMessage(const fcml_cstring &message) const
Prepares an error message basing on the first error in the container.
Definition: fcml_errors.hpp:283
Types converter.
Definition: fcml_errors.hpp:329
Base exception for all exceptions exposed by FCML library.
Definition: fcml_common.hpp:187
ErrorLevel getLevel() const
Gets error level.
Definition: fcml_errors.hpp:102
bool isError() const
Returns true if there is any error in the container.
Definition: fcml_errors.hpp:214
void setErrorContainer(const ErrorContainer &errorContainer)
Sets a new error container for the exception.
Definition: fcml_errors.hpp:375
fcml_en_ceh_error_level level
Error level.
Definition: fcml_errors.h:176
fcml_ceh_error code
Error code.
Definition: fcml_errors.h:174
ErrorInfo(const fcml_cstring &message, fcml_ceh_error code=FCML_CEH_GEC_NO_ERROR, ErrorLevel level=EL_ERROR)
Creates an error for given message and optional error code and level.
Definition: fcml_errors.hpp:71
Wraps multiple errors into one component.
Definition: fcml_errors.hpp:148
Contains an error message together with error level and error code.
Definition: fcml_errors.hpp:42
void setLevel(ErrorLevel level)
Sets error level.
Definition: fcml_errors.hpp:111
fcml_ceh_error getCode() const
Gets error code.
Definition: fcml_errors.hpp:84
fcml_string message
Error message.
Definition: fcml_errors.h:172
Operation succeed.
Definition: fcml_errors.h:42
void clean()
Cleans all errors and warnings.
Definition: fcml_errors.hpp:296
Information about one particular error/warning.
Definition: fcml_errors.h:168
Bad arguments.
Definition: fcml_common.hpp:242
Errors are reported when something more important happened and processing should be stopped...
Definition: fcml_errors.h:164
fcml_st_ceh_error_info * errors
All errors and warnings going here.
Definition: fcml_errors.h:182
Global error handling related declarations.
fcml_cstring getFirstErrorMessage() const
Returns the first error message from the container.
Definition: fcml_errors.hpp:267
ErrorContainerAwareException(const fcml_cstring &msg, const ErrorContainer &errorContainer, fcml_ceh_error error=FCML_CEH_GEC_NO_ERROR)
Creates an error container aware exception instance and sets basic information for it...
Definition: fcml_errors.hpp:357
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:156
const ErrorInfo & getFirstError() const
Returns the first error from the container.
Definition: fcml_errors.hpp:242
Types declarations.
ErrorInfo()
Default constructor.
Definition: fcml_errors.hpp:59
bool isWarn() const
Returns true if there is any warning in the container.
Definition: fcml_errors.hpp:223