Improved formatting of PhoneNumberTooLongException

This commit is contained in:
BLM 2015-07-28 16:11:44 -05:00
parent 7550592412
commit 9061767728
3 changed files with 34 additions and 3 deletions

View File

@ -1,4 +1,5 @@
#include <exception> #include "error_handling.h"
#include <string> #include <string>
#include "phonenumbers/phonenumberutil.h" #include "phonenumbers/phonenumberutil.h"
@ -7,7 +8,7 @@ extern "C" {
#include "postgres.h" #include "postgres.h"
} }
#include "error_handling.h" #include "short_phone_number.h"
using namespace i18n::phonenumbers; using namespace i18n::phonenumbers;
@ -37,6 +38,12 @@ void reportOutOfMemory() {
errmsg("Out of memory"))); errmsg("Out of memory")));
} }
/*
* Reports a generic C++ exception as a PostgreSQL error
*
* May produce specialized SQLSTATE values and/or messages
* depending on the type of the exception
*/
void reportException(const std::exception& exception) { void reportException(const std::exception& exception) {
{ {
const std::bad_alloc* bad_alloc = dynamic_cast<const std::bad_alloc*>(&exception); const std::bad_alloc* bad_alloc = dynamic_cast<const std::bad_alloc*>(&exception);
@ -44,6 +51,17 @@ void reportException(const std::exception& exception) {
reportOutOfMemory(); reportOutOfMemory();
return; return;
} }
const PhoneNumberTooLongException* too_long =
dynamic_cast<const PhoneNumberTooLongException*>(&exception);
if(too_long != nullptr) {
std::string phone_number = too_long->number_string();
phone_number += '\0';
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("phone number '%s' is too long", phone_number.data()),
errdetail("%s", exception.what())));
return;
}
} }
//If we don't have a special way to handle this exception, report //If we don't have a special way to handle this exception, report

View File

@ -3,10 +3,16 @@
using namespace google::protobuf; using namespace google::protobuf;
using namespace i18n::phonenumbers; using namespace i18n::phonenumbers;
const PhoneNumberUtil* const PhoneNumberTooLongException::phoneUtil = PhoneNumberUtil::GetInstance();
PhoneNumberTooLongException::PhoneNumberTooLongException(const PhoneNumber& number, const char* msg) : PhoneNumberTooLongException::PhoneNumberTooLongException(const PhoneNumber& number, const char* msg) :
_number(number), std::runtime_error(msg) {}; _number(number), std::runtime_error(msg) {};
const PhoneNumberUtil* const PhoneNumberTooLongException::phoneUtil = PhoneNumberUtil::GetInstance(); std::string PhoneNumberTooLongException::number_string() const {
std::string formatted;
phoneUtil->Format(number(), PhoneNumberUtil::INTERNATIONAL, &formatted);
return formatted;
}
ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) { ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) {
uint32 country_code = number.country_code(); uint32 country_code = number.country_code();

View File

@ -1,4 +1,5 @@
#include <exception> #include <exception>
#include <string>
#include "phonenumbers/phonenumberutil.h" #include "phonenumbers/phonenumberutil.h"
@ -8,6 +9,12 @@ class PhoneNumberTooLongException : public std::runtime_error {
public: public:
PhoneNumberTooLongException(const i18n::phonenumbers::PhoneNumber& number, const char* msg); PhoneNumberTooLongException(const i18n::phonenumbers::PhoneNumber& number, const char* msg);
i18n::phonenumbers::PhoneNumber number() const {
return _number;
}
//TODO: just get the number string from which the PhoneNumber was parsed? (if it exists...)
std::string number_string() const;
private: private:
i18n::phonenumbers::PhoneNumber _number; i18n::phonenumbers::PhoneNumber _number;