Refactored error handling functions

This commit is contained in:
BLM 2015-07-22 12:44:00 -05:00
parent 89d2c9a241
commit bca033d1f8
4 changed files with 73 additions and 48 deletions

View File

@ -6,8 +6,10 @@ version := 1.0
extension_script := $(EXTENSION)--$(version).sql
DATA_built := $(extension_script)
cpp_files := $(wildcard *.cpp)
MODULE_big := pg_libphonenumber
OBJS := pg_libphonenumber.o short_phone_number.o
OBJS := $(patsubst %.cpp,%.o,$(cpp_files))
PG_CPPFLAGS := -fPIC -std=c++11
ifeq ($(CONFIG),debug)
PG_CPPFLAGS += -g -Og
@ -27,3 +29,4 @@ $(extension_script): $(EXTENSION).sql.template get_sizeof_phone_number
get_sizeof_phone_number: get_sizeof_phone_number.cpp
$(CXX) -std=c++11 $< -o $@

56
error_handling.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <exception>
#include <string>
#include "phonenumbers/phonenumberutil.h"
extern "C" {
#include "postgres.h"
}
using namespace i18n::phonenumbers;
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
using PNU = i18n::phonenumbers::PhoneNumberUtil;
switch(error) {
case PNU::NO_PARSING_ERROR:
return "Parsed successfully";
case PNU::NOT_A_NUMBER:
return "String does not appear to contain a phone number.";
case PNU::INVALID_COUNTRY_CODE_ERROR:
return "Invalid country code";
//TODO: handle more error cases specifically.
default:
//We have some generic parsing error.
return "Unable to parse number";
}
}
/*
* Utility functions for error handling
*/
void reportOutOfMemory() {
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY)));
}
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", parseErrorMessage(err))));
}
void reportGenericError(const 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) {
ereport(INFO,
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
errmsg("%s", msg)));
}

10
error_handling.h Normal file
View File

@ -0,0 +1,10 @@
#include <exception>
#include "phonenumbers/phonenumberutil.h"
void reportOutOfMemory();
void reportParseError(const char* phone_number, i18n::phonenumbers::PhoneNumberUtil::ErrorType err);
void reportGenericError(const std::exception& exception);
void logInfo(const char* msg);

View File

@ -9,57 +9,13 @@ extern "C" {
#include "fmgr.h"
}
#include "error_handling.h"
#include "short_phone_number.h"
using namespace i18n::phonenumbers;
static const PhoneNumberUtil* const phoneUtil = PhoneNumberUtil::GetInstance();
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
using PNU = i18n::phonenumbers::PhoneNumberUtil;
switch(error) {
case PNU::NO_PARSING_ERROR:
return "Parsed successfully";
case PNU::NOT_A_NUMBER:
return "String does not appear to contain a phone number.";
case PNU::INVALID_COUNTRY_CODE_ERROR:
return "Invalid country code";
//TODO: handle more error cases specifically.
default:
//We have some generic parsing error.
return "Unable to parse number";
}
}
/*
* Utility functions for error handling
*/
static void reportOutOfMemory() {
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY)));
}
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", parseErrorMessage(err))));
}
static void reportGenericError(const std::exception& exception) {
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
errmsg("C++ exception: %s", typeid(exception).name()),
errdetail("%s", exception.what())));
}
static void logInfo(const char* msg) {
ereport(INFO,
(errcode(ERRCODE_SUCCESSFUL_COMPLETION),
errmsg("%s", msg)));
}
/*
* Utility functions
*/
@ -107,8 +63,7 @@ ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country)
return nullptr;
}
//TODO: handle non-exception thrown types? (shouldn't happen, but you never know...)
//TODO: check null args? (PG_ARGISNULL). Make non-strict?
//TODO: check null args (PG_ARGISNULL) and make non-strict?
/*
* Extension functions
@ -200,6 +155,7 @@ extern "C" {
PGDLLEXPORT PG_FUNCTION_INFO_V1(phone_number_recv);
//TODO: handle leading zeroes?
//TODO: implement these correctly.
PGDLLEXPORT
Datum
phone_number_recv(PG_FUNCTION_ARGS)