Replaced some exception handling
This commit is contained in:
parent
ef761bbe3b
commit
787ab8a9c5
|
@ -11,19 +11,12 @@ extern "C" {
|
||||||
|
|
||||||
using namespace i18n::phonenumbers;
|
using namespace i18n::phonenumbers;
|
||||||
|
|
||||||
PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
|
static thread_local PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
|
||||||
|
|
||||||
//Raised when a phone number can't be parsed
|
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
|
||||||
class ParseException : std::exception {
|
|
||||||
public:
|
|
||||||
ParseException(PhoneNumberUtil::ErrorType et) : error_type(et) {}
|
|
||||||
|
|
||||||
const char* what() const throw() override {
|
|
||||||
using PNU = i18n::phonenumbers::PhoneNumberUtil;
|
using PNU = i18n::phonenumbers::PhoneNumberUtil;
|
||||||
switch(error_type) {
|
switch(error) {
|
||||||
case PNU::NO_PARSING_ERROR:
|
case PNU::NO_PARSING_ERROR:
|
||||||
//This case shouldn't occur in correctly-functioning code.
|
|
||||||
assert(0);
|
|
||||||
return "Parsed successfully";
|
return "Parsed successfully";
|
||||||
case PNU::NOT_A_NUMBER:
|
case PNU::NOT_A_NUMBER:
|
||||||
return "String does not appear to contain a phone 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.
|
//TODO: handle more error cases specifically.
|
||||||
default:
|
default:
|
||||||
//We have some generic parsing error.
|
//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
|
static void reportOutOfMemory() {
|
||||||
|
|
||||||
void reportOutOfMemory() {
|
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_OUT_OF_MEMORY)));
|
(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,
|
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", exception.what())));
|
errdetail("%s", parseErrorMessage(err))));
|
||||||
}
|
}
|
||||||
|
|
||||||
void reportGenericError(std::exception& exception) {
|
static void reportGenericError(std::exception& exception) {
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||||
errmsg("C++ exception: %s", typeid(exception).name()),
|
errmsg("C++ exception: %s", typeid(exception).name()),
|
||||||
errdetail("%s", exception.what())));
|
errdetail("%s", exception.what())));
|
||||||
}
|
}
|
||||||
|
|
||||||
void logInfo(const char* msg) {
|
static void logInfo(const char* msg) {
|
||||||
ereport(INFO,
|
ereport(INFO,
|
||||||
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
|
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
|
||||||
errmsg("%s", msg)));
|
errmsg("%s", msg)));
|
||||||
|
@ -72,7 +63,8 @@ void logInfo(const char* msg) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//Internal function used by phone_number_in and parse_phone_number
|
//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;
|
PhoneNumber *number;
|
||||||
|
|
||||||
number = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
|
number = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
|
||||||
|
@ -84,11 +76,20 @@ PhoneNumber* parsePhoneNumber(const char* number_str, const char* country) {
|
||||||
|
|
||||||
PhoneNumberUtil::ErrorType error;
|
PhoneNumberUtil::ErrorType error;
|
||||||
error = phoneUtil->Parse(number_str, country, number);
|
error = phoneUtil->Parse(number_str, country, number);
|
||||||
if(error != PhoneNumberUtil::NO_PARSING_ERROR) {
|
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
||||||
throw ParseException(error);
|
return number;
|
||||||
|
} else {
|
||||||
|
reportParseError(number_str, error);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
//TODO: check number validity.
|
//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...)
|
//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);
|
const char *number_str = PG_GETARG_CSTRING(0);
|
||||||
|
|
||||||
try {
|
PhoneNumber* number = parsePhoneNumber(number_str, "US");
|
||||||
PG_RETURN_POINTER(parsePhoneNumber(number_str, "US"));
|
if(number) {
|
||||||
} catch(std::bad_alloc& e) {
|
PG_RETURN_POINTER(number);
|
||||||
reportOutOfMemory();
|
} else {
|
||||||
} catch(ParseException e) {
|
|
||||||
reportParseError(number_str, e);
|
|
||||||
} catch (std::exception& e) {
|
|
||||||
reportGenericError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(parse_phone_number);
|
||||||
|
|
||||||
|
@ -133,18 +129,13 @@ extern "C" {
|
||||||
const char *number_str = PG_GETARG_CSTRING(0);
|
const char *number_str = PG_GETARG_CSTRING(0);
|
||||||
const char *country = PG_GETARG_CSTRING(1);
|
const char *country = PG_GETARG_CSTRING(1);
|
||||||
|
|
||||||
try {
|
PhoneNumber* number = parsePhoneNumber(number_str, country);
|
||||||
PG_RETURN_POINTER(parsePhoneNumber(number_str, country));
|
if(number) {
|
||||||
} catch(std::bad_alloc& e) {
|
PG_RETURN_POINTER(number);
|
||||||
reportOutOfMemory();
|
} else {
|
||||||
} catch(ParseException e) {
|
|
||||||
reportParseError(number_str, e);
|
|
||||||
} catch (std::exception& e) {
|
|
||||||
reportGenericError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue