Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4e0dcac2 authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

A simple error handling library

This library introduces the Result type ("value or status_t") as
well as useful macros for error handling, using well-known
patterns from the Google codebase.

Test: atest --host libexpectedutils_test
Change-Id: I7c2cf8432983075f5acaad17ff947ca1e7bb88f1
parent 69b23f10
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_av_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_av_license"],
}

cc_library_headers {
    name: "libexpectedutils_headers",
    host_supported: true,
    vendor_available: true,
    min_sdk_version: "29",
    export_include_dirs: [
        "include",
    ],
    header_libs: [
        "libbase_headers",
        "libutils_headers",
    ],
    export_header_lib_headers: [
        "libbase_headers",
        "libutils_headers",
    ],
    apex_available: [
        "//apex_available:platform",
        "com.android.bluetooth",
        "com.android.media",
        "com.android.media.swcodec",
    ],
}

cc_test_host {
    name: "libexpectedutils_test",
    srcs: [
        "expected_utils_test.cpp",
    ],
    shared_libs: [
        "liblog",
    ],
    header_libs: [
        "libexpectedutils_headers",
    ],
}

cc_library_headers {
    name: "liberror_headers",
    host_supported: true,
    vendor_available: true,
    min_sdk_version: "29",
    apex_available: [
        "//apex_available:platform",
        "com.android.bluetooth",
        "com.android.media",
        "com.android.media.swcodec",
    ],
    export_include_dirs: [
        "include",
    ],
    header_libs: [
        "libexpectedutils_headers",
    ],
    export_header_lib_headers: [
        "libexpectedutils_headers",
    ],
}
+157 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <error/expected_utils.h>
#include <gtest/gtest.h>

#define LOG_TAG "Result-test"

namespace android {
namespace foo {

class Value {
  public:
    explicit Value(int i) : mInt(i) {}
    Value(const Value&) = delete;
    Value(Value&&) = default;

    operator int() const { return mInt; }

  private:
    const int mInt;
};

class Status {
  public:
    explicit Status(int i) : mInt(i) {}
    Status(const Status&) = delete;
    Status(Status&&) = default;

    operator int() const { return mInt; }

  private:
    const int mInt;
};

bool errorIsOk(const Status& e) {
    return e == 0;
}

std::string errorToString(const Status& e) {
    std::ostringstream str;
    str << e;
    return str.str();
}

using Result = base::expected<Value, Status>;

}  // namespace foo

namespace {

using foo::Result;
using foo::Status;
using foo::Value;

TEST(Result, ValueOrReturnSuccess) {
    Result result = []() -> Result {
        Value intermediate = VALUE_OR_RETURN(Result(Value(3)));
        return Value(intermediate + 1);
    }();
    ASSERT_TRUE(result.ok());
    EXPECT_EQ(4, result.value());
}

TEST(Result, ValueOrReturnFailure) {
    Result result = []() -> Result {
        Value intermediate = VALUE_OR_RETURN(Result(base::unexpected(Status(2))));
        return Value(intermediate + 1);
    }();
    ASSERT_FALSE(result.ok());
    EXPECT_EQ(2, result.error());
}

TEST(Result, ValueOrReturnStatusSuccess) {
    Status status = []() -> Status {
        Value intermediate = VALUE_OR_RETURN_STATUS(Result(Value(3)));
        (void) intermediate;
        return Status(0);
    }();
    EXPECT_EQ(0, status);
}

TEST(Result, ValueOrReturnStatusFailure) {
    Status status = []() -> Status {
        Value intermediate = VALUE_OR_RETURN_STATUS(Result(base::unexpected(Status(1))));
        (void) intermediate;
        return Status(0);
    }();
    EXPECT_EQ(1, status);
}

TEST(Result, ReturnIfErrorSuccess) {
    Result result = []() -> Result {
        RETURN_IF_ERROR(Status(0));
        return Value(5);
    }();
    ASSERT_TRUE(result.ok());
    EXPECT_EQ(5, result.value());
}

TEST(Result, ReturnIfErrorFailure) {
    Result result = []() -> Result {
        RETURN_IF_ERROR(Status(4));
        return Value(5);
    }();
    ASSERT_FALSE(result.ok());
    EXPECT_EQ(4, result.error());
}

TEST(Result, ReturnStatusIfErrorSuccess) {
    Status status = []() -> Status {
        RETURN_STATUS_IF_ERROR(Status(0));
        return Status(7);
    }();
    EXPECT_EQ(7, status);
}

TEST(Result, ReturnStatusIfErrorFailure) {
    Status status = []() -> Status {
        RETURN_STATUS_IF_ERROR(Status(3));
        return Status(0);
    }();
    EXPECT_EQ(3, status);
}

TEST(Result, ValueOrFatalSuccess) {
    Value value = VALUE_OR_FATAL(Result(Value(7)));
    EXPECT_EQ(7, value);
}

TEST(Result, ValueOrFatalFailure) {
    EXPECT_DEATH(VALUE_OR_FATAL(Result(base::unexpected(Status(3)))), "");
}

TEST(Result, FatalIfErrorSuccess) {
    FATAL_IF_ERROR(Status(0));
}

TEST(Result, FatalIfErrorFailure) {
    EXPECT_DEATH(FATAL_IF_ERROR(Status(3)), "");
}

}  // namespace
}  // namespace android
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <error/expected_utils.h>
#include <utils/Errors.h>

