Started working on error handling
This commit is contained in:
parent
c9ff5eb8d4
commit
cecd9d4fce
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
||||||
MODULE_big := pg_libphonenumber
|
MODULE_big := pg_libphonenumber
|
||||||
OBJS := pg_libphonenumber.o
|
OBJS := pg_libphonenumber.o
|
||||||
PG_CPPFLAGS := -fPIC
|
PG_CPPFLAGS := -fPIC -std=c++11
|
||||||
SHLIB_LINK := -lphonenumber -lstdc++
|
SHLIB_LINK := -lphonenumber -lstdc++
|
||||||
|
|
||||||
PG_CONFIG := pg_config
|
PG_CONFIG := pg_config
|
||||||
|
|
|
@ -14,6 +14,21 @@ using namespace i18n::phonenumbers;
|
||||||
|
|
||||||
PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
|
PhoneNumberUtil *phoneUtil = PhoneNumberUtil::GetInstance();
|
||||||
|
|
||||||
|
//Utility functions for error handling
|
||||||
|
|
||||||
|
void reportOutOfMemory() {
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_OUT_OF_MEMORY)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void reportGenericError(std::exception& exception) {
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
|
||||||
|
errmsg("%s", exception.what())));
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: handle non-exception thrown types? (shouldn't happen, but you never know...)
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#ifdef PG_MODULE_MAGIC
|
#ifdef PG_MODULE_MAGIC
|
||||||
PG_MODULE_MAGIC;
|
PG_MODULE_MAGIC;
|
||||||
|
@ -29,17 +44,26 @@ extern "C" {
|
||||||
PhoneNumber *number;
|
PhoneNumber *number;
|
||||||
|
|
||||||
number = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
|
number = (PhoneNumber*)palloc0(sizeof(PhoneNumber));
|
||||||
|
if(number == nullptr) {
|
||||||
|
reportOutOfMemory();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
phoneUtil->Parse(str, "US", number);
|
PhoneNumberUtil::ErrorType error;
|
||||||
}
|
error = phoneUtil->Parse(str, "US", number);
|
||||||
catch (std::exception e) {
|
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
||||||
//TODO: implement.
|
//The number was parsed correctly; return it.
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(number);
|
PG_RETURN_POINTER(number);
|
||||||
}
|
}
|
||||||
|
//TODO: handle errors.
|
||||||
|
} catch(std::bad_alloc& e) {
|
||||||
|
reportOutOfMemory();
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
reportGenericError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
PG_RETURN_NULL();
|
||||||
|
}
|
||||||
|
|
||||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_out);
|
||||||
|
|
||||||
|
@ -58,6 +82,9 @@ extern "C" {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t len = formatted.length();
|
size_t len = formatted.length();
|
||||||
|
ereport(INFO,
|
||||||
|
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
|
||||||
|
errmsg("Length: %zu", len)));
|
||||||
result = (char*)palloc(len + 1);
|
result = (char*)palloc(len + 1);
|
||||||
//TODO: test allocation.
|
//TODO: test allocation.
|
||||||
memcpy(result, formatted.data(), len);
|
memcpy(result, formatted.data(), len);
|
||||||
|
|
Loading…
Reference in New Issue