Adjusted error handling

This commit is contained in:
BLM 2015-07-24 16:22:04 -05:00
parent 2e40efe3b2
commit 0df1546bd5
3 changed files with 35 additions and 32 deletions

View File

@ -7,6 +7,8 @@ extern "C" {
#include "postgres.h" #include "postgres.h"
} }
#include "error_handling.h"
using namespace i18n::phonenumbers; using namespace i18n::phonenumbers;
static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) { static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
@ -31,7 +33,25 @@ static const char* parseErrorMessage(PhoneNumberUtil::ErrorType error) {
void reportOutOfMemory() { void reportOutOfMemory() {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY))); (errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("Out of memory")));
}
void reportException(const std::exception& exception) {
{
const std::bad_alloc* bad_alloc = reinterpret_cast<const std::bad_alloc*>(&exception);
if(bad_alloc != nullptr) {
reportOutOfMemory();
return;
}
}
//If we don't have a special way to handle this exception, report
//a generic error.
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
errmsg("C++ exception: %s", typeid(exception).name()),
errdetail("%s", exception.what())));
} }
void reportParseError(const char* phone_number, PhoneNumberUtil::ErrorType err) { void reportParseError(const char* phone_number, PhoneNumberUtil::ErrorType err) {
@ -41,13 +61,6 @@ void reportParseError(const char* phone_number, PhoneNumberUtil::ErrorType err)
errdetail("%s", parseErrorMessage(err)))); 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) { void logInfo(const char* msg) {
ereport(INFO, ereport(INFO,
(errcode(ERRCODE_SUCCESSFUL_COMPLETION), (errcode(ERRCODE_SUCCESSFUL_COMPLETION),

View File

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

View File

@ -57,13 +57,11 @@ ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country)
return nullptr; return nullptr;
} }
//TODO: check number validity. //TODO: check number validity.
} catch(const std::bad_alloc& e) {
reportOutOfMemory();
//TODO: figure out why we need this. //TODO: figure out why we need this.
} catch(const PhoneNumberTooLongException& e) { } catch(const PhoneNumberTooLongException& e) {
reportGenericError(e); reportException(e);
} catch(const std::exception& e) { } catch(const std::exception& e) {
reportGenericError(e); reportException(e);
} }
return nullptr; return nullptr;
@ -115,10 +113,8 @@ extern "C" {
} else { } else {
PG_RETURN_NULL(); PG_RETURN_NULL();
} }
} catch(std::bad_alloc& e) {
reportOutOfMemory();
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
} }
@ -144,10 +140,8 @@ extern "C" {
result[len] = '\0'; result[len] = '\0';
PG_RETURN_CSTRING(result); PG_RETURN_CSTRING(result);
} catch(const std::bad_alloc& e) {
reportOutOfMemory();
} catch (const std::exception& e) { } catch (const std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -165,10 +159,8 @@ extern "C" {
//TODO: make portable (fix endianness issues, etc.). //TODO: make portable (fix endianness issues, etc.).
pq_copymsgbytes(buf, (char*)number, sizeof(ShortPhoneNumber)); pq_copymsgbytes(buf, (char*)number, sizeof(ShortPhoneNumber));
PG_RETURN_POINTER(number); PG_RETURN_POINTER(number);
} catch(const std::bad_alloc& e) {
reportOutOfMemory();
} catch (const std::exception& e) { } catch (const std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -185,10 +177,8 @@ extern "C" {
pq_begintypsend(&buf); pq_begintypsend(&buf);
pq_sendbytes(&buf, (const char*)number, sizeof(ShortPhoneNumber)); pq_sendbytes(&buf, (const char*)number, sizeof(ShortPhoneNumber));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
} catch(const std::bad_alloc& e) {
reportOutOfMemory();
} catch (const std::exception& e) { } catch (const std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -204,7 +194,7 @@ extern "C" {
PG_RETURN_BOOL(*number1 == *number2); PG_RETURN_BOOL(*number1 == *number2);
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -220,7 +210,7 @@ extern "C" {
PG_RETURN_BOOL(*number1 != *number2); PG_RETURN_BOOL(*number1 != *number2);
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -236,7 +226,7 @@ extern "C" {
PG_RETURN_BOOL(number1->compare_fast(*number2) < 0); PG_RETURN_BOOL(number1->compare_fast(*number2) < 0);
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -252,7 +242,7 @@ extern "C" {
PG_RETURN_BOOL(number1->compare_fast(*number2) <= 0); PG_RETURN_BOOL(number1->compare_fast(*number2) <= 0);
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -268,7 +258,7 @@ extern "C" {
PG_RETURN_BOOL(number1->compare_fast(*number2) > 0); PG_RETURN_BOOL(number1->compare_fast(*number2) > 0);
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -284,7 +274,7 @@ extern "C" {
PG_RETURN_BOOL(number1->compare_fast(*number2) >= 0); PG_RETURN_BOOL(number1->compare_fast(*number2) >= 0);
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();
@ -302,7 +292,7 @@ extern "C" {
PG_RETURN_INT32(clip<int64>(compared, -1, 1)); PG_RETURN_INT32(clip<int64>(compared, -1, 1));
} catch(std::exception& e) { } catch(std::exception& e) {
reportGenericError(e); reportException(e);
} }
PG_RETURN_NULL(); PG_RETURN_NULL();