clean up error handling slightly

This commit is contained in:
Ben Merritt 2018-06-16 15:11:44 -07:00
parent b22c086e10
commit 3ca4cdb37a
No known key found for this signature in database
GPG Key ID: F8AD20ED4E6239B7
1 changed files with 37 additions and 36 deletions

View File

@ -13,24 +13,26 @@ extern "C" {
using namespace i18n::phonenumbers; using namespace i18n::phonenumbers;
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) { namespace {
using PNU = i18n::phonenumbers::PhoneNumberUtil; const char* getParseErrorMessage(PhoneNumberUtil::ErrorType error) {
switch(error) { using PNU = i18n::phonenumbers::PhoneNumberUtil;
case PNU::NO_PARSING_ERROR: switch(error) {
return "Parsed successfully"; case PNU::NO_PARSING_ERROR:
case PNU::INVALID_COUNTRY_CODE_ERROR: return "Parsed successfully";
return "Invalid country code"; case PNU::INVALID_COUNTRY_CODE_ERROR:
case PNU::NOT_A_NUMBER: return "Invalid country code";
return "String does not appear to contain a phone number"; case PNU::NOT_A_NUMBER:
case PNU::TOO_SHORT_AFTER_IDD: return "String does not appear to contain a phone number";
return "Too short after IDD"; case PNU::TOO_SHORT_AFTER_IDD:
case PNU::TOO_SHORT_NSN: return "Too short after IDD";
return "National number is too short"; case PNU::TOO_SHORT_NSN:
case PNU::TOO_LONG_NSN: return "National number is too short";
return "National number is too long"; case PNU::TOO_LONG_NSN:
default: return "National number is too long";
//We have some generic parsing error. default:
return "Unable to parse number"; //We have some generic parsing error.
return "Unable to parse number";
}
} }
} }
@ -51,23 +53,22 @@ void reportOutOfMemory() {
* depending on the type of the exception * depending on the type of the exception
*/ */
void reportException(const std::exception& exception) { void reportException(const std::exception& exception) {
{ const std::bad_alloc* bad_alloc = dynamic_cast<const std::bad_alloc*>(&exception);
const std::bad_alloc* bad_alloc = dynamic_cast<const std::bad_alloc*>(&exception); if(bad_alloc != nullptr) {
if(bad_alloc != nullptr) { reportOutOfMemory();
reportOutOfMemory(); return;
return; }
}
const PhoneNumberTooLongException* too_long = const PhoneNumberTooLongException* too_long =
dynamic_cast<const PhoneNumberTooLongException*>(&exception); dynamic_cast<const PhoneNumberTooLongException*>(&exception);
if(too_long != nullptr) { if(too_long != nullptr) {
std::string phone_number = too_long->number_string(); std::string phone_number = too_long->number_string();
phone_number += '\0'; phone_number += '\0';
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("phone number '%s' is too long", phone_number.data()), errmsg("phone number '%s' is too long", phone_number.data()),
errdetail("%s", exception.what()))); errdetail("%s", exception.what())));
return; return;
}
} }
//If we don't have a special way to handle this exception, report //If we don't have a special way to handle this exception, report
@ -82,7 +83,7 @@ void reportParseError(const char* phone_number, PhoneNumberUtil::ErrorType err)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unable to parse '%s' as a phone number", phone_number), errmsg("unable to parse '%s' as a phone number", phone_number),
errdetail("%s", parseErrorMessage(err)))); errdetail("%s", getParseErrorMessage(err))));
} }
void logInfo(const char* msg) { void logInfo(const char* msg) {