Exceptions improvements

This commit is contained in:
Valentino Orlandi 2022-08-25 01:05:53 +02:00
parent e6dce3763d
commit c314e66367
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
5 changed files with 40 additions and 21 deletions

View File

@ -66,8 +66,8 @@ const std::unordered_map<std::string, QString> Craphelp::getColorScheme( const i
{"link", "#ff85a1"} };
default:
// wrong
throw GenericException( "Unexpected ColorScheme ID: "+std::to_string( scheme_id ) );
// wrong, shouldn't be here
throw GenericException( "Unexpected ColorScheme ID: "+std::to_string( scheme_id ), true ); // leave un-catched
}
}

View File

@ -77,7 +77,7 @@ std::string HashOps::digestFile( const std::string& file_path )
// try reading as gzip compressed file
GZutils::readFile( file_path, content );
} catch (GenericException& e) {
} catch (const GenericException& e) {
// failed closing file pointer
throw e;
@ -89,17 +89,27 @@ std::string HashOps::digestFile( const std::string& file_path )
IOutils::readFile( file_path, content );
}
} catch (GenericException& err) {
// failed closing
throw err;
// re-catched in craplog
} catch (const GenericException) {
// failed closing gzip file pointer
throw GenericException( QString("%1:\n%2").arg(
DialogSec::tr("An error accured while reading the gzipped file"),
QString::fromStdString( file_path )
).toStdString() );
} catch (const std::ios_base::failure& err) {
// failed reading
throw err;
// failed reading as text
throw GenericException( QString("%1:\n%2").arg(
DialogSec::tr("An error accured while reading the file"),
QString::fromStdString( file_path )
).toStdString() );
} catch (...) {
// failed somehow
throw GenericException( "Something failed while handling file" );
throw GenericException( QString("%1:\n%2").arg(
DialogSec::tr("Something failed while handling the file"),
QString::fromStdString( file_path )
).toStdString() );
}
SHA256 sha;

View File

@ -53,7 +53,7 @@ void Crapnote::setColorScheme( const int& scheme_id )
break;
default:
// wrong
throw GenericException( "Unexpected ColorScheme ID for Crapnote: "+std::to_string( scheme_id ) );
throw GenericException( "Unexpected ColorScheme ID for Crapnote: "+std::to_string( scheme_id ), true ); // leave un-catched
}
}

View File

@ -35,7 +35,7 @@ const int DbQuery::getMinuteGap( const int& minute , const int& gap )
int m = -1;
if ( minute < 0 || minute >= 60 ) {
// unexpected value
throw GenericException( "Unexpected Minute: "+std::to_string( minute ) );
throw DateTimeException( "Unexpected Minute: "+std::to_string( minute ) );
}
int n = 0;
for ( int g=0; g<60; g+=gap ) {
@ -81,7 +81,7 @@ const int DbQuery::getMonthsCount( const QString& from_year, const QString& from
to_month_ = ( to_month.size() == 0 ) ? from_month_ : this->getMonthNumber( to_month ) ;
} catch (...) {
// failed to convert to integers
throw GenericException( "Failed to convert from string to int" );
throw DateTimeException( "Failed to convert from string to int" ); // leave un-catched
}
return this->getMonthsCount( from_year_, from_month_, to_year_, to_month_ );
}

View File

@ -138,7 +138,7 @@ void IOutils::randomLines(const std::string& path, std::vector<std::string>& lin
// try reading a gzipped file
GZutils::readFile( path, aux );
} catch (GenericException& e) {
} catch (const GenericException& e) {
// failed closing file pointer
throw e;
@ -181,21 +181,30 @@ void IOutils::randomLines(const std::string& path, std::vector<std::string>& lin
}
}
aux_lines.clear();
} catch (const std::ios_base::failure& err) {
// re-catched in craplog
} catch (const GenericException) {
// failed closing gzip file pointer
lines.clear(); aux_lines.clear();
if ( file.is_open() ) {
file.close();
}
throw GenericException( "An error accured while reading the gzipped file" );
} catch (const std::ios_base::failure) {
// failed reading
lines.clear();
aux_lines.clear();
lines.clear(); aux_lines.clear();
if ( file.is_open() ) {
file.close();
}
throw err;
throw GenericException( "An error accured while reading the file" );
} catch (...) {
lines.clear();
aux_lines.clear();
lines.clear(); aux_lines.clear();
if ( file.is_open() ) {
file.close();
}
throw std::exception(); // exception catched in Craplog
throw GenericException( "Something failed while handling the file" );
}
if ( file.is_open() ) {
file.close();
@ -209,7 +218,7 @@ void IOutils::readFile( const std::string& path , std::string& content )
// read the whole file
std::ifstream file;
try {
constexpr std::size_t read_size = std::size_t(4096);
/*constexpr std::size_t read_size = std::size_t(4096);*/
file = std::ifstream(path);
if ( ! file.is_open() ) {
throw std::ios_base::failure( "file is not open" );