From ff1eb78369783be580d2a78a4b8b45fb241c8761 Mon Sep 17 00:00:00 2001 From: Ryszard Matuszyk Date: Wed, 25 Feb 2015 09:31:21 +0100 Subject: [PATCH] [contact] Removed try/catch error handling Verification] TCT test should pass: 438/438 Change-Id: I9d8c5b2aca7c9ccc1d9d8526cd6d493bd9cf99dd Signed-off-by: Ryszard Matuszyk --- src/contact/addressbook.cc | 404 +++++++--- src/contact/addressbook.h | 46 +- src/contact/contact_instance.cc | 366 +++++---- src/contact/contact_manager.cc | 442 +++++++---- src/contact/contact_manager.h | 37 +- src/contact/contact_util.cc | 1323 ++++++++++++++++++++----------- src/contact/contact_util.h | 138 ++-- src/contact/person.cc | 49 +- src/contact/person.h | 11 +- 9 files changed, 1777 insertions(+), 1039 deletions(-) diff --git a/src/contact/addressbook.cc b/src/contact/addressbook.cc index f8383840..2799b5aa 100644 --- a/src/contact/addressbook.cc +++ b/src/contact/addressbook.cc @@ -48,8 +48,9 @@ inline bool IsUnified(long id) { return id == kUnifiedAddressBookId; } } // anonymous namespace -void AddressBookGet(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookGet(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; int contact_id = common::stol(FromJson(args, "id")); @@ -59,24 +60,32 @@ void AddressBookGet(const JsonObject& args, JsonObject& out) { &contacts_record); if (CONTACTS_ERROR_NONE != err) { LoggerW("Contacts record get error, error code: %d", err); - throw common::NotFoundException("Contacts record get error"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Contacts record get error"); } ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); out.insert(std::make_pair("id", std::to_string(contact_id))); - ContactUtil::ImportContactFromContactsRecord(*contacts_record_ptr, &out); - ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); + status = + ContactUtil::ImportContactFromContactsRecord(*contacts_record_ptr, &out); + if (status.IsError()) return status; + + status = ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookAdd(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookAdd(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; const JsonObject& contact = FromJson(args, "contact"); if (!IsNull(contact, "id")) { LoggerW("Contact already exists"); - throw common::UnknownException("Contact already exists"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contact already exists"); } contacts_record_h contacts_record = nullptr; @@ -84,26 +93,29 @@ void AddressBookAdd(const JsonObject& args, JsonObject& out) { err = contacts_record_create(_contacts_contact._uri, &contacts_record); if (CONTACTS_ERROR_NONE != err) { LoggerW("Contacts record create error, error code: %d", err); - throw common::UnknownException("Contacts record create error"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Contacts record create error"); } // contacts_record starts to be protected by unique_ptr ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); - ContactUtil::ExportContactToContactsRecord(*contacts_record_ptr, contact); + status = + ContactUtil::ExportContactToContactsRecord(*contacts_record_ptr, contact); + if (status.IsError()) return status; int id = -1; err = contacts_db_insert_record(*contacts_record_ptr, &id); if (CONTACTS_ERROR_NONE != err) { LoggerW("Contacts db insert error, error code: %d", err); - throw common::UnknownException("Contacts db insert error"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts db insert error"); } contacts_record_h reset_record; err = contacts_db_get_record(_contacts_contact._uri, id, &reset_record); if (CONTACTS_ERROR_NONE != err) { LoggerW("Contacts record get error, error code: %d", err); - throw common::UnknownException("Contacts record get error"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record get error"); } if (nullptr != reset_record) { LoggerE("reset"); @@ -112,11 +124,15 @@ void AddressBookAdd(const JsonObject& args, JsonObject& out) { out.insert(std::make_pair("id", JsonValue{std::to_string(id)})); - ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); + status = ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookUpdate(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookUpdate(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; const JsonObject& contact = FromJson(args, "contact"); const JsonObject& addressbook = FromJson(args, "addressBook"); @@ -124,12 +140,13 @@ void AddressBookUpdate(const JsonObject& args, JsonObject& out) { if (IsNull(contact, "id")) { LoggerW("Contact doesn't exist"); - throw common::UnknownException("Contact doesn't exist"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contact doesn't exist"); } if (IsNull(addressbook, "id")) { LoggerE("Contact is not saved in database"); - throw common::InvalidValuesException("Contact is not saved in database"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Contact is not saved in database"); } int err = CONTACTS_ERROR_NONE; @@ -137,52 +154,68 @@ void AddressBookUpdate(const JsonObject& args, JsonObject& out) { err = contacts_db_get_record(_contacts_contact._uri, contactId, &to_update); if (CONTACTS_ERROR_NONE != err) { LoggerW("Problem with getting contact. Error: %d", err); - throw common::NotFoundException("Problem with getting contact"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Problem with getting contact"); } ContactUtil::ContactsRecordHPtr contacts_record_ptr( &to_update, ContactUtil::ContactsDeleter); - ContactUtil::ExportContactToContactsRecord(*contacts_record_ptr, contact); + status = + ContactUtil::ExportContactToContactsRecord(*contacts_record_ptr, contact); + if (status.IsError()) return status; + err = contacts_db_update_record(*contacts_record_ptr); if (CONTACTS_ERROR_NONE != err) { if (CONTACTS_ERROR_INVALID_PARAMETER == err) { LoggerE("Error during executing contacts_db_update_record(). Error: %d", err); - throw common::NotFoundException( + return PlatformResult( + ErrorCode::NOT_FOUND_ERR, "Error during executing contacts_db_update_record()."); } if (CONTACTS_ERROR_DB == err) { LoggerE("Error during executing contacts_db_update_record(). Error: %d", err); - throw common::UnknownException( + return PlatformResult( + ErrorCode::UNKNOWN_ERR, "Error during executing contacts_db_update_record()."); } } - ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); + status = + ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookRemove(const JsonObject& args, JsonObject&) { +PlatformResult AddressBookRemove(const JsonObject& args, JsonObject&) { LoggerE("entered"); - ContactUtil::CheckDBConnection(); + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; int contact_id = common::stol(FromJson(args, "id")); if (contact_id < 0) { - throw common::InvalidValuesException("Nagative contact id"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Nagative contact id"); } int err = contacts_db_delete_record(_contacts_contact._uri, contact_id); if (CONTACTS_ERROR_NO_DATA == err) { - throw common::NotFoundException("Remove failed: contact not found"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Remove failed: contact not found"); } else if (CONTACTS_ERROR_NONE != err) { - throw common::UnknownException("Contacts record delete error"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Contacts record delete error"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookAddBatch(const JsonObject& args, JsonArray& out) { +PlatformResult AddressBookAddBatch(const JsonObject& args, JsonArray& out) { LoggerD("Enter"); - ContactUtil::CheckDBConnection(); + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; const JsonArray& batch_args = FromJson(args, "batchArgs"); long addressBookId = @@ -194,7 +227,7 @@ void AddressBookAddBatch(const JsonObject& args, JsonArray& out) { int error_code = contacts_list_create(&contacts_list); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("list creation failed, code: %d", error_code); - throw new common::UnknownException("list creation failed"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "list creation failed"); } ContactUtil::ContactsListHPtr contacts_list_ptr( &contacts_list, ContactUtil::ContactsListDeleter); @@ -205,18 +238,24 @@ void AddressBookAddBatch(const JsonObject& args, JsonArray& out) { err = contacts_record_create(_contacts_contact._uri, &contacts_record); if (CONTACTS_ERROR_NONE != err) { LoggerW("Contacts record create error, error code: %d", err); - throw common::UnknownException("Contacts record create error"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Contacts record create error"); } ContactUtil::ContactsRecordHPtr x(&contacts_record, ContactUtil::ContactsDeleter); - ContactUtil::ExportContactToContactsRecord(contacts_record, - JsonCast(item)); - ContactUtil::SetIntInRecord( + PlatformResult status = ContactUtil::ExportContactToContactsRecord( + contacts_record, JsonCast(item)); + if (status.IsError()) return status; + + status = ContactUtil::SetIntInRecord( contacts_record, _contacts_contact.address_book_id, addressBookId); + if (status.IsError()) return status; + error_code = contacts_list_add(*contacts_list_ptr, *(x.release())); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("error during add record to list, code: %d", error_code); - throw new common::UnknownException("error during add record to list"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "error during add record to list"); } } @@ -229,7 +268,8 @@ void AddressBookAddBatch(const JsonObject& args, JsonArray& out) { ids = NULL; } LoggerE("inserting contacts to db fails, code: %d", error_code); - throw new common::UnknownException("inserting contacts to db fails"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "inserting contacts to db fails"); } if (length != count) { LoggerW("Added different number of contacts"); @@ -246,20 +286,27 @@ void AddressBookAddBatch(const JsonObject& args, JsonArray& out) { ids = NULL; } LoggerW("Contacts record get error, error code: %d", error_code); - throw common::UnknownException("Contacts record get error"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Contacts record get error"); } - ContactUtil::ImportContactFromContactsRecord(contact_record, &out_object); + PlatformResult status = ContactUtil::ImportContactFromContactsRecord( + contact_record, &out_object); + if (status.IsError()) return status; + out.push_back(JsonValue{out_object}); } if (ids) { free(ids); ids = NULL; } + + return PlatformResult(ErrorCode::NO_ERROR); } // TODO all batch operations should be implemented using CAPI batch functions -void AddressBookBatchFunc(NativeFunction impl, const char* single_arg_name, - const JsonObject& args, JsonArray& out) { +PlatformResult AddressBookBatchFunc(NativeFunction impl, + const char* single_arg_name, + const JsonObject& args, JsonArray& out) { const JsonArray& batch_args = FromJson(args, "batchArgs"); const JsonObject& address_book = FromJson(args, "addressBook"); @@ -272,15 +319,20 @@ void AddressBookBatchFunc(NativeFunction impl, const char* single_arg_name, single_args.insert(std::make_pair(single_arg_name, item)); JsonObject single_out; - impl(single_args, single_out); + PlatformResult status = impl(single_args, single_out); + if (status.IsError()) return status; + if (!single_out.empty()) { out.push_back(JsonValue{single_out}); } } + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookFind(const JsonObject& args, JsonArray& array) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookFind(const JsonObject& args, JsonArray& array) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; // TODO implement contact filter and sorting. const JsonObject& address_book = FromJson(args, "addressBook"); @@ -292,59 +344,79 @@ void AddressBookFind(const JsonObject& args, JsonArray& array) { contacts_list_h list = nullptr; error_code = contacts_query_create(_contacts_contact._uri, &query); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + if (status.IsError()) return status; ContactUtil::ContactsQueryHPtr query_ptr(&query, ContactUtil::ContactsQueryDeleter); error_code = contacts_filter_create(_contacts_contact._uri, &filter); - ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_create"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_create"); + if (status.IsError()) return status; ContactUtil::ContactsFilterPtr filter_ptr(filter, ContactUtil::ContactsFilterDeleter); error_code = contacts_filter_add_int(filter, _contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, addressbook_id); - ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + if (status.IsError()) return status; error_code = contacts_query_set_filter(query, filter); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + if (status.IsError()) return status; error_code = contacts_db_get_records_with_query(query, 0, 0, &list); - ContactUtil::ErrorChecker(error_code, - "Failed contacts_db_get_records_with_query"); + status = ContactUtil::ErrorChecker( + error_code, "Failed contacts_db_get_records_with_query"); + if (status.IsError()) return status; ContactUtil::ContactsListHPtr list_ptr(&list, ContactUtil::ContactsListDeleter); int record_count = 0; error_code = contacts_list_get_count(list, &record_count); - ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); + if (status.IsError()) return status; contacts_list_first(list); for (unsigned int i = 0; i < record_count; i++) { contacts_record_h record; error_code = contacts_list_get_current_record_p(list, &record); - ContactUtil::ErrorChecker(error_code, - "Failed contacts_list_get_current_record_p"); + status = ContactUtil::ErrorChecker( + error_code, "Failed contacts_list_get_current_record_p"); + if (status.IsError()) return status; int id_value = 0; error_code = contacts_record_get_int(record, _contacts_contact.id, &id_value); - ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); + if (status.IsError()) return status; array.push_back(JsonValue(static_cast(id_value))); contacts_list_next(list); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookAddGroup(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookAddGroup(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + const JsonObject& group = FromJson(args, "group"); if (!IsNull(group, "id") || !IsNull(group, "addressBookId")) { LoggerE("Group object is previously added"); - throw common::InvalidValuesException("Group object is previously added"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Group object is previously added"); } int err = CONTACTS_ERROR_NONE; contacts_record_h contacts_record = nullptr; err = contacts_record_create(_contacts_group._uri, &contacts_record); - ContactUtil::ErrorChecker(err, - "Error during executing contacts_record_create()"); + status = ContactUtil::ErrorChecker( + err, "Error during executing contacts_record_create()"); + if (status.IsError()) return status; ContactUtil::ContactsRecordHPtr record(&contacts_record, ContactUtil::ContactsDeleter); @@ -352,25 +424,33 @@ void AddressBookAddGroup(const JsonObject& args, JsonObject& out) { long addressbook_id = common::stol(FromJson(args, "addressBookId")); addressbook_id = (IsUnified(addressbook_id)) ? 0 : addressbook_id; - ContactUtil::SetIntInRecord(contacts_record, _contacts_group.address_book_id, - addressbook_id); + status = ContactUtil::SetIntInRecord( + contacts_record, _contacts_group.address_book_id, addressbook_id); + if (status.IsError()) return status; + + status = + ContactUtil::ExportContactGroupToContactsRecord(contacts_record, group); + if (status.IsError()) return status; - ContactUtil::ExportContactGroupToContactsRecord(contacts_record, group); int groupId = 0; err = contacts_db_insert_record(contacts_record, &groupId); - ContactUtil::ErrorChecker(err, "Error during insert group record"); + status = ContactUtil::ErrorChecker(err, "Error during insert group record"); + if (status.IsError()) return status; out.insert(std::make_pair("id", std::to_string(groupId))); out.insert(std::make_pair("addressBookId", std::to_string(addressbook_id))); + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookGetGroup(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookGetGroup(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; long id = common::stol(FromJson(args, "id")); if (id < 0) { - throw common::InvalidValuesException("Incorrect group id"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Incorrect group id"); } int err = CONTACTS_ERROR_NONE; @@ -378,7 +458,7 @@ void AddressBookGetGroup(const JsonObject& args, JsonObject& out) { err = contacts_db_get_record(_contacts_group._uri, id, &contacts_record); if (CONTACTS_ERROR_NONE != err || nullptr == contacts_record) { LoggerE("Group not exist"); - throw common::NotFoundException("Group not exist"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Group not exist"); } ContactUtil::ContactsRecordHPtr record(&contacts_record, @@ -388,24 +468,33 @@ void AddressBookGetGroup(const JsonObject& args, JsonObject& out) { common::stol(FromJson(args, "addressBook", "id")); if (IsUnified(addressbook_id)) { int address_book_id = 0; - ContactUtil::GetIntFromRecord( + status = ContactUtil::GetIntFromRecord( contacts_record, _contacts_group.address_book_id, &address_book_id); + if (status.IsError()) return status; + if (address_book_id != addressbook_id) { - throw common::NotFoundException("No group in this address book."); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "No group in this address book."); } } - ContactUtil::ImportContactGroupFromContactsRecord(contacts_record, &out); + status = + ContactUtil::ImportContactGroupFromContactsRecord(contacts_record, &out); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookUpdateGroup(const JsonObject& args, JsonObject&) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookUpdateGroup(const JsonObject& args, JsonObject&) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; const JsonObject& group = FromJson(args, "group"); if (IsNull(group, "id") || IsNull(group, "addressBookId")) { LoggerE("Group object is not added"); - throw common::InvalidValuesException("Group object is not added"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Group object is not added"); } long addressbook_id = @@ -414,17 +503,18 @@ void AddressBookUpdateGroup(const JsonObject& args, JsonObject&) { common::stol(FromJson(group, "addressBookId")); if (IsUnified(addressbook_id) && (addressbook_id != group_addressbook_id)) { LoggerE("Wrong address book"); - throw common::InvalidValuesException("Wrong address book"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Wrong address book"); } if (FromJson(group, "readOnly")) { LoggerW("Group is readonly - cancel update"); - throw common::UnknownException("Group is readonly - cancel update"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Group is readonly - cancel update"); } long group_id = common::stol(FromJson(group, "id")); if (group_id < 0) { - throw common::InvalidValuesException("Incorrect group id"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Incorrect group id"); } contacts_record_h contacts_record = nullptr; @@ -432,30 +522,38 @@ void AddressBookUpdateGroup(const JsonObject& args, JsonObject&) { contacts_db_get_record(_contacts_group._uri, group_id, &contacts_record); if (CONTACTS_ERROR_NONE != err || nullptr == contacts_record) { LoggerE("Group not exist"); - throw common::NotFoundException("Group not exist"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Group not exist"); } - ContactUtil::ErrorChecker(err, - "Error during executing contacts_db_get_record()"); + status = ContactUtil::ErrorChecker( + err, "Error during executing contacts_db_get_record()"); + if (status.IsError()) return status; ContactUtil::ContactsRecordHPtr record(&contacts_record, ContactUtil::ContactsDeleter); - ContactUtil::ExportContactGroupToContactsRecord(contacts_record, group); + status = + ContactUtil::ExportContactGroupToContactsRecord(contacts_record, group); + if (status.IsError()) return status; err = contacts_db_update_record(contacts_record); if (CONTACTS_ERROR_INVALID_PARAMETER == err) { LoggerE("Problem during db_update_record"); - throw common::NotFoundException("Problem during db_update_record"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Problem during db_update_record"); } - ContactUtil::ErrorChecker(err, "Problem during db_update_record"); + status = ContactUtil::ErrorChecker(err, "Problem during db_update_record"); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookRemoveGroup(const JsonObject& args, JsonObject&) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookRemoveGroup(const JsonObject& args, JsonObject&) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; long id = common::stol(FromJson(args, "id")); if (id < 0) { - throw common::InvalidValuesException("Incorrect group id"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Incorrect group id"); } int err; @@ -464,29 +562,35 @@ void AddressBookRemoveGroup(const JsonObject& args, JsonObject&) { contacts_record_h contacts_record = nullptr; err = contacts_db_get_record(_contacts_group._uri, id, &contacts_record); if (CONTACTS_ERROR_NONE != err || contacts_record == nullptr) { - throw common::NotFoundException("Group not exist"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Group not exist"); } int group_addressbook_id = 0; - ContactUtil::GetIntFromRecord(contacts_record, - _contacts_group.address_book_id, - &group_addressbook_id); + status = ContactUtil::GetIntFromRecord(contacts_record, + _contacts_group.address_book_id, + &group_addressbook_id); + if (status.IsError()) return status; if (group_addressbook_id != addressbook_id) { - throw common::UnknownException( - "Contact is not a member of this address book"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Contact is not a member of this address book"); } } err = contacts_db_delete_record(_contacts_group._uri, id); if (CONTACTS_ERROR_INVALID_PARAMETER == err) { - throw common::UnknownException("Problem during db_delete_record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Problem during db_delete_record"); } - ContactUtil::ErrorChecker(err, "Problem during db_delete_record"); + status = ContactUtil::ErrorChecker(err, "Problem during db_delete_record"); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookGetGroups(const JsonObject& args, JsonArray& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookGetGroups(const JsonObject& args, JsonArray& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; int err = CONTACTS_ERROR_NONE; contacts_list_h groups_list = nullptr; @@ -494,37 +598,53 @@ void AddressBookGetGroups(const JsonObject& args, JsonArray& out) { if (IsUnified(addressbook_id)) { err = contacts_db_get_all_records(_contacts_group._uri, 0, 0, &groups_list); - ContactUtil::ErrorChecker(err, "Fail to get group list"); + status = ContactUtil::ErrorChecker(err, "Fail to get group list"); + if (status.IsError()) return status; } else { contacts_query_h query = nullptr; contacts_filter_h filter = nullptr; err = contacts_query_create(_contacts_group._uri, &query); - ContactUtil::ErrorChecker(err, "Fail to get contacts_query_create "); + status = + ContactUtil::ErrorChecker(err, "Fail to get contacts_query_create "); + if (status.IsError()) return status; err = contacts_filter_create(_contacts_group._uri, &filter); - ContactUtil::ErrorChecker(err, "Fail to get contacts_filter_create "); + status = + ContactUtil::ErrorChecker(err, "Fail to get contacts_filter_create "); + if (status.IsError()) return status; err = contacts_filter_add_int(filter, _contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, addressbook_id); - ContactUtil::ErrorChecker(err, "Fail to get contacts_filter_add_int "); + status = + ContactUtil::ErrorChecker(err, "Fail to get contacts_filter_add_int "); + if (status.IsError()) return status; err = contacts_query_set_filter(query, filter); - ContactUtil::ErrorChecker(err, "Fail to get contacts_query_set_filter "); + status = ContactUtil::ErrorChecker( + err, "Fail to get contacts_query_set_filter "); + if (status.IsError()) return status; err = contacts_db_get_records_with_query(query, 0, 0, &groups_list); - ContactUtil::ErrorChecker( + status = ContactUtil::ErrorChecker( err, "Fail to get contacts_db_get_records_with_query "); + if (status.IsError()) return status; err = contacts_filter_destroy(filter); - ContactUtil::ErrorChecker(err, "Fail to get contacts_filter_destroy "); + status = + ContactUtil::ErrorChecker(err, "Fail to get contacts_filter_destroy "); + if (status.IsError()) return status; err = contacts_query_destroy(query); - ContactUtil::ErrorChecker(err, "Fail to get contacts_query_destroy "); + status = + ContactUtil::ErrorChecker(err, "Fail to get contacts_query_destroy "); + if (status.IsError()) return status; } int record_count = 0; err = contacts_list_get_count(groups_list, &record_count); - ContactUtil::ErrorChecker(err, "Fail to get contacts_list_get_count "); + status = + ContactUtil::ErrorChecker(err, "Fail to get contacts_list_get_count "); + if (status.IsError()) return status; contacts_list_first(groups_list); @@ -533,12 +653,14 @@ void AddressBookGetGroups(const JsonObject& args, JsonArray& out) { err = contacts_list_get_current_record_p(groups_list, &contacts_record); if (CONTACTS_ERROR_NONE != err || nullptr == contacts_record) { LoggerE("Fail to get group record"); - throw common::UnknownException("Fail to get group record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Fail to get group record"); } JsonValue group{JsonObject{}}; - ContactUtil::ImportContactGroupFromContactsRecord(contacts_record, - &group.get()); + status = ContactUtil::ImportContactGroupFromContactsRecord( + contacts_record, &group.get()); + if (status.IsError()) return status; + out.push_back(group); if (i < record_count - 1) { @@ -549,6 +671,8 @@ void AddressBookGetGroups(const JsonObject& args, JsonArray& out) { } } } + + return PlatformResult(ErrorCode::NO_ERROR); } namespace { @@ -556,7 +680,12 @@ void AddressBookListenerCallback(const char* view_uri, void* user_data) { LoggerD("entered"); (void)view_uri; (void)user_data; - ContactUtil::CheckDBConnection(); + + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) { + LoggerE("Fail to check db connection: %s", status.message().c_str()); + return; + } contacts_list_h contacts_list = nullptr; @@ -621,21 +750,27 @@ void AddressBookListenerCallback(const char* view_uri, void* user_data) { int changed_ab_id = 0; int changed_type = 0; - try { - ContactUtil::GetIntFromRecord(contact_updated_record, - _contacts_contact_updated_info.contact_id, - &changed_id); - - ContactUtil::GetIntFromRecord( - contact_updated_record, - _contacts_contact_updated_info.address_book_id, &changed_ab_id); + PlatformResult status = ContactUtil::GetIntFromRecord( + contact_updated_record, _contacts_contact_updated_info.contact_id, + &changed_id); + if (status.IsError()) { + LoggerE("Fail to get int from record: %s", status.message().c_str()); + break; + } - ContactUtil::GetIntFromRecord(contact_updated_record, - _contacts_contact_updated_info.type, - &changed_type); + status = ContactUtil::GetIntFromRecord( + contact_updated_record, + _contacts_contact_updated_info.address_book_id, &changed_ab_id); + if (status.IsError()) { + LoggerE("Fail to get int from record: %s", status.message().c_str()); + break; } - catch (const PlatformException&) { - LoggerE("fail to get int from record"); + + status = ContactUtil::GetIntFromRecord( + contact_updated_record, _contacts_contact_updated_info.type, + &changed_type); + if (status.IsError()) { + LoggerE("Fail to get int from record: %s", status.message().c_str()); break; } @@ -655,8 +790,13 @@ void AddressBookListenerCallback(const char* view_uri, void* user_data) { &contacts_record, ContactUtil::ContactsDeleter); JsonValue contact{JsonObject{}}; - ContactUtil::ImportContactFromContactsRecord( + status = ContactUtil::ImportContactFromContactsRecord( contacts_record, &contact.get()); + if (status.IsError()) { + LoggerE("Fail to get contact from record: %s", + status.message().c_str()); + break; + } if (CONTACTS_CHANGE_INSERTED == changed_type) { added.push_back(std::move(contact)); @@ -683,8 +823,10 @@ void AddressBookListenerCallback(const char* view_uri, void* user_data) { } } -void AddressBookStartListening(const JsonObject&, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookStartListening(const JsonObject&, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + // Set the initial latest version before registering the callback. // The callback should only be registered once so no race can occur. int error_code = @@ -699,19 +841,27 @@ void AddressBookStartListening(const JsonObject&, JsonObject& out) { if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Error while registering listener to contacts db, code: %d", error_code); - throw UnknownException("Error while registering listener to contacts db"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error while registering listener to contacts db"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void AddressBookStopListening(const JsonObject&, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult AddressBookStopListening(const JsonObject&, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + int error_code = contacts_db_remove_changed_cb( _contacts_contact._uri, AddressBookListenerCallback, NULL); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Error while removing listener"); - throw UnknownException("Error while removing listener"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error while removing listener"); } + + return PlatformResult(ErrorCode::NO_ERROR); } } // AddressBook diff --git a/src/contact/addressbook.h b/src/contact/addressbook.h index 6feb3332..90dfcb22 100644 --- a/src/contact/addressbook.h +++ b/src/contact/addressbook.h @@ -20,31 +20,45 @@ #include "common/picojson.h" #include "contact/contact_util.h" #include "functional" +#include "common/platform_result.h" namespace extension { namespace contact { namespace AddressBook { -typedef std::function NativeFunction; +typedef std::function + NativeFunction; -void AddressBookGet(const JsonObject& args, JsonObject& out); -void AddressBookAdd(const JsonObject& args, JsonObject& out); -void AddressBookUpdate(const JsonObject& args, JsonObject& out); -void AddressBookRemove(const JsonObject& args, JsonObject&); -void AddressBookFind(const JsonObject& args, JsonArray& array); -void AddressBookAddGroup(const JsonObject& args, JsonObject& out); -void AddressBookGetGroup(const JsonObject& args, JsonObject& out); -void AddressBookUpdateGroup(const JsonObject& args, JsonObject&); -void AddressBookRemoveGroup(const JsonObject& args, JsonObject&); -void AddressBookGetGroups(const JsonObject& args, JsonArray& out); -void AddressBookStartListening(const JsonObject& args, JsonObject& out); -void AddressBookStopListening(const JsonObject& args, JsonObject& out); +common::PlatformResult AddressBookGet(const JsonObject& args, JsonObject& out); +common::PlatformResult AddressBookAdd(const JsonObject& args, JsonObject& out); +common::PlatformResult AddressBookUpdate(const JsonObject& args, + JsonObject& out); +common::PlatformResult AddressBookRemove(const JsonObject& args, JsonObject&); +common::PlatformResult AddressBookFind(const JsonObject& args, + JsonArray& array); +common::PlatformResult AddressBookAddGroup(const JsonObject& args, + JsonObject& out); +common::PlatformResult AddressBookGetGroup(const JsonObject& args, + JsonObject& out); +common::PlatformResult AddressBookUpdateGroup(const JsonObject& args, + JsonObject&); +common::PlatformResult AddressBookRemoveGroup(const JsonObject& args, + JsonObject&); +common::PlatformResult AddressBookGetGroups(const JsonObject& args, + JsonArray& out); +common::PlatformResult AddressBookStartListening(const JsonObject& args, + JsonObject& out); +common::PlatformResult AddressBookStopListening(const JsonObject& args, + JsonObject& out); -void AddressBookBatchFunc(NativeFunction impl, const char* single_arg_name, - const JsonObject& args, JsonArray& out); +common::PlatformResult AddressBookBatchFunc(NativeFunction impl, + const char* single_arg_name, + const JsonObject& args, + JsonArray& out); // TODO all batch operations should be implemented using CAPI batch functions -void AddressBookAddBatch(const JsonObject& args, JsonArray& out); +common::PlatformResult AddressBookAddBatch(const JsonObject& args, + JsonArray& out); } // AddressBook } // contact diff --git a/src/contact/contact_instance.cc b/src/contact/contact_instance.cc index 2ea6454f..2d2d2cd8 100644 --- a/src/contact/contact_instance.cc +++ b/src/contact/contact_instance.cc @@ -80,16 +80,22 @@ ContactInstance::~ContactInstance() {} void ContactInstance::AddressBookGet(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookGet(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookGet( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookAdd(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookAdd(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookAdd( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookAddBatch(const JsonValue& args, @@ -99,16 +105,14 @@ void ContactInstance::AddressBookAddBatch(const JsonValue& args, const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - AddressBook::AddressBookAddBatch(common::JsonCast(args), - result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + PlatformResult status = AddressBook::AddressBookAddBatch( + common::JsonCast(args), result.get()); + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; auto get_response = [this, callback_id]( @@ -130,24 +134,22 @@ void ContactInstance::AddressBookRemoveBatch(const JsonValue& args, const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - // TODO all batch operations should be implemented using CAPI batch - // functions - AddressBook::AddressBookBatchFunc(AddressBook::AddressBookRemove, "id", - common::JsonCast(args), - result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + // TODO all batch operations should be implemented using CAPI batch + // functions + PlatformResult status = AddressBook::AddressBookBatchFunc( + AddressBook::AddressBookRemove, "id", + common::JsonCast(args), result.get()); + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; - auto get_response = [this, callback_id]( - const std::shared_ptr& response) { + auto get_response = + [this, callback_id](const std::shared_ptr& response) { JsonObject& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); @@ -165,24 +167,22 @@ void ContactInstance::AddressBookUpdateBatch(const JsonValue& args, const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - // TODO all batch operations should be implemented using CAPI batch - // functions - AddressBook::AddressBookBatchFunc( - AddressBook::AddressBookUpdate, "contact", - common::JsonCast(args), result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + // TODO all batch operations should be implemented using CAPI batch + // functions + PlatformResult status = AddressBook::AddressBookBatchFunc( + AddressBook::AddressBookUpdate, "contact", + common::JsonCast(args), result.get()); + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; - auto get_response = [this, callback_id]( - const std::shared_ptr& response) { + auto get_response = + [this, callback_id](const std::shared_ptr& response) { JsonObject& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); @@ -196,33 +196,37 @@ void ContactInstance::AddressBookUpdateBatch(const JsonValue& args, void ContactInstance::AddressBookUpdate(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookUpdate(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookUpdate( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookRemove(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookRemove(common::JsonCast(args), - val.get()); - ReportSuccess(out); + PlatformResult status = AddressBook::AddressBookRemove( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(out); + else + ReportError(status, &out); } void ContactInstance::AddressBookFind(const JsonValue& args, JsonObject& out) { LoggerD("entered"); const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - AddressBook::AddressBookFind(JsonCast(args), - result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + PlatformResult status = AddressBook::AddressBookFind( + JsonCast(args), result.get()); + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; auto get_response = [this, callback_id]( @@ -240,65 +244,77 @@ void ContactInstance::AddressBookFind(const JsonValue& args, JsonObject& out) { void ContactInstance::AddressBookAddGroup(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookAddGroup(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookAddGroup( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookGetGroup(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookGetGroup(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookGetGroup( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookUpdateGroup(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookUpdateGroup(common::JsonCast(args), - val.get()); - ReportSuccess(out); + PlatformResult status = AddressBook::AddressBookUpdateGroup( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(out); + else + ReportError(status, &out); } void ContactInstance::AddressBookRemoveGroup(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookRemoveGroup(common::JsonCast(args), - val.get()); - ReportSuccess(out); + PlatformResult status = AddressBook::AddressBookRemoveGroup( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(out); + else + ReportError(status, &out); } void ContactInstance::AddressBookGetGroups(const JsonValue& args, JsonObject& out) { JsonValue val{JsonArray{}}; - AddressBook::AddressBookGetGroups(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookGetGroups( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerGetAddressBooks(const JsonValue& args, JsonObject& out) { - LoggerD("entered"); - // TODO check privileges const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - ContactManager::ContactManagerGetAddressBooks( - common::JsonCast(args), result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + PlatformResult status = ContactManager::ContactManagerGetAddressBooks( + common::JsonCast(args), result.get()); + + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; - auto get_response = [this, callback_id]( - const std::shared_ptr& response) { + auto get_response = + [this, callback_id](const std::shared_ptr& response) { JsonObject& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); @@ -312,57 +328,78 @@ void ContactInstance::ContactManagerGetAddressBooks(const JsonValue& args, void ContactInstance::ContactManagerGetAddressBook(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerGetAddressBook( + PlatformResult status = ContactManager::ContactManagerGetAddressBook( common::JsonCast(args), val.get()); - ReportSuccess(val, out); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerAddAddressBook(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerAddAddressBook( + PlatformResult status = ContactManager::ContactManagerAddAddressBook( common::JsonCast(args), val.get()); - ReportSuccess(val, out); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerRemoveAddressBook(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerRemoveAddressBook( + PlatformResult status = ContactManager::ContactManagerRemoveAddressBook( common::JsonCast(args), val.get()); - ReportSuccess(val, out); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookStartListening(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookStartListening(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookStartListening( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::AddressBookStopListening(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - AddressBook::AddressBookStopListening(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = AddressBook::AddressBookStopListening( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerGet(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerGet(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = ContactManager::ContactManagerGet( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerUpdate(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerUpdate(common::JsonCast(args), - val.get()); - ReportSuccess(out); + PlatformResult status = ContactManager::ContactManagerUpdate( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerUpdateBatch(const JsonValue& args, @@ -372,24 +409,22 @@ void ContactInstance::ContactManagerUpdateBatch(const JsonValue& args, const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - // TODO all batch operations should be implemented using CAPI batch - // functions - AddressBook::AddressBookBatchFunc( - ContactManager::ContactManagerUpdate, "person", - common::JsonCast(args), result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + // TODO all batch operations should be implemented using CAPI batch + // functions + PlatformResult status = AddressBook::AddressBookBatchFunc( + ContactManager::ContactManagerUpdate, "person", + common::JsonCast(args), result.get()); + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; - auto get_response = [this, callback_id]( - const std::shared_ptr& response) { + auto get_response = + [this, callback_id](const std::shared_ptr& response) { JsonObject& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); @@ -403,9 +438,12 @@ void ContactInstance::ContactManagerUpdateBatch(const JsonValue& args, void ContactInstance::ContactManagerRemove(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerRemove(common::JsonCast(args), - val.get()); - ReportSuccess(out); + PlatformResult status = ContactManager::ContactManagerRemove( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerRemoveBatch(const JsonValue& args, @@ -415,24 +453,22 @@ void ContactInstance::ContactManagerRemoveBatch(const JsonValue& args, const double callback_id = args.get("callbackId").get(); - auto get = [=](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - // TODO all batch operations should be implemented using CAPI batch - // functions - AddressBook::AddressBookBatchFunc( - ContactManager::ContactManagerRemove, "personId", - common::JsonCast(args), result.get()); + auto get = [=](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + // TODO all batch operations should be implemented using CAPI batch + // functions + PlatformResult status = AddressBook::AddressBookBatchFunc( + ContactManager::ContactManagerRemove, "personId", + common::JsonCast(args), result.get()); + if (status.IsSuccess()) ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); - } + else + ReportError(status, &response->get()); }; - auto get_response = [this, callback_id]( - const std::shared_ptr& response) { + auto get_response = + [this, callback_id](const std::shared_ptr& response) { JsonObject& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); @@ -445,24 +481,22 @@ void ContactInstance::ContactManagerRemoveBatch(const JsonValue& args, void ContactInstance::ContactManagerFind(const JsonValue& args, JsonObject& out) { - LoggerD("entered"); - const double callback_id = args.get("callbackId").get(); - auto get = [ this, args ](const std::shared_ptr & response)->void { - try { - JsonValue result = JsonValue(JsonArray()); - ContactManager::ContactManagerFind(common::JsonCast(args), - result.get()); + auto get = [this, args](const std::shared_ptr& response) -> void { + JsonValue result = JsonValue(JsonArray()); + + PlatformResult status = ContactManager::ContactManagerFind( + common::JsonCast(args), result.get()); + if (status.IsSuccess()) { ReportSuccess(result, response->get()); - } - catch (const PlatformException& e) { - ReportError(e, response->get()); + } else { + ReportError(status, &response->get()); } }; - auto get_response = [this, callback_id]( - const std::shared_ptr& response) { + auto get_response = + [this, callback_id](const std::shared_ptr& response) { JsonObject& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); @@ -476,38 +510,54 @@ void ContactInstance::ContactManagerFind(const JsonValue& args, void ContactInstance::ContactManagerImportFromVCard(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerImportFromVCard( + PlatformResult status = ContactManager::ContactManagerImportFromVCard( common::JsonCast(args), val.get()); - ReportSuccess(val, out); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerStartListening(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerStartListening( + PlatformResult status = ContactManager::ContactManagerStartListening( common::JsonCast(args), val.get()); - ReportSuccess(val, out); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::ContactManagerStopListening(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - ContactManager::ContactManagerStopListening( + PlatformResult status = ContactManager::ContactManagerStopListening( common::JsonCast(args), val.get()); - ReportSuccess(val, out); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } void ContactInstance::PersonLink(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - Person::PersonLink(common::JsonCast(args), val.get()); - ReportSuccess(out); + PlatformResult status = Person::PersonLink(common::JsonCast(args), + val.get()); + if (status.IsSuccess()) + ReportSuccess(out); + else + ReportError(status, &out); } void ContactInstance::PersonUnlink(const JsonValue& args, JsonObject& out) { JsonValue val{JsonObject{}}; - Person::PersonUnlink(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + PlatformResult status = Person::PersonUnlink( + common::JsonCast(args), val.get()); + if (status.IsSuccess()) + ReportSuccess(val, out); + else + ReportError(status, &out); } } // namespace contact diff --git a/src/contact/contact_manager.cc b/src/contact/contact_manager.cc index 1fa6ce98..3d52e497 100644 --- a/src/contact/contact_manager.cc +++ b/src/contact/contact_manager.cc @@ -37,10 +37,10 @@ const char* kTokenDelimiter = " ,:"; using namespace common; -void ContactManagerGetAddressBooks(const JsonObject& args, JsonArray& out) { - LoggerD("entered"); - - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerGetAddressBooks(const JsonObject& args, + JsonArray& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; contacts_list_h address_book_list = nullptr; @@ -48,7 +48,8 @@ void ContactManagerGetAddressBooks(const JsonObject& args, JsonArray& out) { 0, &address_book_list); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Fail to get address book list, error: %d", error_code); - throw UnknownException("Fail to get address book list"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Fail to get address book list"); } ContactUtil::ContactsListHPtr contacts_list_ptr( @@ -58,13 +59,15 @@ void ContactManagerGetAddressBooks(const JsonObject& args, JsonArray& out) { error_code = contacts_list_get_count(*contacts_list_ptr, &record_count); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Fail to get address book list count, error: %d", error_code); - throw UnknownException("Fail to get address book list count"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Fail to get address book list count"); } error_code = contacts_list_first(*contacts_list_ptr); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Fail to get address book from list, error: %d", error_code); - throw UnknownException("Fail to get address book from list"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Fail to get address book from list"); } for (unsigned int i = 0; i < record_count; i++) { @@ -81,14 +84,21 @@ void ContactManagerGetAddressBooks(const JsonObject& args, JsonArray& out) { int account_id = 0; int mode = 0; char* name = nullptr; - ContactUtil::GetIntFromRecord(contacts_record, _contacts_address_book.id, - &id); - ContactUtil::GetIntFromRecord( + status = ContactUtil::GetIntFromRecord( + contacts_record, _contacts_address_book.id, &id); + if (status.IsError()) return status; + + status = ContactUtil::GetIntFromRecord( contacts_record, _contacts_address_book.account_id, &account_id); - ContactUtil::GetIntFromRecord(contacts_record, _contacts_address_book.mode, - &mode); - ContactUtil::GetStrFromRecord(contacts_record, _contacts_address_book.name, - &name); + if (status.IsError()) return status; + + status = ContactUtil::GetIntFromRecord(contacts_record, + _contacts_address_book.mode, &mode); + if (status.IsError()) return status; + + status = ContactUtil::GetStrFromRecord(contacts_record, + _contacts_address_book.name, &name); + if (status.IsError()) return status; JsonValue single = JsonValue(JsonObject()); JsonObject& single_obj = single.get(); @@ -102,17 +112,16 @@ void ContactManagerGetAddressBooks(const JsonObject& args, JsonArray& out) { contacts_list_next(*contacts_list_ptr); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerGetAddressBook(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); - long address_book_id; - try { - address_book_id = common::stol(FromJson(args, "addressBookId")); - } - catch (const common::InvalidValuesException&) { - throw common::NotFoundException("Invalid id"); - } +PlatformResult ContactManagerGetAddressBook(const JsonObject& args, + JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + + long address_book_id = common::stol(FromJson(args, "addressBookId")); contacts_record_h contacts_record; int error_code = contacts_db_get_record(_contacts_address_book._uri, @@ -120,56 +129,68 @@ void ContactManagerGetAddressBook(const JsonObject& args, JsonObject& out) { &contacts_record); if (CONTACTS_ERROR_NONE != error_code || nullptr == contacts_record) { LoggerE("Fail to get addressbook record, error code: %d", error_code); - throw NotFoundException("Fail to get address book with given id"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Fail to get address book with given id"); } ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); int account_id; - ContactUtil::GetIntFromRecord(contacts_record, - _contacts_address_book.account_id, &account_id); + status = ContactUtil::GetIntFromRecord( + contacts_record, _contacts_address_book.account_id, &account_id); + if (status.IsError()) return status; int mode; - ContactUtil::GetIntFromRecord(contacts_record, _contacts_address_book.mode, - &mode); + status = ContactUtil::GetIntFromRecord(contacts_record, + _contacts_address_book.mode, &mode); + if (status.IsError()) return status; char* name; - ContactUtil::GetStrFromRecord(contacts_record, _contacts_address_book.name, - &name); + status = ContactUtil::GetStrFromRecord(contacts_record, + _contacts_address_book.name, &name); + if (status.IsError()) return status; out.insert(std::make_pair("accountId", static_cast(account_id))); out.insert(std::make_pair("name", std::string(name))); out.insert(std::make_pair("readOnly", (CONTACTS_ADDRESS_BOOK_MODE_READONLY == mode))); + + return PlatformResult(ErrorCode::NO_ERROR); } namespace { -void ContactManagerGetInternal(int person_id, JsonObject* out) { +PlatformResult ContactManagerGetInternal(int person_id, JsonObject* out) { contacts_record_h contacts_record = nullptr; int error_code = contacts_db_get_record(_contacts_person._uri, person_id, &contacts_record); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Person with id: %d, not found, error: %d", person_id, error_code); - throw NotFoundException("Person not found"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Person not found"); } ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); - ContactUtil::ImportPersonFromContactsRecord(contacts_record, out); + PlatformResult status = + ContactUtil::ImportPersonFromContactsRecord(contacts_record, out); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } } -void ContactManagerAddAddressBook(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerAddAddressBook(const JsonObject& args, + JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; const JsonObject& addressBook = FromJson(args, "addressBook"); if (!IsNull(addressBook, "id")) { LoggerW("AddressBook already exists"); - throw common::UnknownException("AddressBook already exists"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "AddressBook already exists"); } contacts_record_h contacts_record; @@ -177,45 +198,50 @@ void ContactManagerAddAddressBook(const JsonObject& args, JsonObject& out) { contacts_record_create(_contacts_address_book._uri, &contacts_record); if (CONTACTS_ERROR_NONE != ret) { LoggerE("Failed to create address book record, error code : %d", ret); - throw UnknownException("Failed to create address book record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Failed to create address book record"); } ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); - ContactUtil::SetStrInRecord( + status = ContactUtil::SetStrInRecord( contacts_record, _contacts_address_book.name, FromJson(addressBook, "name").c_str()); + if (status.IsError()) return status; contacts_address_book_mode_e mode = FromJson(addressBook, "readOnly") ? CONTACTS_ADDRESS_BOOK_MODE_READONLY : CONTACTS_ADDRESS_BOOK_MODE_NONE; - ContactUtil::SetIntInRecord(contacts_record, _contacts_address_book.mode, - static_cast(mode)); + status = ContactUtil::SetIntInRecord( + contacts_record, _contacts_address_book.mode, static_cast(mode)); + if (status.IsError()) return status; double account_id = FromJson(addressBook, "accountId"); - ContactUtil::SetIntInRecord(contacts_record, - _contacts_address_book.account_id, - static_cast(account_id)); + status = ContactUtil::SetIntInRecord(contacts_record, + _contacts_address_book.account_id, + static_cast(account_id)); + if (status.IsError()) return status; int address_book_id; ret = contacts_db_insert_record(*contacts_record_ptr, &address_book_id); if (CONTACTS_ERROR_NONE != ret) { LoggerE("Failed to insert address book record, error code: %d", ret); - throw UnknownException("Failed to insert address book record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Failed to insert address book record"); } out.insert(std::make_pair("id", std::to_string(address_book_id))); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerRemoveAddressBook(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); - long address_book_id; - try { - address_book_id = common::stol(FromJson(args, "addressBookId")); - } - catch (const common::InvalidValuesException&) { - throw common::NotFoundException("Invalid id"); - } +PlatformResult ContactManagerRemoveAddressBook(const JsonObject& args, + JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + + long address_book_id = + common::stol(FromJson(args, "addressBookId")); contacts_record_h contacts_record; int error_code = contacts_db_get_record(_contacts_address_book._uri, @@ -223,73 +249,90 @@ void ContactManagerRemoveAddressBook(const JsonObject& args, JsonObject& out) { &contacts_record); if (CONTACTS_ERROR_NONE != error_code || nullptr == contacts_record) { LoggerE("Fail to get addressbook record, error code: %d", error_code); - throw NotFoundException("Fail to get address book with given id"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Fail to get address book with given id"); } int ret = contacts_db_delete_record(_contacts_address_book._uri, static_cast(address_book_id)); if (CONTACTS_ERROR_NONE != ret) { LOGE("Failed to delete address book record, error code : %d", ret); - throw UnknownException("Failed to delete address book record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Failed to delete address book record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerGet(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerGet(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + long person_id = common::stol(FromJson(args, "personId")); - ContactManagerGetInternal(person_id, &out); + return ContactManagerGetInternal(person_id, &out); } -void ContactManagerUpdate(const JsonObject& args, JsonObject&) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerUpdate(const JsonObject& args, JsonObject&) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + const JsonObject& person = FromJson(args, "person"); long person_id = common::stol(FromJson(person, "id")); - contacts_record_h contacts_record = nullptr; - int error_code = contacts_db_get_record(_contacts_person._uri, person_id, &contacts_record); if (CONTACTS_ERROR_NONE != error_code) { - throw NotFoundException("Person not found"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Person not found"); } - ContactUtil::ExportPersonToContactsRecord(contacts_record, person); + status = ContactUtil::ExportPersonToContactsRecord(contacts_record, person); + if (status.IsError()) return status; + ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); error_code = contacts_db_update_record(*contacts_record_ptr); - if (CONTACTS_ERROR_NONE != error_code) { LoggerE("error code: %d", error_code); - throw UnknownException( - "Error during executing contacts_db_update_record()"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during executing contacts_db_update_record()"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerRemove(const JsonObject& args, JsonObject&) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerRemove(const JsonObject& args, JsonObject&) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + long person_id = common::stol(FromJson(args, "personId")); if (person_id < 0) { - throw common::InvalidValuesException("Negative person id"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Negative person id"); } int error_code = contacts_db_delete_record(_contacts_person._uri, person_id); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("Error during removing contact, error: %d", error_code); - throw NotFoundException("Error during removing contact"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, + "Error during removing contact"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerFind(const JsonObject& args, JsonArray& out) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; contacts_query_h contacts_query = nullptr; int error_code = contacts_query_create(_contacts_person._uri, &contacts_query); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + if (status.IsError()) return status; ContactUtil::ContactsQueryHPtr contacts_query_ptr( &contacts_query, ContactUtil::ContactsQueryDeleter); @@ -297,19 +340,23 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { // Add filter to query std::vector> intermediate_filters( 1); + if (!IsNull(args, "filter")) { FilterVisitor visitor; visitor.SetOnAttributeFilter([&](const std::string& name, AttributeMatchFlag match_flag, const JsonValue& match_value) { - - const Person::PersonProperty& property = - Person::PersonPropertyFromString(name); + Person::PersonProperty property; + status = Person::PersonPropertyFromString(name, &property); + if (status.IsError()) return status; contacts_filter_h contacts_filter = nullptr; int error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + status = ContactUtil::ErrorChecker(error_code, + "Failed contacts_query_set_filter"); + if (status.IsError()) return status; + ContactUtil::ContactsFilterPtr contacts_filter_ptr( contacts_filter, ContactUtil::ContactsFilterDeleter); @@ -320,8 +367,9 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { } error_code = contacts_filter_add_bool(contacts_filter, property.propertyId, value); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); + if (status.IsError()) return status; } else if (property.type == kPrimitiveTypeString) { std::string value = JsonCast(match_value); @@ -342,7 +390,9 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { } error_code = contacts_filter_add_str( contacts_filter, property.propertyId, flag, value.c_str()); - ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + status = ContactUtil::ErrorChecker(error_code, + "Failed contacts_filter_add_str"); + if (status.IsError()) return status; } else if (property.type == kPrimitiveTypeLong || property.type == kPrimitiveTypeId) { int value; @@ -352,7 +402,8 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { value = common::stol(JsonCast(match_value)); } if (value < 0) { - throw InvalidValuesException("Match value cannot be less than 0"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Match value cannot be less than 0"); } contacts_match_int_flag_e flag; if (AttributeMatchFlag::kExists == match_flag) { @@ -369,9 +420,13 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { error_code = contacts_filter_add_int(contacts_filter, property.propertyId, flag, value); - ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + status = ContactUtil::ErrorChecker(error_code, + "Failed contacts_filter_add_str"); + if (status.IsError()) return status; } else { - throw UnknownException("Invalid primitive type!"); + LoggerE("Invalid primitive type!"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Invalid primitive type!"); } intermediate_filters[intermediate_filters.size() - 1] .push_back(std::move(contacts_filter_ptr)); @@ -382,14 +437,17 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { visitor.SetOnAttributeRangeFilter([&](const std::string& name, const JsonValue& initial_value, const JsonValue& end_value) { - - const Person::PersonProperty& property = - Person::PersonPropertyFromString(name); + Person::PersonProperty property; + status = Person::PersonPropertyFromString(name, &property); + if (status.IsError()) return status; contacts_filter_h contacts_filter = nullptr; int error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + status = ContactUtil::ErrorChecker(error_code, + "Failed contacts_query_set_filter"); + if (status.IsError()) return status; + ContactUtil::ContactsFilterPtr contacts_filter_ptr( contacts_filter, ContactUtil::ContactsFilterDeleter); @@ -411,22 +469,25 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { if (initial_value_bool == end_value_bool) { error_code = contacts_filter_add_bool( contacts_filter, property.propertyId, initial_value_bool); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); + if (status.IsError()) return status; } } else if (initial_value_exists) { if (initial_value_bool) { error_code = contacts_filter_add_bool(contacts_filter, property.propertyId, true); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); + if (status.IsError()) return status; } } else if (end_value_exists) { if (!end_value_bool) { error_code = contacts_filter_add_bool(contacts_filter, property.propertyId, false); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); + if (status.IsError()) return status; } } } else if (property.type == kPrimitiveTypeString) { @@ -446,8 +507,9 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; ContactUtil::ContactsFilterPtr sub_filter_ptr( sub_filter, ContactUtil::ContactsFilterDeleter); @@ -455,37 +517,45 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { error_code = contacts_filter_add_str(sub_filter, property.propertyId, CONTACTS_MATCH_STARTSWITH, initial_value_str.c_str()); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; + error_code = contacts_filter_add_operator( sub_filter, CONTACTS_FILTER_OPERATOR_AND); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; error_code = contacts_filter_add_str(sub_filter, property.propertyId, CONTACTS_MATCH_ENDSWITH, end_value_str.c_str()); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; error_code = contacts_filter_add_filter(contacts_filter, sub_filter); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; } else if (initial_value_exists) { error_code = contacts_filter_add_str( contacts_filter, property.propertyId, CONTACTS_MATCH_STARTSWITH, initial_value_str.c_str()); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; } else if (end_value_exists) { error_code = contacts_filter_add_str( contacts_filter, property.propertyId, CONTACTS_MATCH_ENDSWITH, end_value_str.c_str()); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); + if (status.IsError()) return status; } } else if (property.type == kPrimitiveTypeLong || property.type == kPrimitiveTypeId) { + int initial_value_int = 0; int end_value_int = 0; @@ -512,46 +582,55 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); + if (status.IsError()) return status; + ContactUtil::ContactsFilterPtr sub_filter_ptr( sub_filter, ContactUtil::ContactsFilterDeleter); error_code = contacts_filter_add_int( sub_filter, property.propertyId, CONTACTS_MATCH_GREATER_THAN_OR_EQUAL, initial_value_int); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + if (status.IsError()) return status; error_code = contacts_filter_add_operator( sub_filter, CONTACTS_FILTER_OPERATOR_AND); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_operator"); + if (status.IsError()) return status; error_code = contacts_filter_add_int( sub_filter, property.propertyId, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, end_value_int); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + if (status.IsError()) return status; error_code = contacts_filter_add_filter(contacts_filter, sub_filter); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_filter"); + if (status.IsError()) return status; } else if (initial_value_exists) { error_code = contacts_filter_add_int( contacts_filter, property.propertyId, CONTACTS_MATCH_GREATER_THAN_OR_EQUAL, initial_value_int); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + if (status.IsError()) return status; } else if (end_value_exists) { error_code = contacts_filter_add_int( contacts_filter, property.propertyId, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, end_value_int); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + if (status.IsError()) return status; } } else { - throw UnknownException("Invalid primitive type!"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Invalid primitive type!"); } intermediate_filters[intermediate_filters.size() - 1] .push_back(std::move(contacts_filter_ptr)); @@ -568,33 +647,42 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { visitor.SetOnCompositeFilterEnd([&](CompositeFilterType type) { if (intermediate_filters.size() == 0) { - throw UnknownException("Reached stack size equal to 0!"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Reached stack size equal to 0!"); } contacts_filter_h merged_filter = nullptr; int error_code = contacts_filter_create(_contacts_person._uri, &merged_filter); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + status = ContactUtil::ErrorChecker(error_code, + "Failed contacts_query_set_filter"); + if (status.IsError()) return status; + ContactUtil::ContactsFilterPtr merged_filter_ptr( merged_filter, ContactUtil::ContactsFilterDeleter); for (std::size_t i = 0; i < intermediate_filters.back().size(); ++i) { error_code = contacts_filter_add_filter( merged_filter, intermediate_filters.back().at(i).get()); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + if (status.IsError()) return status; + if (CompositeFilterType::kIntersection == type) { error_code = contacts_filter_add_operator( merged_filter, CONTACTS_FILTER_OPERATOR_AND); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + if (status.IsError()) return status; } else if (CompositeFilterType::kUnion == type) { error_code = contacts_filter_add_operator( merged_filter, CONTACTS_FILTER_OPERATOR_OR); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + if (status.IsError()) return status; } else { - throw InvalidValuesException("Invalid union type!"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Invalid union type!"); } } @@ -604,32 +692,39 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { return PlatformResult(ErrorCode::NO_ERROR); }); - visitor.Visit(FromJson(args, "filter")); + status = visitor.Visit(FromJson(args, "filter")); + if (status.IsError()) return status; + // Should compute only one filter always. if ((intermediate_filters.size() != 1) || (intermediate_filters[0].size() != 1)) { LoggerE("Bad filter evaluation!"); - throw UnknownException("Bad filter evaluation!"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Bad filter evaluation!"); } // Filter is generated error_code = contacts_query_set_filter(contacts_query, intermediate_filters[0][0].get()); - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + status = ContactUtil::ErrorChecker(error_code, + "Failed contacts_query_set_filter"); + if (status.IsError()) return status; } contacts_list_h person_list = nullptr; error_code = contacts_db_get_records_with_query(contacts_query, 0, 0, &person_list); - ContactUtil::ErrorChecker(error_code, + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_db_get_records_with_query"); + if (status.IsError()) return status; ContactUtil::ContactsListHPtr person_list_ptr( &person_list, ContactUtil::ContactsListDeleter); int record_count = 0; error_code = contacts_list_get_count(person_list, &record_count); - ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); + if (status.IsError()) return status; contacts_list_first(person_list); @@ -646,17 +741,24 @@ void ContactManagerFind(const JsonObject& args, JsonArray& out) { error_code = contacts_record_get_int(contacts_record, _contacts_person.id, &id_value); - ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); + status = + ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); + if (status.IsError()) return status; out.push_back(JsonValue(static_cast(id_value))); contacts_list_next(person_list); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out) { +PlatformResult ContactManagerImportFromVCard(const JsonObject& args, + JsonObject& out) { // I'm not sure how to call it. Should it be 'Contact', 'vCard' or what? - ContactUtil::CheckDBConnection(); + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + const char* vcard_char_ptr = FromJson(args, "contact").c_str(); contacts_list_h contacts_list = nullptr; @@ -665,10 +767,11 @@ void ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out) { err = contacts_vcard_parse_to_contacts(vcard_char_ptr, &contacts_list); if (CONTACTS_ERROR_INVALID_PARAMETER == err) { LoggerE("Invalid vCard string"); - throw UnknownException("Invalid vCard string"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Invalid vCard string"); } else if (CONTACTS_ERROR_NONE != err) { LoggerE("Fail to convert vCard from string"); - throw UnknownException("Fail to convert vCard from string"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Fail to convert vCard from string"); } int record_count = 0; @@ -676,7 +779,7 @@ void ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out) { if (CONTACTS_ERROR_NONE != err || 0 == record_count) { contacts_list_destroy(contacts_list, true); LoggerE("Invalid vCard string."); - throw UnknownException("Invalid vCard string."); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Invalid vCard string."); } contacts_record_h contacts_record; @@ -685,10 +788,13 @@ void ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out) { if (CONTACTS_ERROR_NONE != err || nullptr == contacts_record) { contacts_list_destroy(contacts_list, true); LoggerE("Invalid vCard string."); - throw UnknownException("Invalid vCard string."); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Invalid vCard string."); } - ContactUtil::ImportContactFromContactsRecord(contacts_record, &out); + status = ContactUtil::ImportContactFromContactsRecord(contacts_record, &out); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } namespace { @@ -730,42 +836,48 @@ void ContactManagerListenerCallback(const char* view_uri, char* changes, char* token = strtok(tmp.get(), kTokenDelimiter); while (token) { - try { + if (IsNumeric(token)) { + int type = atoi(token); + token = strtok(nullptr, kTokenDelimiter); + if (!token) { + break; + } if (IsNumeric(token)) { - int type = atoi(token); - token = strtok(nullptr, kTokenDelimiter); - if (!token) { - break; - } - if (IsNumeric(token)) { - int person_id = atoi(token); - switch (type) { - case CONTACTS_CHANGE_INSERTED: { - added.push_back(JsonValue{JsonObject{}}); - ContactManagerGetInternal(person_id, - &added.back().get()); - break; - } - case CONTACTS_CHANGE_UPDATED: { - updated.push_back(JsonValue{JsonObject{}}); - ContactManagerGetInternal(person_id, - &updated.back().get()); - break; + int person_id = atoi(token); + switch (type) { + case CONTACTS_CHANGE_INSERTED: { + added.push_back(JsonValue{JsonObject{}}); + PlatformResult status = ContactManagerGetInternal( + person_id, &added.back().get()); + if (status.IsError()) { + LoggerE("Caught exception in listener callback: %s", + status.message().c_str()); + return; } - case CONTACTS_CHANGE_DELETED: { - std::string id_str{std::to_string(person_id)}; - removed.push_back(JsonValue{id_str.c_str()}); - break; + + break; + } + case CONTACTS_CHANGE_UPDATED: { + updated.push_back(JsonValue{JsonObject{}}); + PlatformResult status = ContactManagerGetInternal( + person_id, &updated.back().get()); + if (status.IsError()) { + LoggerE("Caught exception in listener callback: %s", + status.message().c_str()); + return; } - default: {} + + break; } + case CONTACTS_CHANGE_DELETED: { + std::string id_str{std::to_string(person_id)}; + removed.push_back(JsonValue{id_str.c_str()}); + break; + } + default: {} } } } - catch (common::PlatformException& ex) { - LoggerE("Caught exception %s\" in listener callback: %s", - ex.name().c_str(), ex.message().c_str()); - } token = strtok(nullptr, kTokenDelimiter); } @@ -774,30 +886,38 @@ void ContactManagerListenerCallback(const char* view_uri, char* changes, } } -void ContactManagerStartListening(const JsonObject& /*args*/, - JsonObject& /*out*/) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerStartListening(const JsonObject& /*args*/, + JsonObject& /*out*/) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + int error_code = contacts_db_add_changed_cb_with_info( _contacts_person._uri, ContactManagerListenerCallback, nullptr); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("contacts_db_add_changed_cb(_contacts_person._uri) error: %d", error_code); - throw UnknownException("Failed to start listening"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to start listening"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ContactManagerStopListening(const JsonObject& /*args*/, - JsonObject& /*out*/) { - ContactUtil::CheckDBConnection(); +PlatformResult ContactManagerStopListening(const JsonObject& /*args*/, + JsonObject& /*out*/) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; + int error_code = contacts_db_remove_changed_cb_with_info( _contacts_person._uri, ContactManagerListenerCallback, nullptr); if (CONTACTS_ERROR_NONE != error_code) { LoggerE("contacts_db_remove_changed_cb(_contacts_person._uri) error: %d", error_code); - throw UnknownException("Failed to stop listening"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to stop listening"); } + + return PlatformResult(ErrorCode::NO_ERROR); } } // namespace ContactManager diff --git a/src/contact/contact_manager.h b/src/contact/contact_manager.h index a7809b7c..69f35a80 100644 --- a/src/contact/contact_manager.h +++ b/src/contact/contact_manager.h @@ -24,27 +24,32 @@ namespace extension { namespace contact { namespace ContactManager { -void ContactManagerGetAddressBooks(const JsonObject& args, JsonArray& out); +common::PlatformResult ContactManagerGetAddressBooks(const JsonObject& args, + JsonArray& out); -void ContactManagerGetAddressBook(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerGetAddressBook(const JsonObject& args, + JsonObject& out); -void ContactManagerAddAddressBook(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerAddAddressBook(const JsonObject& args, + JsonObject& out); -void ContactManagerRemoveAddressBook(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerRemoveAddressBook(const JsonObject& args, + JsonObject& out); -void ContactManagerGet(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerGet(const JsonObject& args, + JsonObject& out); -void ContactManagerUpdate(const JsonObject& args, JsonObject&); +common::PlatformResult ContactManagerUpdate(const JsonObject& args, + JsonObject&); -void ContactManagerUpdateBatch(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerRemove(const JsonObject& args, + JsonObject&); -void ContactManagerRemove(const JsonObject& args, JsonObject&); +common::PlatformResult ContactManagerFind(const JsonObject& args, + JsonArray& out); -void ContactManagerRemoveBatch(const JsonObject& args, JsonObject& out); - -void ContactManagerFind(const JsonObject& args, JsonArray& out); - -void ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerImportFromVCard(const JsonObject& args, + JsonObject& out); /** * Signature: @code void getAddressBook(contactString); @endcode @@ -57,7 +62,8 @@ void ContactManagerImportFromVCard(const JsonObject& args, JsonObject& out); * {status: 'success'} * @endcode */ -void ContactManagerStartListening(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerStartListening(const JsonObject& args, + JsonObject& out); /** * Signature: @code void getAddressBook(contactString); @endcode @@ -70,7 +76,8 @@ void ContactManagerStartListening(const JsonObject& args, JsonObject& out); * {status: 'success'} * @endcode */ -void ContactManagerStopListening(const JsonObject& args, JsonObject& out); +common::PlatformResult ContactManagerStopListening(const JsonObject& args, + JsonObject& out); } // namespace ContactManager } // namespace contact diff --git a/src/contact/contact_util.cc b/src/contact/contact_util.cc index c0781e49..54409fc6 100644 --- a/src/contact/contact_util.cc +++ b/src/contact/contact_util.cc @@ -135,188 +135,171 @@ static const char kContactInstantMessageTypeIrc[] = "IRC"; static const char kContactInstantMessageTypeCustom[] = "CUSTOM"; } -void ErrorChecker(int err, const char* message) { +PlatformResult ErrorChecker(int err, const char* message) { if (CONTACTS_ERROR_NONE != err) { LoggerE("%s, error code: %i", message, err); - throw common::UnknownException(message); + return PlatformResult(ErrorCode::UNKNOWN_ERR, message); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void GetStrFromRecord(contacts_record_h record, unsigned int property_id, - char** value) { +PlatformResult GetStrFromRecord(contacts_record_h record, + unsigned int property_id, char** value) { int err = contacts_record_get_str_p(record, property_id, value); if (CONTACTS_ERROR_NONE != err) { LoggerE("Error during getting contact record, error code: %i", err); - throw common::UnknownException("Error during getting contact record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during getting contact record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void GetIntFromRecord(contacts_record_h record, unsigned int property_id, - int* value) { +PlatformResult GetIntFromRecord(contacts_record_h record, + unsigned int property_id, int* value) { int err = contacts_record_get_int(record, property_id, value); if (CONTACTS_ERROR_NONE != err) { LoggerE("Error during getting contact record, error code: %i", err); - throw common::UnknownException("Error during getting contact record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during getting contact record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void GetBoolFromRecord(contacts_record_h record, unsigned int property_id, - bool* value) { +PlatformResult GetBoolFromRecord(contacts_record_h record, + unsigned int property_id, bool* value) { int err = contacts_record_get_bool(record, property_id, value); if (CONTACTS_ERROR_NONE != err) { LoggerE("Error during getting contact record, error code: %i", err); - throw common::UnknownException("Error during getting contact record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during getting contact record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void SetStrInRecord(contacts_record_h record, unsigned int property_id, - const char* value) { +PlatformResult SetStrInRecord(contacts_record_h record, + unsigned int property_id, const char* value) { int err = contacts_record_set_str(record, property_id, value); if (CONTACTS_ERROR_NONE != err) { - LoggerE("Error during getting contact record, error code: %i", err); - throw common::UnknownException("Error during setting contact record"); + LoggerE("Error during setting str contact record property, error code: %i", + err); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during setting contact record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void SetIntInRecord(contacts_record_h record, unsigned int property_id, - int value) { +PlatformResult SetIntInRecord(contacts_record_h record, + unsigned int property_id, int value) { int err = contacts_record_set_int(record, property_id, value); if (CONTACTS_ERROR_NONE != err) { LoggerE("Error during getting contact record, error code: %i", err); - throw common::UnknownException("Error during setting contact record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during setting contact record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void SetBoolInRecord(contacts_record_h record, unsigned int property_id, - bool value) { +PlatformResult SetBoolInRecord(contacts_record_h record, + unsigned int property_id, bool value) { int err = contacts_record_set_bool(record, property_id, value); if (CONTACTS_ERROR_NONE != err) { LoggerE("Error during getting contact record, error code: %i", err); - throw common::UnknownException("Error during setting contact record"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Error during setting contact record"); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ClearAllContactRecord(contacts_record_h contacts_record, - unsigned int property_id) { +PlatformResult ClearAllContactRecord(contacts_record_h contacts_record, + unsigned int property_id) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } - unsigned int record_count = - GetNumberOfChildRecord(contacts_record, property_id); + int record_count; + PlatformResult status = + GetNumberOfChildRecord(contacts_record, property_id, &record_count); + if (status.IsError()) return status; for (unsigned int i = 0; i < record_count; ++i) { unsigned int actual_index = record_count - 1 - i; contacts_record_h phone_record = nullptr; int err = contacts_record_get_child_record_at_p( contacts_record, property_id, actual_index, &phone_record); - ContactUtil::ErrorChecker(err, "Error during getting phone record"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Error during getting phone record"); + if (status.IsError()) return status; err = contacts_record_remove_child_record(contacts_record, property_id, phone_record); - ContactUtil::ErrorChecker(err, "Error during getting phone record"); + status = + ContactUtil::ErrorChecker(err, "Error during getting phone record"); + if (status.IsError()) return status; } -} -unsigned int GetNumberOfChildRecord(contacts_record_h contacts_record, - unsigned int property_id) { - int err = CONTACTS_ERROR_NONE; - int child_count = 0; - err = contacts_record_get_child_record_count(contacts_record, property_id, - &child_count); - if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - throw common::UnknownException("Problem during getting child count"); - } - - return child_count; + return PlatformResult(ErrorCode::NO_ERROR); } -JsonValue ImportBirthdayFromContactsRecord(contacts_record_h contacts_record, - unsigned int index) { - // contacts_record is protected by unique_ptr and its ownership is not passed - // here - if (!contacts_record) { - LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); - } +PlatformResult GetNumberOfChildRecord(contacts_record_h contacts_record, + unsigned int property_id, + int* child_count) { + assert(child_count); int err = CONTACTS_ERROR_NONE; - contacts_record_h child_record = nullptr; - err = contacts_record_get_child_record_at_p( - contacts_record, _contacts_contact.event, index, &child_record); + err = contacts_record_get_child_record_count(contacts_record, property_id, + child_count); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return {}; + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Problem during getting child count"); } - int value = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_event.type, &value); - - if (CONTACTS_EVENT_TYPE_BIRTH == value) { - int date = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_event.date, &date); - return JsonValue{static_cast(date)}; - } - return {}; -} - -void ExportBirthdayToContactsRecord(contacts_record_h contacts_record, - int date) { - // contacts_record is protected by unique_ptr and its ownership is not passed - // here - if (!contacts_record) { - LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); - } - - int err = CONTACTS_ERROR_NONE; - contacts_record_h birthday_record = nullptr; - err = contacts_record_create(_contacts_event._uri, &birthday_record); - ContactUtil::ErrorChecker(err, - "Failed to create birthday record in database"); - ContactsRecordHPtr record(&birthday_record, ContactsDeleter); - - ContactUtil::SetIntInRecord(birthday_record, _contacts_event.type, - CONTACTS_EVENT_TYPE_BIRTH); - - ContactUtil::SetIntInRecord(birthday_record, _contacts_event.date, date); - - err = contacts_record_add_child_record( - contacts_record, _contacts_contact.event, birthday_record); - ContactUtil::ErrorChecker(err, "Fail to save birthday record in database"); - // Do not delete record, it is passed to the platform - record.release(); + return PlatformResult(ErrorCode::NO_ERROR); } -bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, - JsonObject* out_ptr) { +PlatformResult ImportContactNameFromContactsRecord( + contacts_record_h contacts_record, JsonObject* out_ptr, + bool* is_contact_name) { JsonObject& out = *out_ptr; if (!contacts_record) { LoggerW("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int count = 0; int err = CONTACTS_ERROR_NONE; err = contacts_record_get_child_record_count(contacts_record, _contacts_contact.name, &count); - ContactUtil::ErrorChecker(err, "Contacts child record get count error"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Contacts child record get count error"); + if (status.IsError()) return status; if (count > 1) { LoggerE("More than one ContactName for one Contact"); - throw common::UnknownException("More than one ContactName for one Contact"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "More than one ContactName for one Contact"); } LoggerD("Contact name record count: %i", count); if (count == 0) { - return false; + *is_contact_name = false; + return PlatformResult(ErrorCode::NO_ERROR); } contacts_record_h contact_name = nullptr; err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.name, 0, &contact_name); - ContactUtil::ErrorChecker(err, "Contacts name record get childerror"); + status = + ContactUtil::ErrorChecker(err, "Contacts name record get childerror"); + if (status.IsError()) return status; // Documentation says: // child_record MUST NOT be released by you. @@ -324,55 +307,67 @@ bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, // so it won't be protected by unique_ptr. char* char_value = nullptr; - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.prefix, - &char_value); + status = ContactUtil::GetStrFromRecord(contact_name, _contacts_name.prefix, + &char_value); + if (status.IsError()) return status; + out.insert(std::make_pair("prefix", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.suffix, - &char_value); + status = ContactUtil::GetStrFromRecord(contact_name, _contacts_name.suffix, + &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("suffix", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.first, - &char_value); + status = ContactUtil::GetStrFromRecord(contact_name, _contacts_name.first, + &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("firstName", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.addition, - &char_value); + status = ContactUtil::GetStrFromRecord(contact_name, _contacts_name.addition, + &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("middleName", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.last, &char_value); + status = ContactUtil::GetStrFromRecord(contact_name, _contacts_name.last, + &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("lastName", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.phonetic_first, - &char_value); + status = ContactUtil::GetStrFromRecord( + contact_name, _contacts_name.phonetic_first, &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("phoneticFirstName", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.phonetic_middle, - &char_value); + status = ContactUtil::GetStrFromRecord( + contact_name, _contacts_name.phonetic_middle, &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("phoneticMiddleName", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(contact_name, _contacts_name.phonetic_last, - &char_value); + status = ContactUtil::GetStrFromRecord( + contact_name, _contacts_name.phonetic_last, &char_value); + if (status.IsError()) return status; out.insert(std::make_pair("phoneticLastName", char_value ? JsonValue{char_value} : JsonValue{})); err = contacts_record_get_child_record_count( contacts_record, _contacts_contact.nickname, &count); - ContactUtil::ErrorChecker(err, "Contacts child record get count error"); + status = + ContactUtil::ErrorChecker(err, "Contacts child record get count error"); + if (status.IsError()) return status; JsonArray& nicknames = out.insert(std::make_pair("nicknames", JsonArray())) .first->second.get(); @@ -380,25 +375,30 @@ bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, contacts_record_h nickname = nullptr; err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.nickname, i, &nickname); - ContactUtil::ErrorChecker(err, "Contacts nicknames record get child error"); - ContactUtil::GetStrFromRecord(nickname, _contacts_nickname.name, - &char_value); + status = ContactUtil::ErrorChecker( + err, "Contacts nicknames record get child error"); + if (status.IsError()) return status; + + status = ContactUtil::GetStrFromRecord(nickname, _contacts_nickname.name, + &char_value); + if (status.IsError()) return status; if (char_value) { nicknames.push_back(JsonValue{char_value}); } } - return true; + *is_contact_name = true; + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactNameToContactsRecord(contacts_record_h contacts_record, - const JsonObject& in) { +PlatformResult ExportContactNameToContactsRecord( + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerW("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -408,46 +408,62 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, bool update = true; if (CONTACTS_ERROR_NONE != err && nullptr == contact_name) { err = contacts_record_create(_contacts_name._uri, &contact_name); - ContactUtil::ErrorChecker(err, "Contacts record create error"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Contacts record create error"); + if (status.IsError()) return status; + update = false; } // contact_name starts to be protected by unique_ptr ContactsRecordHPtr contacts_name_ptr(&contact_name, ContactsDeleter); if (!IsNull(in, "prefix")) { - ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.prefix, - FromJson(in, "prefix").c_str()); + PlatformResult status = + ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.prefix, + FromJson(in, "prefix").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "suffix")) { - ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.suffix, - FromJson(in, "suffix").c_str()); + PlatformResult status = + ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.suffix, + FromJson(in, "suffix").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "firstName")) { - ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.first, - FromJson(in, "firstName").c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + *contacts_name_ptr, _contacts_name.first, + FromJson(in, "firstName").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "middleName")) { - ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.addition, - FromJson(in, "middleName").c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + *contacts_name_ptr, _contacts_name.addition, + FromJson(in, "middleName").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "lastName")) { - ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.last, - FromJson(in, "lastName").c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + *contacts_name_ptr, _contacts_name.last, + FromJson(in, "lastName").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "phoneticFirstName")) { - ContactUtil::SetStrInRecord( + PlatformResult status = ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.phonetic_first, FromJson(in, "phoneticFirstName").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "phoneticMiddleName")) { - ContactUtil::SetStrInRecord( + PlatformResult status = ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.phonetic_middle, FromJson(in, "phoneticMiddleName").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "phoneticLastName")) { - ContactUtil::SetStrInRecord( + PlatformResult status = ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.phonetic_last, FromJson(in, "phoneticLastName").c_str()); + if (status.IsError()) return status; } // contact_name is being added as a child to contacts_record @@ -455,7 +471,9 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, if (!update) { err = contacts_record_add_child_record( contacts_record, _contacts_contact.name, *contacts_name_ptr); - ContactUtil::ErrorChecker(err, "Contacts record add child error"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Contacts record add child error"); + if (status.IsError()) return status; } // and now unique_ptr can be released - contacts_name is protected // by its parent (contacts_record) @@ -469,26 +487,36 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, contacts_record, _contacts_contact.nickname, 0, &nickname_record); if (CONTACTS_ERROR_NONE != err && nullptr == nickname_record) { err = contacts_record_create(_contacts_nickname._uri, &nickname_record); - ContactUtil::ErrorChecker(err, "Contacts record create error"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Contacts record create error"); + if (status.IsError()) return status; + update = false; } ContactsRecordHPtr nickname_ptr(&nickname_record, ContactsDeleter); - ContactUtil::SetStrInRecord(*nickname_ptr, _contacts_nickname.name, - JsonCast(nickname).c_str()); + PlatformResult status = + ContactUtil::SetStrInRecord(*nickname_ptr, _contacts_nickname.name, + JsonCast(nickname).c_str()); + if (status.IsError()) return status; + if (!update) { err = contacts_record_add_child_record( contacts_record, _contacts_contact.nickname, *nickname_ptr); - ContactUtil::ErrorChecker(err, "Contacts record add child error"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Contacts record add child error"); + if (status.IsError()) return status; } // Do not delete record, it is passed to the platform nickname_ptr.release(); } // TODO update displayName in JS! + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactEmailAddressFromContactsRecord( +PlatformResult ImportContactEmailAddressFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { JsonObject& out = *out_ptr; @@ -496,7 +524,7 @@ void ImportContactEmailAddressFromContactsRecord( // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -504,27 +532,37 @@ void ImportContactEmailAddressFromContactsRecord( err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.email, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* email = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_email.email, &email); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_email.email, &email); + if (status.IsError()) return status; + if (!email) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } out.insert(std::make_pair("email", JsonValue{email})); bool is_default = false; - ContactUtil::GetBoolFromRecord(child_record, _contacts_email.is_default, - &is_default); + status = ContactUtil::GetBoolFromRecord( + child_record, _contacts_email.is_default, &is_default); + if (status.IsError()) return status; + out.insert(std::make_pair("isDefault", JsonValue{is_default})); char* label = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_email.label, &label); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_email.label, + &label); + if (status.IsError()) return status; + out.insert(std::make_pair("label", label ? JsonValue{label} : JsonValue{})); int type = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_email.type, &type); + status = + ContactUtil::GetIntFromRecord(child_record, _contacts_email.type, &type); + if (status.IsError()) return status; JsonArray types; if (type & CONTACTS_EMAIL_TYPE_HOME) { @@ -545,30 +583,43 @@ void ImportContactEmailAddressFromContactsRecord( } out.insert(std::make_pair("types", JsonValue{types})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactEmailAddressToContactsRecord( +PlatformResult ExportContactEmailAddressToContactsRecord( contacts_record_h contacts_record, const JsonObject& in) { contacts_record_h c_email_record_h = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; err = contacts_record_create(_contacts_email._uri, &c_email_record_h); - ContactUtil::ErrorChecker(err, "Failed to create email record in database"); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Failed to create email record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&c_email_record_h, ContactsDeleter); - ContactUtil::SetStrInRecord(c_email_record_h, _contacts_email.email, - FromJson(in, "email").c_str()); - ContactUtil::SetBoolInRecord(c_email_record_h, _contacts_email.is_default, - FromJson(in, "isDefault")); + status = + ContactUtil::SetStrInRecord(c_email_record_h, _contacts_email.email, + FromJson(in, "email").c_str()); + if (status.IsError()) return status; + + status = + ContactUtil::SetBoolInRecord(c_email_record_h, _contacts_email.is_default, + FromJson(in, "isDefault")); + if (status.IsError()) return status; + if (!IsNull(in, "label")) { - ContactUtil::SetStrInRecord(c_email_record_h, _contacts_email.label, - FromJson(in, "label").c_str()); + status = + ContactUtil::SetStrInRecord(c_email_record_h, _contacts_email.label, + FromJson(in, "label").c_str()); + if (status.IsError()) return status; } int type_to_set = 0; @@ -586,43 +637,56 @@ void ExportContactEmailAddressToContactsRecord( type_to_set |= CONTACTS_EMAIL_TYPE_HOME; } } - ContactUtil::SetIntInRecord(c_email_record_h, _contacts_email.type, - type_to_set); + status = ContactUtil::SetIntInRecord(c_email_record_h, _contacts_email.type, + type_to_set); + if (status.IsError()) return status; err = contacts_record_add_child_record( contacts_record, _contacts_contact.email, c_email_record_h); - ContactUtil::ErrorChecker(err, "Fail to save email record into database"); + status = + ContactUtil::ErrorChecker(err, "Fail to save email record into database"); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactPhoneNumberFromContactsRecord(contacts_record_h contacts_record, - unsigned int index, JsonObject* out_ptr) { +PlatformResult ImportContactPhoneNumberFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, + JsonObject* out_ptr) { JsonObject& out = *out_ptr; contacts_record_h child_record = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.number, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* phone = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_number.number, &phone); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_number.number, &phone); + if (status.IsError()) return status; out.insert(std::make_pair("number", JsonValue{phone})); bool is_default = false; - ContactUtil::GetBoolFromRecord(child_record, _contacts_number.is_default, - &is_default); + status = ContactUtil::GetBoolFromRecord( + child_record, _contacts_number.is_default, &is_default); + if (status.IsError()) return status; + out.insert(std::make_pair("isDefault", JsonValue{is_default})); int type = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_number.type, &type); + status = + ContactUtil::GetIntFromRecord(child_record, _contacts_number.type, &type); + if (status.IsError()) return status; JsonArray types; if (type & CONTACTS_NUMBER_TYPE_HOME) { @@ -679,31 +743,46 @@ void ImportContactPhoneNumberFromContactsRecord(contacts_record_h contacts_recor out.insert(std::make_pair("types", types)); char* label = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_number.label, &label); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_number.label, + &label); + if (status.IsError()) return status; + out.insert(std::make_pair("label", label ? JsonValue{label} : JsonValue{})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactPhoneNumberToContactsRecord(contacts_record_h contacts_record, - const JsonObject& in) { +PlatformResult ExportContactPhoneNumberToContactsRecord( + contacts_record_h contacts_record, const JsonObject& in) { contacts_record_h phone_record = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = contacts_record_create(_contacts_number._uri, &phone_record); - ContactUtil::ErrorChecker(err, "Fail to create phone_record in database"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Fail to create phone_record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&phone_record, ContactsDeleter); - ContactUtil::SetStrInRecord(phone_record, _contacts_number.number, - FromJson(in, "number").c_str()); + status = + ContactUtil::SetStrInRecord(phone_record, _contacts_number.number, + FromJson(in, "number").c_str()); + if (status.IsError()) return status; + + status = + ContactUtil::SetBoolInRecord(phone_record, _contacts_number.is_default, + FromJson(in, "isDefault")); + if (status.IsError()) return status; - ContactUtil::SetBoolInRecord(phone_record, _contacts_number.is_default, - FromJson(in, "isDefault")); if (!IsNull(in, "label")) { - ContactUtil::SetStrInRecord(phone_record, _contacts_address.label, - FromJson(in, "label").c_str()); + status = + ContactUtil::SetStrInRecord(phone_record, _contacts_address.label, + FromJson(in, "label").c_str()); + if (status.IsError()) return status; } int type_to_set = 0; @@ -747,16 +826,23 @@ void ExportContactPhoneNumberToContactsRecord(contacts_record_h contacts_record, } } - ContactUtil::SetIntInRecord(phone_record, _contacts_number.type, type_to_set); + status = ContactUtil::SetIntInRecord(phone_record, _contacts_number.type, + type_to_set); + if (status.IsError()) return status; err = contacts_record_add_child_record( contacts_record, _contacts_contact.number, phone_record); - ContactUtil::ErrorChecker(err, "Fail to set number value to phone_record"); + status = ContactUtil::ErrorChecker( + err, "Fail to set number value to phone_record"); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactOrganizationFromContactsRecord( +PlatformResult ImportContactOrganizationFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { JsonObject& out = *out_ptr; @@ -764,7 +850,7 @@ void ImportContactOrganizationFromContactsRecord( // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -772,93 +858,119 @@ void ImportContactOrganizationFromContactsRecord( err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.company, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* char_value = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_company.name, - &char_value); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_company.name, &char_value); + if (status.IsError()) return status; + out.insert( std::make_pair("name", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_company.department, - &char_value); + status = ContactUtil::GetStrFromRecord( + child_record, _contacts_company.department, &char_value); + if (status.IsError()) return status; + out.insert(std::make_pair("department", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_company.job_title, - &char_value); + status = ContactUtil::GetStrFromRecord( + child_record, _contacts_company.job_title, &char_value); + if (status.IsError()) return status; + out.insert(std::make_pair("title", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_company.role, - &char_value); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_company.role, + &char_value); + if (status.IsError()) return status; + out.insert( std::make_pair("role", char_value ? JsonValue{char_value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_company.logo, - &char_value); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_company.logo, + &char_value); + if (status.IsError()) return status; + out.insert(std::make_pair("logoURI", char_value ? JsonValue{char_value} : JsonValue{})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactOrganizationToContactsRecord( +PlatformResult ExportContactOrganizationToContactsRecord( contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } contacts_record_h organization_record = nullptr; int err = CONTACTS_ERROR_NONE; err = contacts_record_create(_contacts_company._uri, &organization_record); - ContactUtil::ErrorChecker(err, - "Failed to create organization record in database"); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Failed to create organization record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&organization_record, ContactsDeleter); if (!IsNull(in, "name")) { - ContactUtil::SetStrInRecord(organization_record, _contacts_company.name, - FromJson(in, "name").c_str()); + status = + ContactUtil::SetStrInRecord(organization_record, _contacts_company.name, + FromJson(in, "name").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "department")) { - ContactUtil::SetStrInRecord(organization_record, - _contacts_company.department, - FromJson(in, "department").c_str()); + status = ContactUtil::SetStrInRecord( + organization_record, _contacts_company.department, + FromJson(in, "department").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "title")) { - ContactUtil::SetStrInRecord(organization_record, - _contacts_company.job_title, - FromJson(in, "title").c_str()); + status = ContactUtil::SetStrInRecord( + organization_record, _contacts_company.job_title, + FromJson(in, "title").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "role")) { - ContactUtil::SetStrInRecord(organization_record, _contacts_company.role, - FromJson(in, "role").c_str()); + status = + ContactUtil::SetStrInRecord(organization_record, _contacts_company.role, + FromJson(in, "role").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "logoURI")) { std::string path = ContactUtil::ConvertUriToPath(FromJson(in, "logoURI")); - ContactUtil::SetStrInRecord(organization_record, _contacts_company.logo, - path.c_str()); + status = ContactUtil::SetStrInRecord( + organization_record, _contacts_company.logo, path.c_str()); + if (status.IsError()) return status; } err = contacts_record_add_child_record( contacts_record, _contacts_contact.company, organization_record); - ContactUtil::ErrorChecker(err, "Fail to set company value to child_record"); + status = ContactUtil::ErrorChecker( + err, "Fail to set company value to child_record"); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactWebSiteFromContactsRecord(contacts_record_h contacts_record, - unsigned int index, - JsonObject* out_ptr) { +PlatformResult ImportContactWebSiteFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, + JsonObject* out_ptr) { JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -866,74 +978,91 @@ void ImportContactWebSiteFromContactsRecord(contacts_record_h contacts_record, err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.url, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* char_value = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_url.url, &char_value); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_url.url, &char_value); + if (status.IsError()) return status; + out.insert(std::make_pair("logoURI", char_value ? char_value : "")); int type = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_url.type, &type); + status = + ContactUtil::GetIntFromRecord(child_record, _contacts_url.type, &type); + if (status.IsError()) return status; out.insert(std::make_pair("logoURI", (CONTACTS_URL_TYPE_HOME == type) ? kContactWebSiteTypeHomePage : kContactWebSiteTypeBlog)); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactWebSiteToContactsRecord(contacts_record_h contacts_record, - const JsonObject& in) { +PlatformResult ExportContactWebSiteToContactsRecord( + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } if (IsNull(in, "url")) { LoggerD("WebSite urls are not set"); - return; + return PlatformResult(ErrorCode::NO_ERROR); } const std::string& url = FromJson(in, "url"); if (url.empty()) { LoggerD("WebSite urls are not set"); - return; + return PlatformResult(ErrorCode::NO_ERROR); } int err = CONTACTS_ERROR_NONE; contacts_record_h website_record_h = nullptr; err = contacts_record_create(_contacts_url._uri, &website_record_h); - ContactUtil::ErrorChecker(err, "Fail to create website record in database."); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Fail to create website record in database."); + if (status.IsError()) return status; + ContactsRecordHPtr record(&website_record_h, ContactsDeleter); - ContactUtil::SetStrInRecord(website_record_h, _contacts_url.url, url.c_str()); + status = ContactUtil::SetStrInRecord(website_record_h, _contacts_url.url, + url.c_str()); + if (status.IsError()) return status; int type_to_set = (FromJson(in, "type") == kContactWebSiteTypeHomePage) ? CONTACTS_URL_TYPE_HOME : CONTACTS_URL_TYPE_WORK; - ContactUtil::SetIntInRecord(website_record_h, _contacts_url.type, - type_to_set); + status = ContactUtil::SetIntInRecord(website_record_h, _contacts_url.type, + type_to_set); + if (status.IsError()) return status; err = contacts_record_add_child_record(contacts_record, _contacts_contact.url, website_record_h); - ContactUtil::ErrorChecker( + status = ContactUtil::ErrorChecker( err, "Problem during saving WebSite urls into database."); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -bool ImportContactAnniversariesFromContactsRecord( - contacts_record_h contacts_record, unsigned int index, - JsonObject* out_ptr) { +PlatformResult ImportContactAnniversariesFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr, + bool* ret) { JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -941,67 +1070,90 @@ bool ImportContactAnniversariesFromContactsRecord( err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.event, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return false; + *ret = false; + return PlatformResult(ErrorCode::NO_ERROR); } int value = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_event.type, &value); + PlatformResult status = + ContactUtil::GetIntFromRecord(child_record, _contacts_event.type, &value); + if (status.IsError()) return status; if (CONTACTS_EVENT_TYPE_BIRTH == value) { - return false; + *ret = false; + return PlatformResult(ErrorCode::NO_ERROR); } if (CONTACTS_EVENT_TYPE_ANNIVERSARY == value) { - ContactUtil::GetIntFromRecord(child_record, _contacts_event.date, &value); + status = ContactUtil::GetIntFromRecord(child_record, _contacts_event.date, + &value); + if (status.IsError()) return status; + out.insert(std::make_pair("date", JsonValue{static_cast(value)})); char* label = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_event.label, &label); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_event.label, + &label); + if (status.IsError()) return status; out.insert(std::make_pair("label", label ? JsonValue{label} : JsonValue{})); } - return true; + + *ret = true; + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactAnniversariesToContactsRecord( +PlatformResult ExportContactAnniversariesToContactsRecord( contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int date = static_cast(FromJson(in, "date")); if (date == 0) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } int err = CONTACTS_ERROR_NONE; contacts_record_h anniversary_record = nullptr; err = contacts_record_create(_contacts_event._uri, &anniversary_record); - ContactUtil::ErrorChecker(err, - "Failed to create anniversary record in database"); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Failed to create anniversary record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&anniversary_record, ContactsDeleter); - ContactUtil::SetIntInRecord(anniversary_record, _contacts_event.type, - CONTACTS_EVENT_TYPE_ANNIVERSARY); + status = ContactUtil::SetIntInRecord(anniversary_record, _contacts_event.type, + CONTACTS_EVENT_TYPE_ANNIVERSARY); + if (status.IsError()) return status; - ContactUtil::SetIntInRecord(anniversary_record, _contacts_event.date, date); + status = ContactUtil::SetIntInRecord(anniversary_record, _contacts_event.date, + date); + if (status.IsError()) return status; if (!IsNull(in, "label")) { - ContactUtil::SetStrInRecord(anniversary_record, _contacts_event.label, - FromJson(in, "label").c_str()); + status = + ContactUtil::SetStrInRecord(anniversary_record, _contacts_event.label, + FromJson(in, "label").c_str()); + if (status.IsError()) return status; } err = contacts_record_add_child_record( contacts_record, _contacts_contact.event, anniversary_record); - ContactUtil::ErrorChecker(err, "Fail to save anniversary record in database"); + status = ContactUtil::ErrorChecker( + err, "Fail to save anniversary record in database"); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactRelationshipFromContactsRecord( +PlatformResult ImportContactRelationshipFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { JsonObject& out = *out_ptr; @@ -1009,7 +1161,7 @@ void ImportContactRelationshipFromContactsRecord( // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -1017,21 +1169,24 @@ void ImportContactRelationshipFromContactsRecord( err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.relationship, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* relative = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_relationship.name, - &relative); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_relationship.name, &relative); + if (status.IsError()) return status; + if (!relative) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } out.insert(std::make_pair("relativeName", JsonString{relative})); int type = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_relationship.type, - &type); + status = ContactUtil::GetIntFromRecord(child_record, + _contacts_relationship.type, &type); + if (status.IsError()) return status; // TODO Move out.insert outside of switch statement. switch (type) { @@ -1103,28 +1258,37 @@ void ImportContactRelationshipFromContactsRecord( } char* label = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_relationship.label, - &label); + status = ContactUtil::GetStrFromRecord(child_record, + _contacts_relationship.label, &label); + if (status.IsError()) return status; + out.insert(std::make_pair("label", label ? JsonValue{label} : JsonValue{})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactRelationshipToContactsRecord( +PlatformResult ExportContactRelationshipToContactsRecord( contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; contacts_record_h child_record = nullptr; err = contacts_record_create(_contacts_relationship._uri, &child_record); - ContactUtil::ErrorChecker(err, "Fail to create child_record in database"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Fail to create child_record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&child_record, ContactsDeleter); - ContactUtil::SetStrInRecord(child_record, _contacts_relationship.name, - FromJson(in, "relativeName").c_str()); + status = ContactUtil::SetStrInRecord( + child_record, _contacts_relationship.name, + FromJson(in, "relativeName").c_str()); + if (status.IsError()) return status; const JsonString& type = FromJson(in, "type"); int type_to_set; @@ -1162,21 +1326,29 @@ void ExportContactRelationshipToContactsRecord( type_to_set = CONTACTS_MESSENGER_TYPE_OTHER; } - ContactUtil::SetIntInRecord(child_record, _contacts_relationship.type, - type_to_set); + status = ContactUtil::SetIntInRecord( + child_record, _contacts_relationship.type, type_to_set); + if (status.IsError()) return status; if (!IsNull(in, "label")) { - ContactUtil::SetStrInRecord(child_record, _contacts_relationship.label, - FromJson(in, "label").c_str()); + status = + ContactUtil::SetStrInRecord(child_record, _contacts_relationship.label, + FromJson(in, "label").c_str()); + if (status.IsError()) return status; } err = contacts_record_add_child_record( contacts_record, _contacts_contact.relationship, child_record); - ContactUtil::ErrorChecker(err, "Fail to set number value to child_record"); + status = ContactUtil::ErrorChecker( + err, "Fail to set number value to child_record"); + if (status.IsError()) return status; + record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactInstantMessengerFromContactsRecord( +PlatformResult ImportContactInstantMessengerFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { JsonObject& out = *out_ptr; @@ -1184,7 +1356,7 @@ void ImportContactInstantMessengerFromContactsRecord( // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -1193,21 +1365,25 @@ void ImportContactInstantMessengerFromContactsRecord( contacts_record, _contacts_contact.messenger, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { LoggerW("Skipping message with index %i. error code: %i", index, err); - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* im_address = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_messenger.im_id, - &im_address); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_messenger.im_id, &im_address); + if (status.IsError()) return status; + if (!im_address) { LoggerW("Skipping message with index %i. missing im address", index); - return; + return PlatformResult(ErrorCode::NO_ERROR); } out.insert(std::make_pair("imAddress", JsonValue{im_address})); int type = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_messenger.type, &type); + status = ContactUtil::GetIntFromRecord(child_record, _contacts_messenger.type, + &type); + if (status.IsError()) return status; // TODO Move out.insert outside of switch statement. switch (type) { @@ -1263,28 +1439,37 @@ void ImportContactInstantMessengerFromContactsRecord( } char* label = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_messenger.label, - &label); + status = ContactUtil::GetStrFromRecord(child_record, + _contacts_messenger.label, &label); + if (status.IsError()) return status; + out.insert(std::make_pair("label", label ? JsonValue{label} : JsonValue{})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactInstantMessengerToContactsRecord( +PlatformResult ExportContactInstantMessengerToContactsRecord( contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; contacts_record_h child_record = nullptr; err = contacts_record_create(_contacts_messenger._uri, &child_record); - ContactUtil::ErrorChecker(err, "Fail to create child_record in database"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Fail to create child_record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&child_record, ContactsDeleter); - ContactUtil::SetStrInRecord(child_record, _contacts_messenger.im_id, - FromJson(in, "imAddress").c_str()); + status = ContactUtil::SetStrInRecord( + child_record, _contacts_messenger.im_id, + FromJson(in, "imAddress").c_str()); + if (status.IsError()) return status; int type_to_set = 0; const JsonString& type = FromJson(in, "type"); @@ -1314,29 +1499,37 @@ void ExportContactInstantMessengerToContactsRecord( type_to_set = CONTACTS_MESSENGER_TYPE_OTHER; } - ContactUtil::SetIntInRecord(child_record, _contacts_messenger.type, - type_to_set); + status = ContactUtil::SetIntInRecord(child_record, _contacts_messenger.type, + type_to_set); + if (status.IsError()) return status; if (!IsNull(in, "label")) { - ContactUtil::SetStrInRecord(child_record, _contacts_messenger.label, - FromJson(in, "label").c_str()); + status = + ContactUtil::SetStrInRecord(child_record, _contacts_messenger.label, + FromJson(in, "label").c_str()); + if (status.IsError()) return status; } err = contacts_record_add_child_record( contacts_record, _contacts_contact.messenger, child_record); - ContactUtil::ErrorChecker(err, "Fail to set number value to child_record"); + status = ContactUtil::ErrorChecker( + err, "Fail to set number value to child_record"); + if (status.IsError()) return status; + record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactAddressFromContactsRecord(contacts_record_h contacts_record, - unsigned int index, - JsonObject* out_ptr) { +PlatformResult ImportContactAddressFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, + JsonObject* out_ptr) { JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -1344,40 +1537,60 @@ void ImportContactAddressFromContactsRecord(contacts_record_h contacts_record, err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.address, index, &child_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return; + return PlatformResult(ErrorCode::NO_ERROR); } char* value = nullptr; - ContactUtil::GetStrFromRecord(child_record, _contacts_address.country, - &value); + PlatformResult status = ContactUtil::GetStrFromRecord( + child_record, _contacts_address.country, &value); + if (status.IsError()) return status; + out.insert(std::make_pair("country", value ? JsonValue{value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_address.region, &value); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_address.region, + &value); + if (status.IsError()) return status; + out.insert(std::make_pair("region", value ? JsonValue{value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_address.locality, - &value); + status = ContactUtil::GetStrFromRecord(child_record, + _contacts_address.locality, &value); + if (status.IsError()) return status; + out.insert(std::make_pair("city", value ? JsonValue{value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_address.street, &value); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_address.street, + &value); + if (status.IsError()) return status; + out.insert( std::make_pair("streetAddress", value ? JsonValue{value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_address.extended, - &value); + status = ContactUtil::GetStrFromRecord(child_record, + _contacts_address.extended, &value); + if (status.IsError()) return status; + out.insert(std::make_pair("additionalInformation", value ? JsonValue{value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_address.postal_code, - &value); + status = ContactUtil::GetStrFromRecord(child_record, + _contacts_address.postal_code, &value); + if (status.IsError()) return status; + out.insert( std::make_pair("postalCode", value ? JsonValue{value} : JsonValue{})); - ContactUtil::GetStrFromRecord(child_record, _contacts_address.label, &value); + status = ContactUtil::GetStrFromRecord(child_record, _contacts_address.label, + &value); + if (status.IsError()) return status; + out.insert(std::make_pair("label", value ? JsonValue{value} : JsonValue{})); bool bool_value = false; - ContactUtil::GetBoolFromRecord(child_record, _contacts_address.is_default, - &bool_value); + status = ContactUtil::GetBoolFromRecord( + child_record, _contacts_address.is_default, &bool_value); + if (status.IsError()) return status; + out.insert(std::make_pair("isDefault", JsonValue{bool_value})); int int_value = 0; - ContactUtil::GetIntFromRecord(child_record, _contacts_address.type, - &int_value); + status = ContactUtil::GetIntFromRecord(child_record, _contacts_address.type, + &int_value); + if (status.IsError()) return status; JsonArray types; if (int_value & CONTACTS_ADDRESS_TYPE_HOME) { @@ -1397,56 +1610,75 @@ void ImportContactAddressFromContactsRecord(contacts_record_h contacts_record, types.push_back(JsonValue{kContactAddressTypeHome}); } out.insert(std::make_pair("types", types)); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactAddressToContactsRecord(contacts_record_h contacts_record, - const JsonObject& in) { +PlatformResult ExportContactAddressToContactsRecord( + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; contacts_record_h address_record = nullptr; err = contacts_record_create(_contacts_address._uri, &address_record); - ContactUtil::ErrorChecker(err, "Failed to create address record in database"); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Failed to create address record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(&address_record, ContactsDeleter); if (!IsNull(in, "country")) { - ContactUtil::SetStrInRecord(address_record, _contacts_address.country, - FromJson(in, "country").c_str()); + status = ContactUtil::SetStrInRecord( + address_record, _contacts_address.country, + FromJson(in, "country").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "region")) { - ContactUtil::SetStrInRecord(address_record, _contacts_address.region, - FromJson(in, "region").c_str()); + status = + ContactUtil::SetStrInRecord(address_record, _contacts_address.region, + FromJson(in, "region").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "city")) { - ContactUtil::SetStrInRecord(address_record, _contacts_address.locality, - FromJson(in, "city").c_str()); + status = + ContactUtil::SetStrInRecord(address_record, _contacts_address.locality, + FromJson(in, "city").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "streetAddress")) { - ContactUtil::SetStrInRecord( + status = ContactUtil::SetStrInRecord( address_record, _contacts_address.street, FromJson(in, "streetAddress").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "additionalInformation")) { - ContactUtil::SetStrInRecord( + status = ContactUtil::SetStrInRecord( address_record, _contacts_address.extended, FromJson(in, "additionalInformation").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "postalCode")) { - ContactUtil::SetStrInRecord(address_record, _contacts_address.postal_code, - FromJson(in, "postalCode").c_str()); + status = ContactUtil::SetStrInRecord( + address_record, _contacts_address.postal_code, + FromJson(in, "postalCode").c_str()); + if (status.IsError()) return status; } if (!IsNull(in, "label")) { - ContactUtil::SetStrInRecord(address_record, _contacts_address.label, - FromJson(in, "label").c_str()); + status = + ContactUtil::SetStrInRecord(address_record, _contacts_address.label, + FromJson(in, "label").c_str()); + if (status.IsError()) return status; } - ContactUtil::SetBoolInRecord(address_record, _contacts_address.is_default, - FromJson(in, "isDefault")); + status = + ContactUtil::SetBoolInRecord(address_record, _contacts_address.is_default, + FromJson(in, "isDefault")); + if (status.IsError()) return status; int type_to_set = 0; const JsonArray& types = FromJson(in, "types"); @@ -1463,23 +1695,29 @@ void ExportContactAddressToContactsRecord(contacts_record_h contacts_record, } } - ContactUtil::SetIntInRecord(address_record, _contacts_address.type, - type_to_set); + status = ContactUtil::SetIntInRecord(address_record, _contacts_address.type, + type_to_set); + if (status.IsError()) return status; err = contacts_record_add_child_record( contacts_record, _contacts_contact.address, address_record); - ContactUtil::ErrorChecker(err, "Fail to save address record in database"); + status = + ContactUtil::ErrorChecker(err, "Fail to save address record in database"); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -JsonValue ImportContactNotesFromContactsRecord( - contacts_record_h contacts_record, unsigned int index) { +PlatformResult ImportContactNotesFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonValue* val) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; @@ -1487,83 +1725,111 @@ JsonValue ImportContactNotesFromContactsRecord( err = contacts_record_get_child_record_at_p( contacts_record, _contacts_contact.note, index, ¬es_record); if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { - return {}; + return PlatformResult(ErrorCode::NO_ERROR); } char* note = nullptr; - ContactUtil::GetStrFromRecord(notes_record, _contacts_note.note, ¬e); + PlatformResult status = + ContactUtil::GetStrFromRecord(notes_record, _contacts_note.note, ¬e); + if (status.IsError()) return status; if (note) { - return JsonValue{note}; + *val = JsonValue{note}; } - return {}; + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportNotesToContactsRecord(contacts_record_h contacts_record, - const std::string& value) { +PlatformResult ExportNotesToContactsRecord(contacts_record_h contacts_record, + const std::string& value) { contacts_record_h notes_record = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int err = CONTACTS_ERROR_NONE; err = contacts_record_create(_contacts_note._uri, ¬es_record); - ContactUtil::ErrorChecker(err, "Fail to create note record in database"); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Fail to create note record in database"); + if (status.IsError()) return status; + ContactsRecordHPtr record(¬es_record, ContactsDeleter); - ContactUtil::SetStrInRecord(notes_record, _contacts_note.note, value.c_str()); + status = ContactUtil::SetStrInRecord(notes_record, _contacts_note.note, + value.c_str()); + if (status.IsError()) return status; err = contacts_record_add_child_record(contacts_record, _contacts_contact.note, notes_record); - ContactUtil::ErrorChecker(err, "Fail to save note record in database"); + status = + ContactUtil::ErrorChecker(err, "Fail to save note record in database"); + if (status.IsError()) return status; // Do not delete record, it is passed to the platform record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactFromContactsRecord(contacts_record_h contacts_record, - JsonObject* out_ptr) { +PlatformResult ImportContactFromContactsRecord( + contacts_record_h contacts_record, JsonObject* out_ptr) { JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerW("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } int id = 0; - ContactUtil::GetIntFromRecord(contacts_record, _contacts_contact.id, &id); + PlatformResult status = + ContactUtil::GetIntFromRecord(contacts_record, _contacts_contact.id, &id); + if (status.IsError()) return status; + out.insert(std::make_pair("id", JsonValue{std::to_string(id)})); - ContactUtil::GetIntFromRecord(contacts_record, - _contacts_contact.address_book_id, &id); + status = ContactUtil::GetIntFromRecord( + contacts_record, _contacts_contact.address_book_id, &id); + if (status.IsError()) return status; + out.insert(std::make_pair("addressBookId", JsonValue{std::to_string(id)})); - ContactUtil::GetIntFromRecord(contacts_record, _contacts_contact.person_id, - &id); + status = ContactUtil::GetIntFromRecord(contacts_record, + _contacts_contact.person_id, &id); + if (status.IsError()) return status; + out.insert(std::make_pair("personId", JsonValue{std::to_string(id)})); bool is_favorite = false; - ContactUtil::GetBoolFromRecord(contacts_record, _contacts_contact.is_favorite, - &is_favorite); + status = ContactUtil::GetBoolFromRecord( + contacts_record, _contacts_contact.is_favorite, &is_favorite); + if (status.IsError()) return status; + out.insert(std::make_pair("isFavorite", JsonValue{is_favorite})); int last_update = 0; - ContactUtil::GetIntFromRecord(contacts_record, _contacts_contact.changed_time, - &last_update); + status = ContactUtil::GetIntFromRecord( + contacts_record, _contacts_contact.changed_time, &last_update); + if (status.IsError()) return status; + out.insert(std::make_pair("lastUpdated", JsonValue{static_cast(last_update)})); //### ContactName: ### JsonObject name; - if (ImportContactNameFromContactsRecord(contacts_record, &name)) { + bool is_contact_name; + status = ImportContactNameFromContactsRecord(contacts_record, &name, + &is_contact_name); + if (status.IsError()) return status; + + if (is_contact_name) { out.insert(std::make_pair("name", name)); } else { out.insert(std::make_pair("name", JsonValue{})); } - typedef void (*ImportFunc)(contacts_record_h, unsigned int, JsonObject*); + typedef PlatformResult (*ImportFunc)(contacts_record_h, unsigned int, JsonObject*); struct ImportData { const char* name; unsigned int property_id; @@ -1588,9 +1854,12 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, JsonArray& array = out.insert(std::make_pair(data.name, JsonArray())) .first->second.get(); - for (unsigned int i = 0, n = ContactUtil::GetNumberOfChildRecord( - contacts_record, data.property_id); - i < n; ++i) { + int child_rec_count; + status = ContactUtil::GetNumberOfChildRecord( + contacts_record, data.property_id, &child_rec_count); + if (status.IsError()) return status; + + for (unsigned int i = 0; i < child_rec_count; ++i) { JsonValue val{JsonObject{}}; data.import_func(contacts_record, i, &val.get()); array.push_back(val); @@ -1601,12 +1870,21 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, JsonArray& anniversaries = out.insert(std::make_pair("anniversaries", JsonArray())) .first->second.get(); - for (unsigned int i = 0, n = ContactUtil::GetNumberOfChildRecord( - contacts_record, _contacts_contact.event); - i < n; ++i) { + + int child_rec_count; + status = ContactUtil::GetNumberOfChildRecord( + contacts_record, _contacts_contact.event, &child_rec_count); + if (status.IsError()) return status; + + bool is_contact_anniv; + for (unsigned int i = 0; i < child_rec_count; ++i) { JsonValue anniversary{JsonObject{}}; - if (ImportContactAnniversariesFromContactsRecord( - contacts_record, i, &anniversary.get())) { + + PlatformResult status = ImportContactAnniversariesFromContactsRecord( + contacts_record, i, &anniversary.get(), &is_contact_anniv); + if (status.IsError()) return status; + + if (is_contact_anniv) { anniversaries.push_back(anniversary); } else { out.insert(std::make_pair("birthday", anniversaries)); @@ -1616,55 +1894,73 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, //### m_notes: ### JsonArray& notes = out.insert(std::make_pair("notes", JsonArray())) .first->second.get(); - for (unsigned int i = 0, n = ContactUtil::GetNumberOfChildRecord( - contacts_record, _contacts_contact.note); - i < n; ++i) { - notes.push_back(ImportContactNotesFromContactsRecord(contacts_record, i)); + + status = ContactUtil::GetNumberOfChildRecord( + contacts_record, _contacts_contact.note, &child_rec_count); + if (status.IsError()) return status; + + for (unsigned int i = 0; i < child_rec_count; ++i) { + JsonValue val{JsonObject{}}; + + status = ImportContactNotesFromContactsRecord(contacts_record, i, &val); + if (status.IsError()) return status; + + notes.push_back(val); } //### m_photo_uri ### //### m_ringtone_uri ### { char* value = nullptr; - ContactUtil::GetStrFromRecord( + status = ContactUtil::GetStrFromRecord( contacts_record, _contacts_contact.image_thumbnail_path, &value); + if (status.IsError()) return status; std::make_pair("photoURI", value ? JsonValue{value} : JsonValue{}); - ContactUtil::GetStrFromRecord(contacts_record, - _contacts_contact.ringtone_path, &value); + status = ContactUtil::GetStrFromRecord( + contacts_record, _contacts_contact.ringtone_path, &value); + if (status.IsError()) return status; + std::make_pair("ringtoneURI", value ? JsonValue{value} : JsonValue{}); value = nullptr; - ContactUtil::GetStrFromRecord(contacts_record, - _contacts_contact.message_alert, &value); + status = ContactUtil::GetStrFromRecord( + contacts_record, _contacts_contact.message_alert, &value); + if (status.IsError()) return status; + out.insert(std::make_pair( "messageAlertURI", value ? JsonValue{ConvertPathToUri(value)} : JsonValue{})); value = nullptr; - ContactUtil::GetStrFromRecord(contacts_record, _contacts_contact.vibration, - &value); + status = ContactUtil::GetStrFromRecord(contacts_record, + _contacts_contact.vibration, &value); + if (status.IsError()) return status; + out.insert(std::make_pair( "vibrationURI", value ? JsonValue{ConvertPathToUri(value)} : JsonValue{})); } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactToContactsRecord(contacts_record_h contacts_record, - const JsonObject& in) { +PlatformResult ExportContactToContactsRecord(contacts_record_h contacts_record, + const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerW("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } //### ContactName: ### if (!IsNull(in, "name")) { - ExportContactNameToContactsRecord(contacts_record, - FromJson(in, "name")); + PlatformResult status = ExportContactNameToContactsRecord( + contacts_record, FromJson(in, "name")); + if (status.IsError()) return status; } - typedef void (*ExportFunc)(contacts_record_h, const JsonObject&); + typedef PlatformResult (*ExportFunc)(contacts_record_h, const JsonObject&); struct ExportDataHelper { unsigned int property_id; const char* name; @@ -1688,7 +1984,10 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, ExportContactRelationshipToContactsRecord}, }; for (auto& data : exports) { - ContactUtil::ClearAllContactRecord(contacts_record, data.property_id); + PlatformResult status = + ContactUtil::ClearAllContactRecord(contacts_record, data.property_id); + if (status.IsError()) return status; + const JsonArray& elements = FromJson(in, data.name); for (auto& element : elements) { data.export_func(contacts_record, JsonCast(element)); @@ -1697,7 +1996,10 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, { //### m_notes: ### - ContactUtil::ClearAllContactRecord(contacts_record, _contacts_contact.note); + PlatformResult status = ContactUtil::ClearAllContactRecord( + contacts_record, _contacts_contact.note); + if (status.IsError()) return status; + const JsonArray& elements = FromJson(in, "notes"); for (auto& element : elements) { auto& str = JsonCast(element); @@ -1712,12 +2014,16 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, if (IsNull(in, "photoURI")) { contacts_record_h child_record = nullptr; int err = contacts_record_create(_contacts_image._uri, &child_record); - ContactUtil::ErrorChecker(err, - "Fail to create image uri record in database."); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Fail to create image uri record in database."); + if (status.IsError()) return status; + ContactsRecordHPtr record(&child_record, ContactsDeleter); err = contacts_record_add_child_record( contacts_record, _contacts_contact.image, child_record); - ContactUtil::ErrorChecker(err, "Fail to add child to image uri."); + status = ContactUtil::ErrorChecker(err, "Fail to add child to image uri."); + if (status.IsError()) return status; + // Do not delete record, it is passed to the platform record.release(); } else { @@ -1727,8 +2033,10 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, contacts_record, _contacts_contact.image, 0, &child_record); if (CONTACTS_ERROR_NONE != err || nullptr == child_record) { err = contacts_record_create(_contacts_image._uri, &child_record); - ContactUtil::ErrorChecker(err, - "Fail to create image uri record in database."); + PlatformResult status = ContactUtil::ErrorChecker( + err, "Fail to create image uri record in database."); + if (status.IsError()) return status; + is_first = true; } ContactsRecordHPtr record(&child_record, ContactsDeleter); @@ -1741,14 +2049,17 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, if (!IsNull(in, "photoURI")) { real_path = ContactUtil::ConvertUriToPath(FromJson(in, "photoURI")); - ContactUtil::SetStrInRecord(child_record, _contacts_image.path, - real_path.c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + child_record, _contacts_image.path, real_path.c_str()); + if (status.IsError()) return status; } if (is_first) { err = contacts_record_add_child_record( contacts_record, _contacts_contact.image, child_record); - ContactUtil::ErrorChecker(err, "Fail to add child to image uri."); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Fail to add child to image uri."); + if (status.IsError()) return status; } // Do not delete record, it is passed to the platform record.release(); @@ -1759,228 +2070,295 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, if (!IsNull(in, "ringtoneURI")) { real_path = ContactUtil::ConvertUriToPath(FromJson(in, "ringtoneURI")); - ContactUtil::SetStrInRecord( + PlatformResult status = ContactUtil::SetStrInRecord( contacts_record, _contacts_contact.ringtone_path, real_path.c_str()); + if (status.IsError()) return status; } // Contact.messageAlertURI if (!IsNull(in, "messageAlertURI")) { real_path = ContactUtil::ConvertUriToPath( FromJson(in, "messageAlertURI")); - ContactUtil::SetStrInRecord( + PlatformResult status = ContactUtil::SetStrInRecord( contacts_record, _contacts_contact.message_alert, real_path.c_str()); + if (status.IsError()) return status; } // Contact.vibrationURI if (!IsNull(in, "vibrationURI")) { real_path = ContactUtil::ConvertUriToPath(FromJson(in, "vibrationURI")); - ContactUtil::SetStrInRecord(contacts_record, _contacts_contact.vibration, - real_path.c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + contacts_record, _contacts_contact.vibration, real_path.c_str()); + if (status.IsError()) return status; } + + return PlatformResult(ErrorCode::NO_ERROR); } -void ImportContactGroupFromContactsRecord(contacts_record_h contacts_record, - JsonObject* out_ptr) { +PlatformResult ImportContactGroupFromContactsRecord( + contacts_record_h contacts_record, JsonObject* out_ptr) { JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { LoggerE("Contacts record is null"); - throw common::UnknownException("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); } // id int int_val = 0; - ContactUtil::GetIntFromRecord(contacts_record, _contacts_group.id, &int_val); + PlatformResult status = ContactUtil::GetIntFromRecord( + contacts_record, _contacts_group.id, &int_val); + if (status.IsError()) return status; + out.insert(std::make_pair("id", JsonValue{std::to_string(int_val)})); // addressBookId - ContactUtil::GetIntFromRecord(contacts_record, - _contacts_group.address_book_id, &int_val); + status = ContactUtil::GetIntFromRecord( + contacts_record, _contacts_group.address_book_id, &int_val); + if (status.IsError()) return status; + out.insert( std::make_pair("addressBookId", JsonValue{std::to_string(int_val)})); // name char* value = nullptr; - ContactUtil::GetStrFromRecord(contacts_record, _contacts_group.name, &value); + status = ContactUtil::GetStrFromRecord(contacts_record, _contacts_group.name, + &value); + if (status.IsError()) return status; + out.insert(std::make_pair("name", value ? JsonValue{value} : JsonValue{})); // photoURI value = nullptr; - ContactUtil::GetStrFromRecord(contacts_record, _contacts_group.image_path, - &value); + status = ContactUtil::GetStrFromRecord(contacts_record, + _contacts_group.image_path, &value); + if (status.IsError()) return status; + out.insert( std::make_pair("photoURI", value ? JsonValue{value} : JsonValue{})); // ringtoneURI value = nullptr; - ContactUtil::GetStrFromRecord(contacts_record, _contacts_group.ringtone_path, - &value); + status = ContactUtil::GetStrFromRecord(contacts_record, + _contacts_group.ringtone_path, &value); + if (status.IsError()) return status; + out.insert( std::make_pair("ringtoneURI", value ? JsonValue{value} : JsonValue{})); // is_read_only bool bool_value = false; - ContactUtil::GetBoolFromRecord(contacts_record, _contacts_group.is_read_only, - &bool_value); + status = ContactUtil::GetBoolFromRecord( + contacts_record, _contacts_group.is_read_only, &bool_value); + if (status.IsError()) return status; + out.insert(std::make_pair("readOnly", JsonValue{bool_value})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void ExportContactGroupToContactsRecord(contacts_record_h contacts_record, - const JsonObject& in) { +PlatformResult ExportContactGroupToContactsRecord( + contacts_record_h contacts_record, const JsonObject& in) { // name - ContactUtil::SetStrInRecord(contacts_record, _contacts_group.name, - FromJson(in, "name").c_str()); + PlatformResult status = + ContactUtil::SetStrInRecord(contacts_record, _contacts_group.name, + FromJson(in, "name").c_str()); + if (status.IsError()) return status; std::string real_path; // photoURI if (!IsNull(in, "photoURI")) { real_path = ConvertUriToPath(FromJson(in, "photoURI")); - ContactUtil::SetStrInRecord(contacts_record, _contacts_group.image_path, - real_path.c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + contacts_record, _contacts_group.image_path, real_path.c_str()); + if (status.IsError()) return status; } // ringtoneURI if (!IsNull(in, "ringtoneURI")) { real_path = ContactUtil::ConvertUriToPath(FromJson(in, "ringtoneURI")); // NOTE in the original code real path was not read - ContactUtil::SetStrInRecord(contacts_record, _contacts_group.ringtone_path, - real_path.c_str()); + PlatformResult status = ContactUtil::SetStrInRecord( + contacts_record, _contacts_group.ringtone_path, real_path.c_str()); + if (status.IsError()) return status; } + + return PlatformResult(ErrorCode::NO_ERROR); } /** * @brief Fills Person object with values from record * @param[in] contacts_record_h Record which is used to fill Person */ -void ImportPersonFromContactsRecord(contacts_record_h record, - JsonObject* out_ptr) { +PlatformResult ImportPersonFromContactsRecord(contacts_record_h record, + JsonObject* out_ptr) { if (nullptr == record) { LoggerW("Platform person record did not set"); - throw InvalidValuesException("Platform person record did not set"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Platform person record did not set"); } JsonObject& arguments_obj = *out_ptr; int int_value = 0; // id - ContactUtil::GetIntFromRecord(record, _contacts_person.id, &int_value); + PlatformResult status = + ContactUtil::GetIntFromRecord(record, _contacts_person.id, &int_value); + if (status.IsError()) return status; + arguments_obj.insert( std::make_pair("id", JsonValue(std::to_string(int_value)))); char* char_value = nullptr; // displayName - ContactUtil::GetStrFromRecord(record, _contacts_person.display_name, - &char_value); + status = ContactUtil::GetStrFromRecord(record, _contacts_person.display_name, + &char_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair( "displayName", char_value ? JsonValue(char_value) : JsonValue{})); // contactCount - ContactUtil::GetIntFromRecord(record, _contacts_person.link_count, - &int_value); + status = ContactUtil::GetIntFromRecord(record, _contacts_person.link_count, + &int_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair( "contactCount", JsonValue(static_cast(int_value)))); bool bool_value = false; // hasPhoneNumber - ContactUtil::GetBoolFromRecord(record, _contacts_person.has_phonenumber, - &bool_value); + status = ContactUtil::GetBoolFromRecord( + record, _contacts_person.has_phonenumber, &bool_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair("hasPhoneNumber", JsonValue(bool_value))); // hasEmail - ContactUtil::GetBoolFromRecord(record, _contacts_person.has_email, - &bool_value); + status = ContactUtil::GetBoolFromRecord(record, _contacts_person.has_email, + &bool_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair("hasEmail", JsonValue(bool_value))); // isFavorite - ContactUtil::GetBoolFromRecord(record, _contacts_person.is_favorite, - &bool_value); + status = ContactUtil::GetBoolFromRecord(record, _contacts_person.is_favorite, + &bool_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair("isFavorite", JsonValue(bool_value))); // photoURI - ContactUtil::GetStrFromRecord(record, _contacts_person.image_thumbnail_path, - &char_value); + status = ContactUtil::GetStrFromRecord( + record, _contacts_person.image_thumbnail_path, &char_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair( "photoURI", char_value ? JsonValue(char_value) : JsonValue{})); // ringtoneURI - ContactUtil::GetStrFromRecord(record, _contacts_person.ringtone_path, - &char_value); + status = ContactUtil::GetStrFromRecord(record, _contacts_person.ringtone_path, + &char_value); + if (status.IsError()) return status; + arguments_obj.insert(std::make_pair( "ringtoneURI", char_value ? JsonValue(char_value) : JsonValue{})); // displayContactId - ContactUtil::GetIntFromRecord(record, _contacts_person.display_contact_id, - &int_value); + status = ContactUtil::GetIntFromRecord( + record, _contacts_person.display_contact_id, &int_value); + if (status.IsError()) return status; + arguments_obj.insert( std::make_pair("displayContactId", std::to_string(int_value))); + + return PlatformResult(ErrorCode::NO_ERROR); } /** * @brief Updates contacts_record_h with values from Person object * @param[out] contacts_record_h Record which is updated */ -void ExportPersonToContactsRecord(contacts_record_h record, - const JsonObject& args) { +PlatformResult ExportPersonToContactsRecord(contacts_record_h record, + const JsonObject& args) { if (nullptr == record) { LoggerE("Platform person object did not set"); - throw UnknownException("Platform person object did not set"); - } - - ContactUtil::SetBoolInRecord(record, _contacts_person.is_favorite, - FromJson(args, "isFavorite")); - try { - if (!IsNull(args, "photoURI") && - !FromJson(args, "photoURI").empty()) { - ContactUtil::SetStrInRecord( - record, _contacts_person.image_thumbnail_path, - FromJson(args, "photoURI").c_str()); - } else { - ContactUtil::SetStrInRecord(record, _contacts_person.image_thumbnail_path, - ""); + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Platform person object did not set"); + } + + PlatformResult status = ContactUtil::SetBoolInRecord( + record, _contacts_person.is_favorite, FromJson(args, "isFavorite")); + if (status.IsError()) return status; + + if (!IsNull(args, "photoURI") && + !FromJson(args, "photoURI").empty()) { + PlatformResult status = ContactUtil::SetStrInRecord( + record, _contacts_person.image_thumbnail_path, + FromJson(args, "photoURI").c_str()); + if (status.IsError()) { + LoggerE("Try updating read only attribute photoURI"); + return status; } + } else { + // TO DO: fix when photoURI attribute changed from read only to write mode } - catch (const PlatformException& ex) { - LoggerD("Platform field is readonly. %s", ex.message().c_str()); - } + if (!IsNull(args, "ringtoneURI")) { - ContactUtil::SetStrInRecord( + PlatformResult status = ContactUtil::SetStrInRecord( record, _contacts_person.ringtone_path, FromJson(args, "ringtoneURI").c_str()); + if (status.IsError()) return status; } else { - ContactUtil::SetStrInRecord(record, _contacts_person.ringtone_path, ""); + PlatformResult status = + ContactUtil::SetStrInRecord(record, _contacts_person.ringtone_path, ""); + if (status.IsError()) return status; } if (!IsNull(args, "displayContactId")) { - ContactUtil::SetIntInRecord( + PlatformResult status = ContactUtil::SetIntInRecord( record, _contacts_person.display_contact_id, common::stol(FromJson(args, "displayContactId"))); + if (status.IsError()) return status; } + + return PlatformResult(ErrorCode::NO_ERROR); } -void UpdateAdditionalInformation(const ContactsRecordHPtr& contacts_record_ptr, - JsonObject* out_ptr) { +PlatformResult UpdateAdditionalInformation( + const ContactsRecordHPtr& contacts_record_ptr, JsonObject* out_ptr) { JsonObject& out = *out_ptr; int int_value = -1; - ContactUtil::GetIntFromRecord(*contacts_record_ptr, - _contacts_contact.person_id, &int_value); + PlatformResult status = ContactUtil::GetIntFromRecord( + *contacts_record_ptr, _contacts_contact.person_id, &int_value); + if (status.IsError()) return status; + out.insert(std::make_pair("personId", JsonValue{std::to_string(int_value)})); - ContactUtil::GetIntFromRecord(*contacts_record_ptr, - _contacts_contact.address_book_id, &int_value); + status = ContactUtil::GetIntFromRecord( + *contacts_record_ptr, _contacts_contact.address_book_id, &int_value); + if (status.IsError()) return status; + out.insert( std::make_pair("addressBookId", JsonValue{std::to_string(int_value)})); - ContactUtil::GetIntFromRecord(*contacts_record_ptr, - _contacts_contact.changed_time, &int_value); + status = ContactUtil::GetIntFromRecord( + *contacts_record_ptr, _contacts_contact.changed_time, &int_value); + if (status.IsError()) return status; + out.insert( std::make_pair("lastUpdated", JsonValue{static_cast(int_value)})); bool bool_value = false; - ContactUtil::GetBoolFromRecord(*contacts_record_ptr, - _contacts_contact.is_favorite, &bool_value); + status = ContactUtil::GetBoolFromRecord( + *contacts_record_ptr, _contacts_contact.is_favorite, &bool_value); + if (status.IsError()) return status; + out.insert(std::make_pair("isFavorite", JsonValue{bool_value})); + + return PlatformResult(ErrorCode::NO_ERROR); } -void CheckDBConnection() { +PlatformResult CheckDBConnection() { static bool _connected = false; - if (_connected) return; + if (_connected) return PlatformResult(ErrorCode::NO_ERROR); int err = contacts_connect(); if (CONTACTS_ERROR_NONE == err) { @@ -1988,9 +2366,12 @@ void CheckDBConnection() { _connected = true; } else { LoggerE("DB connection error occured: %s", std::to_string(err).c_str()); - throw UnknownException("DB connection error occured: " + - std::to_string(err)); + return PlatformResult( + ErrorCode::UNKNOWN_ERR, + "DB connection error occured: " + std::to_string(err)); } + + return PlatformResult(ErrorCode::NO_ERROR); } } // ContactUtil diff --git a/src/contact/contact_util.h b/src/contact/contact_util.h index c5b363eb..8d5d8745 100644 --- a/src/contact/contact_util.h +++ b/src/contact/contact_util.h @@ -23,6 +23,7 @@ #include #include "common/picojson.h" #include "common/platform_exception.h" +#include "common/platform_result.h" namespace extension { namespace contact { @@ -50,100 +51,95 @@ void ContactsQueryDeleter(contacts_query_h *contacts_query); typedef std::unique_ptr ContactsQueryHPtr; -void ErrorChecker(int err, const char *message); +common::PlatformResult ErrorChecker(int err, const char *message); -void GetStrFromRecord(contacts_record_h record, unsigned int property_id, - char **value); +common::PlatformResult GetStrFromRecord(contacts_record_h record, + unsigned int property_id, char **value); -void GetIntFromRecord(contacts_record_h record, unsigned int property_id, - int *value); +common::PlatformResult GetIntFromRecord(contacts_record_h record, + unsigned int property_id, int *value); -void GetBoolFromRecord(contacts_record_h record, unsigned int property_id, - bool *value); +common::PlatformResult GetBoolFromRecord(contacts_record_h record, + unsigned int property_id, bool *value); -void SetStrInRecord(contacts_record_h record, unsigned int property_id, - const char *value); +common::PlatformResult SetStrInRecord(contacts_record_h record, + unsigned int property_id, + const char *value); -void SetIntInRecord(contacts_record_h record, unsigned int property_id, - int value); +common::PlatformResult SetIntInRecord(contacts_record_h record, + unsigned int property_id, int value); -void SetBoolInRecord(contacts_record_h record, unsigned int property_id, - bool value); +common::PlatformResult SetBoolInRecord(contacts_record_h record, + unsigned int property_id, bool value); -void ClearAllContactRecord(contacts_record_h contacts_record, - unsigned int property_id); +common::PlatformResult ClearAllContactRecord(contacts_record_h contacts_record, + unsigned int property_id); -unsigned int GetNumberOfChildRecord(contacts_record_h contacts_record, - unsigned int property_id); +common::PlatformResult GetNumberOfChildRecord(contacts_record_h contacts_record, + unsigned int property_id, + int *child_count); -void UpdateAdditionalInformation(const ContactsRecordHPtr &contacts_record_ptr, - JsonObject *out); +common::PlatformResult UpdateAdditionalInformation( + const ContactsRecordHPtr &contacts_record_ptr, JsonObject *out); -JsonValue ImportBirthdayFromContactsRecord(contacts_record_h contacts_record, - unsigned int index); -void ExportBirthdayToContactsRecord(contacts_record_h contacts_record, - int date); -bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, - JsonObject *out); -void ExportContactNameToContactsRecord(contacts_record_h contacts_record, - const JsonObject &in); -void ImportContactEmailAddressFromContactsRecord( +common::PlatformResult ImportContactNameFromContactsRecord( + contacts_record_h contacts_record, JsonObject *out, bool *is_contact_name); +common::PlatformResult ExportContactNameToContactsRecord( + contacts_record_h contacts_record, const JsonObject &in); +common::PlatformResult ImportContactEmailAddressFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject *out); -void ExportContactEmailAddressToContactsRecord( +common::PlatformResult ExportContactEmailAddressToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); -void ImportContactAddressFromContactsRecord(contacts_record_h contacts_record, - unsigned int index, - JsonObject *out); -void ExportContactAddressToContactsRecord(contacts_record_h contacts_record, - const JsonObject &in); -void ImportContactPhoneNumberFromContactsRecord( +common::PlatformResult ImportContactAddressFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject *out); -void ExportContactPhoneNumberToContactsRecord(contacts_record_h contacts_record, - const JsonObject &in); -void ImportContactOrganizationFromContactsRecord( +common::PlatformResult ExportContactAddressToContactsRecord( + contacts_record_h contacts_record, const JsonObject &in); +common::PlatformResult ImportContactPhoneNumberFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject *out); -void ExportContactOrganizationToContactsRecord( +common::PlatformResult ExportContactPhoneNumberToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); -void ImportContactWebSiteFromContactsRecord(contacts_record_h contacts_record, - unsigned int index, - JsonObject *out); -void ExportContactWebSiteToContactsRecord(contacts_record_h contacts_record, - const JsonObject &in); -bool ImportContactAnniversariesFromContactsRecord( +common::PlatformResult ImportContactOrganizationFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject *out); -void ExportContactAnniversariesToContactsRecord( +common::PlatformResult ExportContactOrganizationToContactsRecord( + contacts_record_h contacts_record, const JsonObject &in); +common::PlatformResult ImportContactWebSiteFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonObject *out); +common::PlatformResult ExportContactWebSiteToContactsRecord( + contacts_record_h contacts_record, const JsonObject &in); +common::PlatformResult ImportContactAnniversariesFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonObject *out, + bool *ret); +common::PlatformResult ExportContactAnniversariesToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); -void ImportContactRelationshipFromContactsRecord( +common::PlatformResult ImportContactRelationshipFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject *out); -void ExportContactRelationshipToContactsRecord( +common::PlatformResult ExportContactRelationshipToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); -void ImportContactInstantMessengerFromContactsRecord( +common::PlatformResult ImportContactInstantMessengerFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject *out); -void ExportContactInstantMessengerToContactsRecord( +common::PlatformResult ExportContactInstantMessengerToContactsRecord( + contacts_record_h contacts_record, const JsonObject &in); + +common::PlatformResult ImportContactNotesFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonValue *val); +common::PlatformResult ExportNotesToContactsRecord( + contacts_record_h contacts_record, const std::string &value); +common::PlatformResult ImportContactFromContactsRecord( + contacts_record_h contacts_record, JsonObject *out); +common::PlatformResult ExportPersonToContactsRecord(contacts_record_h record, + const JsonObject &args); + +common::PlatformResult ExportContactToContactsRecord( + contacts_record_h contacts_record, const JsonObject &in); +common::PlatformResult ImportContactGroupFromContactsRecord( + contacts_record_h contacts_record, JsonObject *out); +common::PlatformResult ExportContactGroupToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); +common::PlatformResult ImportPersonFromContactsRecord( + contacts_record_h contacts_record, JsonObject *out); -JsonValue ImportContactNotesFromContactsRecord( - contacts_record_h contacts_record, unsigned int index); -JsonValue ImportContactNotesFromContactsRecord( - contacts_record_h contacts_record, unsigned int index); -void ExportNotesToContactsRecord(contacts_record_h contacts_record, - const std::string &value); -void ImportContactFromContactsRecord(contacts_record_h contacts_record, - JsonObject *out); -void ExportPersonToContactsRecord(contacts_record_h record, - const JsonObject &args); - -void ExportContactToContactsRecord(contacts_record_h contacts_record, - const JsonObject &in); -void ImportContactGroupFromContactsRecord(contacts_record_h contacts_record, - JsonObject *out); -void ExportContactGroupToContactsRecord(contacts_record_h contacts_record, - const JsonObject &in); -void ImportPersonFromContactsRecord(contacts_record_h contacts_record, - JsonObject *out); - -void CheckDBConnection(); +common::PlatformResult CheckDBConnection(); } // ContactUtil } // contact diff --git a/src/contact/person.cc b/src/contact/person.cc index 33edf0a8..8c224f3b 100644 --- a/src/contact/person.cc +++ b/src/contact/person.cc @@ -38,8 +38,9 @@ static const PersonPropertyMap personPropertyMap = { {"displayContactId", {_contacts_person.display_contact_id, kPrimitiveTypeId}}, }; -void PersonLink(const JsonObject& args, JsonObject&) { - ContactUtil::CheckDBConnection(); +PlatformResult PersonLink(const JsonObject& args, JsonObject&) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; long id = common::stol(FromJson(args, "id")); long person_id = common::stol(FromJson(args, "person", "id")); @@ -52,16 +53,21 @@ void PersonLink(const JsonObject& args, JsonObject&) { if (CONTACTS_ERROR_NONE != err) { LoggerW("Person was not found, error code: %d", err); - throw common::NotFoundException("Person not found"); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Person not found"); } err = contacts_person_link_person(person_id, id); - ContactUtil::ErrorChecker(err, "Error during executing person link()"); + status = + ContactUtil::ErrorChecker(err, "Error during executing person link()"); + if (status.IsError()) return status; + + return PlatformResult(ErrorCode::NO_ERROR); } -void PersonUnlink(const JsonObject& args, JsonObject& out) { - ContactUtil::CheckDBConnection(); +PlatformResult PersonUnlink(const JsonObject& args, JsonObject& out) { + PlatformResult status = ContactUtil::CheckDBConnection(); + if (status.IsError()) return status; long contact_id = common::stol(FromJson(args, "id")); @@ -73,7 +79,7 @@ void PersonUnlink(const JsonObject& args, JsonObject& out) { contacts_record_destroy(contacts_record, true); contacts_record = nullptr; LoggerW("Contact not found, error code: %d", error_code); - throw common::InvalidValuesException("Contact not found"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Contact not found"); } int contacts_person_id = 0; @@ -82,12 +88,15 @@ void PersonUnlink(const JsonObject& args, JsonObject& out) { contacts_record_destroy(contacts_record, true); contacts_record = nullptr; - ContactUtil::ErrorChecker(error_code, "Contact is not a member of person"); + status = ContactUtil::ErrorChecker(error_code, + "Contact is not a member of person"); + if (status.IsError()) return status; long person_id = common::stol(FromJson(args, "person", "id")); if (contacts_person_id != person_id) { LoggerW("Contact is not a member of person (wrong id's)"); - throw common::InvalidValuesException("Contact is not a member of person"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Contact is not a member of person"); } int new_person_id = 0; @@ -95,7 +104,9 @@ void PersonUnlink(const JsonObject& args, JsonObject& out) { error_code = contacts_person_unlink_contact(person_id, contact_id, &new_person_id); - ContactUtil::ErrorChecker(error_code, "Error during executing unlink()"); + status = + ContactUtil::ErrorChecker(error_code, "Error during executing unlink()"); + if (status.IsError()) return status; error_code = contacts_db_get_record(_contacts_person._uri, new_person_id, &contacts_record); @@ -103,22 +114,30 @@ void PersonUnlink(const JsonObject& args, JsonObject& out) { contacts_record_destroy(contacts_record, true); contacts_record = nullptr; LoggerW("Person not found, error code: %d", error_code); - throw common::UnknownException("Person not found"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Person not found"); } - ContactUtil::ImportPersonFromContactsRecord(contacts_record, &out); + status = ContactUtil::ImportPersonFromContactsRecord(contacts_record, &out); + if (status.IsError()) return status; contacts_record_destroy(contacts_record, true); contacts_record = nullptr; + + return PlatformResult(ErrorCode::NO_ERROR); } -const PersonProperty& PersonPropertyFromString(const std::string& name) { +PlatformResult PersonPropertyFromString(const std::string& name, + PersonProperty *person_prop) { auto iter = personPropertyMap.find(name); if (iter == personPropertyMap.end()) { LoggerE("Invalid property name (not in map): %s", name.c_str()); - throw InvalidValuesException("Invalid property name"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, + "Invalid property name"); } - return iter->second; + (*person_prop).propertyId = iter->second.propertyId; + (*person_prop).type = iter->second.type; + + return PlatformResult(ErrorCode::NO_ERROR); } } // Person diff --git a/src/contact/person.h b/src/contact/person.h index b0bceb48..b5b0a60e 100644 --- a/src/contact/person.h +++ b/src/contact/person.h @@ -33,16 +33,17 @@ enum PrimitiveType { namespace Person { struct PersonProperty { - const unsigned int propertyId; - const PrimitiveType type; + unsigned int propertyId; + PrimitiveType type; }; typedef std::map PersonPropertyMap; -const PersonProperty& PersonPropertyFromString(const std::string& name); +common::PlatformResult PersonPropertyFromString(const std::string& name, + PersonProperty* person_prop); -void PersonLink(const JsonObject& args, JsonObject&); -void PersonUnlink(const JsonObject& args, JsonObject&); +common::PlatformResult PersonLink(const JsonObject& args, JsonObject&); +common::PlatformResult PersonUnlink(const JsonObject& args, JsonObject&); } // Person } // contact -- 2.34.1