blob: 13aca0b132f0824573f2ec11549010414580cabf [file] [log] [blame]
Eric Shienbrood5de44ab2011-12-05 10:46:27 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone8fe2c7e2011-06-09 15:51:19 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_ERROR_H_
6#define SHILL_ERROR_H_
Chris Masone8fe2c7e2011-06-09 15:51:19 -07007
8#include <string>
9
Paul Stewart34f424e2015-01-16 15:30:20 -080010#include <base/location.h>
Ben Chancc67c522014-09-03 07:19:18 -070011#include <base/macros.h>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070012
13namespace DBus {
14class Error;
15} // namespace DBus
16
17namespace shill {
18
19class Error {
20 public:
21 enum Type {
Darin Petkove4c0ace2011-08-24 10:32:46 -070022 kSuccess = 0, // No error.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050023 kOperationFailed, // failure, otherwise unspecified
Darin Petkove4c0ace2011-08-24 10:32:46 -070024 kAlreadyConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025 kAlreadyExists,
Paul Stewart71b9ed52014-01-29 08:53:06 -080026 kIncorrectPin,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070027 kInProgress,
28 kInternalError,
Paul Stewart71b9ed52014-01-29 08:53:06 -080029 kInvalidApn,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070030 kInvalidArguments,
31 kInvalidNetworkName,
32 kInvalidPassphrase,
33 kInvalidProperty,
Darin Petkove4c0ace2011-08-24 10:32:46 -070034 kNoCarrier,
35 kNotConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036 kNotFound,
Darin Petkove4c0ace2011-08-24 10:32:46 -070037 kNotImplemented,
38 kNotOnHomeNetwork,
39 kNotRegistered,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070040 kNotSupported,
Darin Petkove4c0ace2011-08-24 10:32:46 -070041 kOperationAborted,
Paul Stewart71b9ed52014-01-29 08:53:06 -080042 kOperationInitiated,
Darin Petkove4c0ace2011-08-24 10:32:46 -070043 kOperationTimeout,
44 kPassphraseRequired,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070045 kPermissionDenied,
Paul Stewart71b9ed52014-01-29 08:53:06 -080046 kPinBlocked,
47 kPinRequired,
48 kWrongState,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070049 kNumErrors
50 };
51
Darin Petkove4c0ace2011-08-24 10:32:46 -070052 Error(); // Success by default.
53 explicit Error(Type type); // Uses the default message for |type|.
Paul Stewarta794cd62015-06-16 13:13:10 -070054 Error(Type type, const std::string& message);
Darin Petkove4c0ace2011-08-24 10:32:46 -070055 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070056
Darin Petkove4c0ace2011-08-24 10:32:46 -070057 void Populate(Type type); // Uses the default message for |type|.
Paul Stewarta794cd62015-06-16 13:13:10 -070058 void Populate(Type type, const std::string& message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070059
Gaurav Shah1b7a6162011-11-09 11:41:01 -080060 void Reset();
61
Paul Stewarta794cd62015-06-16 13:13:10 -070062 void CopyFrom(const Error& error);
Darin Petkove5bc2cb2011-12-07 14:47:32 +010063
mukesh agrawal7a4e4002011-09-06 11:26:05 -070064 // Sets the DBus |error| and returns true if Error represents failure.
65 // Leaves |error| unchanged, and returns false, otherwise.
Paul Stewarta794cd62015-06-16 13:13:10 -070066 bool ToDBusError(::DBus::Error* error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070067
68 Type type() const { return type_; }
Paul Stewarta794cd62015-06-16 13:13:10 -070069 const std::string& message() const { return message_; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070070
71 bool IsSuccess() const { return type_ == kSuccess; }
Eric Shienbrood9a245532012-03-07 14:20:39 -050072 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
73 bool IsOngoing() const { return type_ == kOperationInitiated; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070074
Paul Stewart71b9ed52014-01-29 08:53:06 -080075 static std::string GetDBusResult(Type type);
Darin Petkove4c0ace2011-08-24 10:32:46 -070076 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070077
Paul Stewart34f424e2015-01-16 15:30:20 -080078 // Log an error message from |from_here|. If |error| is non-NULL, also
79 // populate it.
Paul Stewarta794cd62015-06-16 13:13:10 -070080 static void PopulateAndLog(const tracked_objects::Location& from_here,
81 Error* error, Type type,
82 const std::string& message);
Paul Stewartbe005172011-11-02 18:10:29 -070083
Chris Masone8fe2c7e2011-06-09 15:51:19 -070084 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070085 struct Info {
Paul Stewarta794cd62015-06-16 13:13:10 -070086 const char* dbus_result; // Error type name.
87 const char* message; // Default Error type message.
Darin Petkove4c0ace2011-08-24 10:32:46 -070088 };
89
90 static const Info kInfos[kNumErrors];
Darin Petkove4c0ace2011-08-24 10:32:46 -070091
Chris Masone8fe2c7e2011-06-09 15:51:19 -070092 Type type_;
93 std::string message_;
94
95 DISALLOW_COPY_AND_ASSIGN(Error);
96};
97
98} // namespace shill
99
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500100// stream operator provided to facilitate logging
Paul Stewarta794cd62015-06-16 13:13:10 -0700101std::ostream& operator<<(std::ostream& stream, const shill::Error& error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500102
Ben Chanc45688b2014-07-02 23:50:45 -0700103#endif // SHILL_ERROR_H_