Replaced some exception handling

This commit is contained in:
BLM 2015-07-17 13:46:26 -05:00
parent ef761bbe3b
commit 787ab8a9c5
1 changed files with 58 additions and 67 deletions

View File

@ -11,19 +11,12 @@ extern "C" {
using namespace i18n::phonenumbers;
PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
static thread_local PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
//Raised when a phone number can't be parsed
class ParseException : std::exception {
public:
ParseException(PhoneNumberUtil::ErrorType et) : error_type(et) {}
const char* what() const throw() override {
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
using PNU = i18n::phonenumbers::PhoneNumberUtil;
switch(error_type) {
switch(error) {
case PNU::NO_PARSING_ERROR:
//This case shouldn't occur in correctly-functioning code.
assert(0);
return "Parsed successfully";
case PNU::NOT_A_NUMBER:
return "String does not appear to contain a phone number.";
@ -32,36 +25,34 @@ class ParseException : std::exception {
//TODO: handle more error cases specifically.
default:
//We have some generic parsing error.
return "Unrecognized number format";
return "Unable to parse number";
}
};
}
private:
PhoneNumberUtil::ErrorType error_type;
};
/*
* Utility functions for error handling
*/
//Utility functions for error handling
void reportOutOfMemory() {
static void reportOutOfMemory() {
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY)));
}
void reportParseError(const char* phone_number, ParseException& exception) {
static void reportParseError(const char* phone_number, PhoneNumberUtil::ErrorType err) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unable to parse '%s' as a phone number", phone_number),
errdetail("%s", exception.what())));
errdetail("%s", parseErrorMessage(err))));
}
void reportGenericError(std::exception& exception) {
static void reportGenericError(std::exception& exception) {
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
errmsg("C++ exception: %s", typeid(exception).name()),
errdetail("%s", exception.what())));
}
void logInfo(const char* msg) {
static void logInfo(const char* msg) {
ereport(INFO,
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
errmsg("%s", msg)));
@ -72,7 +63,8 @@ void logInfo(const char* msg) {
*/
//Internal function used by phone_number_in and parse_phone_number
PhoneNumber* parsePhoneNumber(const char* number_str, const char* country) {
PhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() {
try {
PhoneNumber *number;
number = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
@ -84,11 +76,20 @@ PhoneNumber* parsePhoneNumber(const char* number_str, const char* country) {
PhoneNumberUtil::ErrorType error;
error = phoneUtil->Parse(number_str, country, number);
if(error != PhoneNumberUtil::NO_PARSING_ERROR) {
throw ParseException(error);
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
return number;
} else {
reportParseError(number_str, error);
return nullptr;
}
//TODO: check number validity.
return number;
} catch(std::bad_alloc& e) {
reportOutOfMemory();
} catch (std::exception& e) {
reportGenericError(e);
}
return nullptr;
}
//TODO: handle non-exception thrown types? (shouldn't happen, but you never know...)
@ -111,18 +112,13 @@ extern "C" {
{
const char *number_str = PG_GETARG_CSTRING(0);
try {
PG_RETURN_POINTER(parsePhoneNumber(number_str, "US"));
} catch(std::bad_alloc& e) {
reportOutOfMemory();
} catch(ParseException e) {
reportParseError(number_str, e);
} catch (std::exception& e) {
reportGenericError(e);
}
PhoneNumber* number = parsePhoneNumber(number_str, "US");
if(number) {
PG_RETURN_POINTER(number);
} else {
PG_RETURN_NULL();
}
}
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
@ -133,18 +129,13 @@ extern "C" {
const char *number_str = PG_GETARG_CSTRING(0);
const char *country = PG_GETARG_CSTRING(1);
try {
PG_RETURN_POINTER(parsePhoneNumber(number_str, country));
} catch(std::bad_alloc& e) {
reportOutOfMemory();
} catch(ParseException e) {
reportParseError(number_str, e);
} catch (std::exception& e) {
reportGenericError(e);
}
PhoneNumber* number = parsePhoneNumber(number_str, country);
if(number) {
PG_RETURN_POINTER(number);
} else {
PG_RETURN_NULL();
}
}
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);