CACException.cpp
Go to the documentation of this file.00001
00010 #include "PKIFUTILS/PKIFException.h"
00011 #include "PKIFCommonErrors.h"
00012 #include "ExceptionInfo.h"
00013 #include <vector>
00014 #include <string>
00015 #include <cstring>
00016
00017 #include <iterator>
00018
00019 using namespace std;
00020
00022
00023 struct CPKIFExceptionImpl
00024 {
00025 ExceptionInfoPtr thisAsExceptionInfoPtr() const;
00026 ExceptionInfo m_thisExceptionInfo;
00027 std::vector<ExceptionInfoPtr> m_previousExceptionInfos;
00028 };
00029
00039 ExceptionInfoPtr CPKIFExceptionImpl::thisAsExceptionInfoPtr() const
00040 {
00041 ExceptionInfoPtr tmp(new ExceptionInfo);
00042 tmp->errorCode = m_thisExceptionInfo.errorCode;
00043 tmp->subComponent = m_thisExceptionInfo.subComponent;
00044
00045 if('\0' != m_thisExceptionInfo.description[0])
00046 strcpy(tmp->description,m_thisExceptionInfo.description);
00047 else
00048 tmp->description[0] = '\0';
00049
00050 tmp->fileName = m_thisExceptionInfo.fileName;
00051 tmp->lineNumber = m_thisExceptionInfo.lineNumber;
00052
00053 return tmp;
00054 }
00055
00057
00066 CPKIFException::CPKIFException(
00068 int subComponent,
00070 int errorCode) : m_impl(new CPKIFExceptionImpl),exception()
00071 {
00072 m_impl->m_thisExceptionInfo.errorCode = errorCode;
00073 m_impl->m_thisExceptionInfo.subComponent = subComponent;
00074 m_impl->m_thisExceptionInfo.description[0] = '\0';
00075
00076 m_impl->m_thisExceptionInfo.fileName = NULL;
00077 m_impl->m_thisExceptionInfo.lineNumber = 0;
00078 }
00087 CPKIFException::CPKIFException(
00089 int subComponent,
00091 int errorCode,
00093 const char* description) : m_impl(new CPKIFExceptionImpl),exception()
00094 {
00095 m_impl->m_thisExceptionInfo.errorCode = errorCode;
00096 m_impl->m_thisExceptionInfo.subComponent = subComponent;
00097
00098 if(NULL != description)
00099 {
00100 if(255 < strlen(description))
00101 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Description value passed to CPKIFException constructor too big.");
00102 strcpy(m_impl->m_thisExceptionInfo.description,description);
00103 }
00104 else
00105 m_impl->m_thisExceptionInfo.description[0] = '\0';
00106
00107 m_impl->m_thisExceptionInfo.fileName = NULL;
00108 m_impl->m_thisExceptionInfo.lineNumber = 0;
00109 }
00118 CPKIFException::CPKIFException(
00120 int subComponent,
00122 int errorCode,
00124 const char* description,
00126 char* file,
00128 int line) : m_impl(new CPKIFExceptionImpl),exception()
00129 {
00130 m_impl->m_thisExceptionInfo.errorCode = errorCode;
00131 m_impl->m_thisExceptionInfo.subComponent = subComponent;
00132
00133 if(NULL != description)
00134 {
00135 if(255 < strlen(description))
00136 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Description value passed to CPKIFException constructor too big.");
00137
00138 strcpy(m_impl->m_thisExceptionInfo.description,description);
00139 }
00140 else
00141 m_impl->m_thisExceptionInfo.description[0] = '\0';
00142
00143 m_impl->m_thisExceptionInfo.fileName = file;
00144 m_impl->m_thisExceptionInfo.lineNumber = line;
00145 }
00154 CPKIFException::CPKIFException(
00156 int subComponentID,
00158 int errorCode,
00160 char* file,
00162 int line):m_impl(new CPKIFExceptionImpl)
00163 {
00164 m_impl->m_thisExceptionInfo.errorCode = errorCode;
00165 m_impl->m_thisExceptionInfo.subComponent = subComponentID;
00166
00167 m_impl->m_thisExceptionInfo.description[0] = '\0';
00168
00169 m_impl->m_thisExceptionInfo.fileName = file;
00170 m_impl->m_thisExceptionInfo.lineNumber = line;
00171 }
00180 CPKIFException::CPKIFException(
00182 const CPKIFException& e)
00183 :m_impl(new CPKIFExceptionImpl)
00184 {
00185 m_impl->m_thisExceptionInfo.errorCode = e.m_impl->m_thisExceptionInfo.errorCode;
00186 m_impl->m_thisExceptionInfo.subComponent = e.m_impl->m_thisExceptionInfo.subComponent;
00187
00188 strcpy(m_impl->m_thisExceptionInfo.description, e.m_impl->m_thisExceptionInfo.description);
00189
00190 m_impl->m_thisExceptionInfo.fileName = e.m_impl->m_thisExceptionInfo.fileName;
00191 m_impl->m_thisExceptionInfo.lineNumber = e.m_impl->m_thisExceptionInfo.lineNumber;
00192 }
00200 CPKIFException::~CPKIFException(void) STDLIB_SUPER_THROW
00201 {
00202 if(m_impl) {
00203 delete m_impl;
00204 m_impl = 0;
00205 }
00206 }
00216 void CPKIFException::push_info(
00218 CPKIFException& e)
00219 {
00220 std::vector<ExceptionInfoPtr>::const_iterator pos;
00221 std::vector<ExceptionInfoPtr>::const_iterator end = e.m_impl->m_previousExceptionInfos.end();
00222 for(pos = e.m_impl->m_previousExceptionInfos.begin(); pos != end; ++pos)
00223 m_impl->m_previousExceptionInfos.push_back(*pos);
00224 }
00237 void CPKIFException::GetExceptionInfos(
00239 std::vector<ExceptionInfoPtr>& v)
00240 {
00241 v.clear();
00242 v.push_back(m_impl->thisAsExceptionInfoPtr());
00243 copy(m_impl->m_previousExceptionInfos.begin(), m_impl->m_previousExceptionInfos.end(), back_inserter(v));
00244 }
00253 CPKIFStringPtr CPKIFException::print() const
00254 {
00255 CPKIFStringPtr result(new std::string());
00256
00257
00258 char numbuf[10];
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 result->append("Component: ");
00272
00273
00274 sprintf (numbuf, "%i", m_impl->m_thisExceptionInfo.subComponent);
00275 result->append(numbuf);
00276 result->append(" Code: ");
00277 sprintf (numbuf, "%i", m_impl->m_thisExceptionInfo.errorCode);
00278 result->append(numbuf);
00279 result->append(" Description: ");
00280 result->append(m_impl->m_thisExceptionInfo.description);
00281
00282 std::vector<ExceptionInfoPtr>::const_iterator pos;
00283 std::vector<ExceptionInfoPtr>::const_iterator end = m_impl->m_previousExceptionInfos.end();
00284 for(pos = m_impl->m_previousExceptionInfos.begin(); pos != end; ++pos)
00285 {
00286 result->append(" Component: ");
00287
00288 sprintf (numbuf, "%i", (*pos)->subComponent);
00289 result->append(numbuf);
00290 result->append(" Code: ");
00291 sprintf (numbuf, "%i", (*pos)->errorCode);
00292 result->append(numbuf);
00293 result->append(" Description: ");
00294 result->append((*pos)->description);
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 }
00305
00306 return result;
00307 }
00355 int CPKIFException::GetErrorCode() const
00356 {
00357 return m_impl->m_thisExceptionInfo.errorCode;
00358 }
00435 int CPKIFException::GetSubcomponentID() const
00436 {
00437 return m_impl->m_thisExceptionInfo.subComponent;
00438 }
00448 const char* CPKIFException::GetDescription() const
00449 {
00450 if(0 != strlen(m_impl->m_thisExceptionInfo.description))
00451 return m_impl->m_thisExceptionInfo.description;
00452 else
00453 return NULL;
00454 }