namespace android {
namespace error {

/**
 * A convenience short-hand for base::expected, where the error type is a status_t.
 */
template <typename T>
using Result = base::expected<T, status_t>;

}  // namespace error
}  // namespace android

// Below are the implementations of errorIsOk and errorToString for status_t .
// This allows status_t to be used in conjunction with the expected_utils.h macros.
// Unfortuantely, since status_t is merely a typedef for int rather than a unique type, we have to
// overload these methods for any int, and do so in the global namespace for ADL to work.

inline bool errorIsOk(int status) {
    return status == android::OK;
}

inline std::string errorToString(int status) {
    return android::statusToString(status);
}
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <sstream>

#include <android-base/expected.h>
#include <log/log_main.h>

/**
 * Useful macros for working with status codes and base::expected.
 *
 * These macros facilitate various kinds of strategies for reduction of error-handling-related
 * boilerplate. They can be can be classified by the following criteria:
 * - Whether the argument is a standalone status code vs. base::expected (status or value). In the
 *   latter case, the macro will evaluate to the contained value in the case of success.
 * - Whether to FATAL or return in response to an error.
 *   - In the latter case, whether the enclosing function returns a status code or a base::expected.
 *
 * The table below summarizes which macro serves which case, based on those criteria:
 * +--------------------+------------------+------------------------------------------------------+
 * |     Error response | FATAL            | Early return                                         |
 * |                    |                  +---------------------------+--------------------------+
 * | Expression type    |                  | Function returns expected | Function returns status  |
 * +--------------------+------------------+---------------------------+--------------------------+
 * | status code        | FATAL_IF_ERROR() | RETURN_IF_ERROR()         | RETURN_STATUS_IF_ERROR() |
 * +--------------------+------------------+---------------------------+--------------------------+
 * | expected           | VALUE_OR_FATAL() | VALUE_OR_RETURN()         | VALUE_OR_RETURN_STATUS() |
 * +--------------------+------------------+---------------------------+--------------------------+
 *
 * All macros expect that:
 * - The error type and value value type are movable.
 * - The macro argument can be assigned to a variable using `auto x = (exp)`.
 * - The expression errorIsOk(e) for the error type evaluatea to a bool which is true iff the
 *   status is considered success.
 * - The expression errorToString(e) for a given error type evaluated to a std::string containing a
 *   human-readable version of the status.
 */

#define VALUE_OR_RETURN(exp)                                                         \
    ({                                                                               \
        auto _tmp = (exp);                                                           \
        if (!_tmp.ok()) return ::android::base::unexpected(std::move(_tmp.error())); \
        std::move(_tmp.value());                                                     \
    })

#define VALUE_OR_RETURN_STATUS(exp)                     \
    ({                                                  \
        auto _tmp = (exp);                              \
        if (!_tmp.ok()) return std::move(_tmp.error()); \
        std::move(_tmp.value());                        \
    })

#define VALUE_OR_FATAL(exp)                                                                       \
    ({                                                                                            \
        auto _tmp = (exp);                                                                        \
        LOG_ALWAYS_FATAL_IF(!_tmp.ok(), "Function: %s Line: %d Failed result (%s)", __FUNCTION__, \
                            __LINE__, errorToString(_tmp.error()).c_str());                       \
        std::move(_tmp.value());                                                                  \
    })

#define RETURN_IF_ERROR(exp) \
    if (auto _tmp = (exp); !errorIsOk(_tmp)) return ::android::base::unexpected(std::move(_tmp));

#define RETURN_STATUS_IF_ERROR(exp) \
    if (auto _tmp = (exp); !errorIsOk(_tmp)) return _tmp;

#define FATAL_IF_ERROR(exp)                                                                \
    {                                                                                      \
        auto _tmp = (exp);                                                                 \
        LOG_ALWAYS_FATAL_IF(!errorIsOk(_tmp), "Function: %s Line: %d Failed result: (%s)", \
                            __FUNCTION__, __LINE__, errorToString(_tmp).c_str());         \
    }