Implement error handling for C++ services

Bug: 25615695
Test: unit tests continue to pass.

Change-Id: I24f4f3a5a7b95438ab7daa707d2b4598b33b8302
diff --git a/tests/aidl_test_client.cpp b/tests/aidl_test_client.cpp
index 211d0fa..c3ae443 100644
--- a/tests/aidl_test_client.cpp
+++ b/tests/aidl_test_client.cpp
@@ -35,6 +35,7 @@
 // libbinder:
 using android::getService;
 using android::IBinder;
+using android::binder::Status;
 
 // generated
 using android::aidl::tests::ITestService;
@@ -62,12 +63,13 @@
 
 template <typename T>
 bool RepeatPrimitive(const sp<ITestService>& service,
-                     status_t(ITestService::*func)(T, T*),
+                     Status(ITestService::*func)(T, T*),
                      const T input) {
   T reply;
-  status_t status = (*service.*func)(input, &reply);
-  if (status != OK || input != reply) {
-    cerr << "Failed to repeat primitive. status=" << status << "." << endl;
+  Status status = (*service.*func)(input, &reply);
+  if (!status.isOk() || input != reply) {
+    cerr << "Failed to repeat primitive. status=" << status.toString8()
+         << "." << endl;
     return false;
   }
   return true;
@@ -93,11 +95,11 @@
   };
   for (const auto& input : inputs) {
     String16 reply;
-    status_t status = s->RepeatString(input, &reply);
-    if (status != OK || input != reply) {
+    Status status = s->RepeatString(input, &reply);
+    if (!status.isOk() || input != reply) {
       cerr << "Failed while requesting service to repeat String16=\""
            << String8(input).string()
-           << "\". Got status=" << status << endl;
+           << "\". Got status=" << status.toString8() << endl;
       return false;
     }
   }
@@ -106,15 +108,16 @@
 
 template <typename T>
 bool ReverseArray(const sp<ITestService>& service,
-                  status_t(ITestService::*func)(const vector<T>&,
-                                                vector<T>*,
-                                                vector<T>*),
+                  Status(ITestService::*func)(const vector<T>&,
+                                              vector<T>*,
+                                              vector<T>*),
                   vector<T> input) {
   vector<T> actual_reversed;
   vector<T> actual_repeated;
-  status_t status = (*service.*func)(input, &actual_repeated, &actual_reversed);
-  if (status != OK) {
-    cerr << "Failed to repeat array. status=" << status << "." << endl;
+  Status status = (*service.*func)(input, &actual_repeated, &actual_reversed);
+  if (!status.isOk()) {
+    cerr << "Failed to repeat array. status=" << status.toString8() << "."
+         << endl;
     return false;
   }
   if (input != actual_repeated) {
@@ -168,7 +171,7 @@
 }
 
 bool ConfirmReverseBinderLists(const sp<ITestService>& s) {
-  status_t status;
+  Status status;
   cout << "Confirming passing and returning List<T> works with binders." << endl;
 
   vector<String16> names = {
@@ -183,7 +186,7 @@
     sp<INamedCallback> got;
 
     status = s->GetOtherTestService(names[i], &got);
-    if (status != OK) {
+    if (!status.isOk()) {
       cerr << "Could not retrieve service for test." << endl;
       return false;
     }
@@ -195,6 +198,9 @@
   vector<sp<IBinder>> reversed;
 
   status = s->ReverseNamedCallbackList(input, &output, &reversed);
+  if (!status.isOk()) {
+    cerr << "Failed to reverse named callback list." << endl;
+  }
 
   if (output.size() != 3) {
     cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl;
@@ -212,7 +218,7 @@
         android::interface_cast<INamedCallback>(output[i]);
     status = named_callback->GetName(&ret);
 
-    if (status != OK) {
+    if (!status.isOk()) {
       cerr << "Could not query INamedCallback from output" << endl;
       return false;
     }
@@ -229,7 +235,7 @@
         android::interface_cast<INamedCallback>(reversed[i]);
     status = named_callback->GetName(&ret);
 
-    if (status != OK) {
+    if (!status.isOk()) {
       cerr << "Could not query INamedCallback from reversed output" << endl;
       return false;
     }
diff --git a/tests/aidl_test_service.cpp b/tests/aidl_test_service.cpp
index b41cac7..5c3d672 100644
--- a/tests/aidl_test_service.cpp
+++ b/tests/aidl_test_service.cpp
@@ -43,7 +43,6 @@
 using android::LooperCallback;
 using android::OK;
 using android::sp;
-using android::status_t;
 using android::String16;
 
 // libbinder:
@@ -53,6 +52,7 @@
 using android::IPCThreadState;
 using android::Parcel;
 using android::ProcessState;
+using android::binder::Status;
 
 // Generated code:
 using android::aidl::tests::BnTestService;
@@ -82,9 +82,9 @@
  public:
   NamedCallback(String16 name) : name_(name) {}
 
-  status_t GetName(String16* ret) {
+  Status GetName(String16* ret) {
     *ret = name_;
-    return OK;
+    return Status::ok();
   }
 
  private:
@@ -135,101 +135,101 @@
     ALOGI("Repeating token %s", token_str.str().c_str());
   }
 
-  status_t RepeatBoolean(bool token, bool* _aidl_return) override {
+  Status RepeatBoolean(bool token, bool* _aidl_return) override {
     LogRepeatedToken(token ? 1 : 0);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatByte(int8_t token, int8_t* _aidl_return) override {
+  Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
     LogRepeatedToken(token);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatChar(char16_t token, char16_t* _aidl_return) override {
+  Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
     LogRepeatedStringToken(String16(&token, 1));
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatInt(int32_t token, int32_t* _aidl_return) override {
+  Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
     LogRepeatedToken(token);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatLong(int64_t token, int64_t* _aidl_return) override {
+  Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
     LogRepeatedToken(token);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatFloat(float token, float* _aidl_return) override {
+  Status RepeatFloat(float token, float* _aidl_return) override {
     LogRepeatedToken(token);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatDouble(double token, double* _aidl_return) override {
+  Status RepeatDouble(double token, double* _aidl_return) override {
     LogRepeatedToken(token);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
-  status_t RepeatString(
+  Status RepeatString(
       const String16& token, String16* _aidl_return) override {
     LogRepeatedStringToken(token);
     *_aidl_return = token;
-    return OK;
+    return Status::ok();
   }
 
   template<typename T>
-  status_t ReverseArray(const vector<T>& input,
+  Status ReverseArray(const vector<T>& input,
                         vector<T>* repeated,
                         vector<T>* _aidl_return) {
     ALOGI("Reversing array of length %zu", input.size());
     *repeated = input;
     *_aidl_return = input;
     std::reverse(_aidl_return->begin(), _aidl_return->end());
-    return OK;
+    return Status::ok();
   }
 
-  status_t ReverseBoolean(const vector<bool>& input,
+  Status ReverseBoolean(const vector<bool>& input,
                           vector<bool>* repeated,
                           vector<bool>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseByte(const vector<int8_t>& input,
+  Status ReverseByte(const vector<int8_t>& input,
                        vector<int8_t>* repeated,
                        vector<int8_t>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseChar(const vector<char16_t>& input,
+  Status ReverseChar(const vector<char16_t>& input,
                        vector<char16_t>* repeated,
                        vector<char16_t>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseInt(const vector<int32_t>& input,
+  Status ReverseInt(const vector<int32_t>& input,
                       vector<int32_t>* repeated,
                       vector<int32_t>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseLong(const vector<int64_t>& input,
+  Status ReverseLong(const vector<int64_t>& input,
                        vector<int64_t>* repeated,
                        vector<int64_t>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseFloat(const vector<float>& input,
+  Status ReverseFloat(const vector<float>& input,
                         vector<float>* repeated,
                         vector<float>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseDouble(const vector<double>& input,
+  Status ReverseDouble(const vector<double>& input,
                          vector<double>* repeated,
                          vector<double>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
-  status_t ReverseString(const vector<String16>& input,
+  Status ReverseString(const vector<String16>& input,
                          vector<String16>* repeated,
                          vector<String16>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
 
-  status_t GetOtherTestService(const String16& name,
+  Status GetOtherTestService(const String16& name,
                                sp<INamedCallback>* returned_service) override {
     if (service_map_.find(name) == service_map_.end()) {
       sp<INamedCallback> new_item(new NamedCallback(name));
@@ -237,28 +237,28 @@
     }
 
     *returned_service = service_map_[name];
-    return OK;
+    return Status::ok();
   }
 
-  status_t VerifyName(const sp<INamedCallback>& service, const String16& name,
+  Status VerifyName(const sp<INamedCallback>& service, const String16& name,
                       bool* returned_value) override {
     String16 foundName;
-    status_t err = service->GetName(&foundName);
+    Status status = service->GetName(&foundName);
 
-    if (err == OK) {
+    if (status.isOk()) {
       *returned_value = foundName == name;
     }
 
-    return err;
+    return status;
   }
 
-  status_t ReverseStringList(const vector<String16>& input,
+  Status ReverseStringList(const vector<String16>& input,
                              vector<String16>* repeated,
                              vector<String16>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
   }
 
-  status_t ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
+  Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
                                     vector<sp<IBinder>>* repeated,
                                     vector<sp<IBinder>>* _aidl_return) override {
     return ReverseArray(input, repeated, _aidl_return);
diff --git a/tests/test_data_ping_responder.cpp b/tests/test_data_ping_responder.cpp
index 0225f8b..9b79c96 100644
--- a/tests/test_data_ping_responder.cpp
+++ b/tests/test_data_ping_responder.cpp
@@ -81,31 +81,37 @@
     : BpInterface<IPingResponder>(impl){
 }
 
-android::status_t BpPingResponder::Ping(int32_t token, int32_t* _aidl_return) {
+android::binder::Status BpPingResponder::Ping(int32_t token, int32_t* _aidl_return) {
 android::Parcel data;
 android::Parcel reply;
 android::status_t status;
+android::binder::Status _aidl_status;
 status = data.writeInterfaceToken(getInterfaceDescriptor());
 if (((status) != (android::OK))) {
-return status;
+goto error;
 }
 status = data.writeInt32(token);
 if (((status) != (android::OK))) {
-return status;
+goto error;
 }
 status = remote()->transact(IPingResponder::PING, data, &reply);
 if (((status) != (android::OK))) {
-return status;
+goto error;
 }
-if (reply.readExceptionCode()) {
-status = android::FAILED_TRANSACTION;
-return status;
+status = _aidl_status.readFromParcel(reply);
+if (((status) != (android::OK))) {
+goto error;
+}
+if (!_aidl_status.isOk()) {
+return _aidl_status;
 }
 status = reply.readInt32(_aidl_return);
 if (((status) != (android::OK))) {
-return status;
+goto error;
 }
-return status;
+error:
+_aidl_status.setFromStatusT(status);
+return _aidl_status;
 }
 
 }  // namespace os
@@ -133,12 +139,12 @@
 if (((status) != (android::OK))) {
 break;
 }
-status = Ping(in_token, &_aidl_return);
+android::binder::Status _aidl_status(Ping(in_token, &_aidl_return));
+status = _aidl_status.writeToParcel(reply);
 if (((status) != (android::OK))) {
 break;
 }
-status = reply->writeNoException();
-if (((status) != (android::OK))) {
+if (!_aidl_status.isOk()) {
 break;
 }
 status = reply->writeInt32(_aidl_return);
@@ -153,6 +159,9 @@
 }
 break;
 }
+if (status == android::UNEXPECTED_NULL) {
+status = android::binder::Status::fromExceptionCode(android::binder::Status::EX_NULL_POINTER).writeToParcel(reply);
+}
 return status;
 }
 
@@ -167,6 +176,7 @@
 
 #include <binder/IBinder.h>
 #include <binder/IInterface.h>
+#include <binder/Status.h>
 #include <cstdint>
 #include <utils/StrongPointer.h>
 
@@ -177,7 +187,7 @@
 class IPingResponder : public android::IInterface {
 public:
 DECLARE_META_INTERFACE(PingResponder);
-virtual android::status_t Ping(int32_t token, int32_t* _aidl_return) = 0;
+virtual android::binder::Status Ping(int32_t token, int32_t* _aidl_return) = 0;
 enum Call {
   PING = android::IBinder::FIRST_CALL_TRANSACTION + 0,
 };
@@ -206,7 +216,7 @@
 public:
 explicit BpPingResponder(const android::sp<android::IBinder>& impl);
 virtual ~BpPingResponder() = default;
-android::status_t Ping(int32_t token, int32_t* _aidl_return) override;
+android::binder::Status Ping(int32_t token, int32_t* _aidl_return) override;
 };  // class BpPingResponder
 
 }  // namespace os