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) { 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) { if(bad_alloc != nullptr) {
reportOutOfMemory(); reportOutOfMemory();
return; return;

View File

@ -36,35 +36,26 @@ static char* textToCString(const text* text) {
//Internal function used by phone_number_in and parse_phone_number //Internal function used by phone_number_in and parse_phone_number
//TODO: take a std::string to minimize copying? //TODO: take a std::string to minimize copying?
ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() { ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) {
try { PhoneNumber number;
PhoneNumber number; ShortPhoneNumber* short_number;
ShortPhoneNumber* short_number;
short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber)); short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber));
if(short_number == nullptr) { if(short_number == nullptr) {
throw std::bad_alloc(); 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);
} }
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? //TODO: check null args (PG_ARGISNULL) and make non-strict?
@ -82,14 +73,18 @@ extern "C" {
PGDLLEXPORT Datum PGDLLEXPORT Datum
phone_number_in(PG_FUNCTION_ARGS) { 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. //TODO: use international format instead.
ShortPhoneNumber* number = parsePhoneNumber(number_str, "US"); ShortPhoneNumber* number = parsePhoneNumber(number_str, "US");
if(number) { if(number) {
PG_RETURN_POINTER(number); PG_RETURN_POINTER(number);
} else { } else {
PG_RETURN_NULL(); PG_RETURN_NULL();
}
} catch(std::exception& e) {
reportException(e);
} }
} }