Fixed error dispatching bug; consolidated catches

This commit is contained in:
BLM 2015-07-27 13:43:34 -05:00
parent dd3253e109
commit 51cf4a0fff
2 changed files with 29 additions and 34 deletions

View File

@ -39,7 +39,7 @@ void reportOutOfMemory() {
void reportException(const std::exception& exception) {
{
const std::bad_alloc* bad_alloc = reinterpret_cast<const std::bad_alloc*>(&exception);
const std::bad_alloc* bad_alloc = dynamic_cast<const std::bad_alloc*>(&exception);
if(bad_alloc != nullptr) {
reportOutOfMemory();
return;

View File

@ -36,35 +36,26 @@ static char* textToCString(const text* text) {
//Internal function used by phone_number_in and parse_phone_number
//TODO: take a std::string to minimize copying?
ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() {
try {
PhoneNumber number;
ShortPhoneNumber* short_number;
ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) {
PhoneNumber number;
ShortPhoneNumber* short_number;
short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber));
if(short_number == nullptr) {
throw std::bad_alloc();
}
PhoneNumberUtil::ErrorType error;
error = phoneUtil->Parse(number_str, country, &number);
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
//Initialize short_number using placement new.
new(short_number) ShortPhoneNumber(number);
return short_number;
} else {
reportParseError(number_str, error);
return nullptr;
}
//TODO: check number validity.
//TODO: figure out why we need this.
} catch(const PhoneNumberTooLongException& e) {
reportException(e);
} catch(const std::exception& e) {
reportException(e);
short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber));
if(short_number == nullptr) {
throw std::bad_alloc();
}
return nullptr;
PhoneNumberUtil::ErrorType error;
error = phoneUtil->Parse(number_str, country, &number);
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
//Initialize short_number using placement new.
new(short_number) ShortPhoneNumber(number);
return short_number;
} else {
reportParseError(number_str, error);
return nullptr;
}
//TODO: check number validity.
}
//TODO: check null args (PG_ARGISNULL) and make non-strict?
@ -82,14 +73,18 @@ extern "C" {
PGDLLEXPORT Datum
phone_number_in(PG_FUNCTION_ARGS) {
const char *number_str = PG_GETARG_CSTRING(0);
try {
const char *number_str = PG_GETARG_CSTRING(0);
//TODO: use international format instead.
ShortPhoneNumber* number = parsePhoneNumber(number_str, "US");
if(number) {
PG_RETURN_POINTER(number);
} else {
PG_RETURN_NULL();
//TODO: use international format instead.
ShortPhoneNumber* number = parsePhoneNumber(number_str, "US");
if(number) {
PG_RETURN_POINTER(number);
} else {
PG_RETURN_NULL();
}
} catch(std::exception& e) {
reportException(e);
}
}