Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2012 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #ifndef SHILL_ERROR_H_
     18 #define SHILL_ERROR_H_
     19 
     20 #include <memory>
     21 #include <string>
     22 
     23 #include <base/location.h>
     24 #include <base/macros.h>
     25 
     26 namespace DBus {
     27 class Error;
     28 }  // namespace DBus
     29 
     30 namespace brillo {
     31 class Error;
     32 using ErrorPtr = std::unique_ptr<Error>;
     33 }  // namespace brillo
     34 
     35 namespace shill {
     36 
     37 class Error {
     38  public:
     39   enum Type {
     40     kSuccess = 0,  // No error.
     41     kOperationFailed,  // failure, otherwise unspecified
     42     kAlreadyConnected,
     43     kAlreadyExists,
     44     kIncorrectPin,
     45     kInProgress,
     46     kInternalError,
     47     kInvalidApn,
     48     kInvalidArguments,
     49     kInvalidNetworkName,
     50     kInvalidPassphrase,
     51     kInvalidProperty,
     52     kNoCarrier,
     53     kNotConnected,
     54     kNotFound,
     55     kNotImplemented,
     56     kNotOnHomeNetwork,
     57     kNotRegistered,
     58     kNotSupported,
     59     kOperationAborted,
     60     kOperationInitiated,
     61     kOperationTimeout,
     62     kPassphraseRequired,
     63     kPermissionDenied,
     64     kPinBlocked,
     65     kPinRequired,
     66     kWrongState,
     67     kNumErrors
     68   };
     69 
     70   Error();  // Success by default.
     71   explicit Error(Type type);  // Uses the default message for |type|.
     72   Error(Type type, const std::string& message);
     73   ~Error();
     74 
     75   void Populate(Type type);  // Uses the default message for |type|.
     76   void Populate(Type type, const std::string& message);
     77   void Populate(Type type,
     78                 const std::string& message,
     79                 const tracked_objects::Location& location);
     80 
     81   void Reset();
     82 
     83   void CopyFrom(const Error& error);
     84 
     85   // Sets the Chromeos |error| and returns true if Error represents failure.
     86   // Leaves error unchanged, and returns false otherwise.
     87   bool ToChromeosError(brillo::ErrorPtr* error) const;
     88 
     89   Type type() const { return type_; }
     90   const std::string& message() const { return message_; }
     91 
     92   bool IsSuccess() const { return type_ == kSuccess; }
     93   bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
     94   bool IsOngoing() const { return type_ == kOperationInitiated; }
     95 
     96   static std::string GetDBusResult(Type type);
     97   static std::string GetDefaultMessage(Type type);
     98 
     99   // Log an error message from |from_here|.  If |error| is non-NULL, also
    100   // populate it.
    101   static void PopulateAndLog(const tracked_objects::Location& from_here,
    102                              Error* error, Type type,
    103                              const std::string& message);
    104 
    105  private:
    106   struct Info {
    107     const char* dbus_result;  // Error type name.
    108     const char* message;  // Default Error type message.
    109   };
    110 
    111   static const Info kInfos[kNumErrors];
    112 
    113   Type type_;
    114   std::string message_;
    115   tracked_objects::Location location_;
    116 
    117   DISALLOW_COPY_AND_ASSIGN(Error);
    118 };
    119 
    120 }  // namespace shill
    121 
    122 // stream operator provided to facilitate logging
    123 std::ostream& operator<<(std::ostream& stream, const shill::Error& error);
    124 
    125 #endif  // SHILL_ERROR_H_
    126