blob: 26d8b9a05540f7793237c198a9352befb8e44931 [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
Peter Qiu93b243d2015-06-26 11:05:23 -07008#include <memory>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <string>
10
Paul Stewart34f424e2015-01-16 15:30:20 -080011#include <base/location.h>
Ben Chancc67c522014-09-03 07:19:18 -070012#include <base/macros.h>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070013
14namespace DBus {
15class Error;
16} // namespace DBus
17
Peter Qiu93b243d2015-06-26 11:05:23 -070018namespace chromeos {
19class Error;
20using ErrorPtr = std::unique_ptr<Error>;
21} // namespace chromeos
22
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023namespace shill {
24
25class Error {
26 public:
27 enum Type {
Darin Petkove4c0ace2011-08-24 10:32:46 -070028 kSuccess = 0, // No error.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050029 kOperationFailed, // failure, otherwise unspecified
Darin Petkove4c0ace2011-08-24 10:32:46 -070030 kAlreadyConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070031 kAlreadyExists,
Paul Stewart71b9ed52014-01-29 08:53:06 -080032 kIncorrectPin,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033 kInProgress,
34 kInternalError,
Paul Stewart71b9ed52014-01-29 08:53:06 -080035 kInvalidApn,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036 kInvalidArguments,
37 kInvalidNetworkName,
38 kInvalidPassphrase,
39 kInvalidProperty,
Darin Petkove4c0ace2011-08-24 10:32:46 -070040 kNoCarrier,
41 kNotConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070042 kNotFound,
Darin Petkove4c0ace2011-08-24 10:32:46 -070043 kNotImplemented,
44 kNotOnHomeNetwork,
45 kNotRegistered,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070046 kNotSupported,
Darin Petkove4c0ace2011-08-24 10:32:46 -070047 kOperationAborted,
Paul Stewart71b9ed52014-01-29 08:53:06 -080048 kOperationInitiated,
Darin Petkove4c0ace2011-08-24 10:32:46 -070049 kOperationTimeout,
50 kPassphraseRequired,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070051 kPermissionDenied,
Paul Stewart71b9ed52014-01-29 08:53:06 -080052 kPinBlocked,
53 kPinRequired,
54 kWrongState,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070055 kNumErrors
56 };
57
Darin Petkove4c0ace2011-08-24 10:32:46 -070058 Error(); // Success by default.
59 explicit Error(Type type); // Uses the default message for |type|.
Paul Stewarta794cd62015-06-16 13:13:10 -070060 Error(Type type, const std::string& message);
Darin Petkove4c0ace2011-08-24 10:32:46 -070061 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070062
Darin Petkove4c0ace2011-08-24 10:32:46 -070063 void Populate(Type type); // Uses the default message for |type|.
Paul Stewarta794cd62015-06-16 13:13:10 -070064 void Populate(Type type, const std::string& message);
Peter Qiu93b243d2015-06-26 11:05:23 -070065 void Populate(Type type,
66 const std::string& message,
67 const tracked_objects::Location& location);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070068
Gaurav Shah1b7a6162011-11-09 11:41:01 -080069 void Reset();
70
Paul Stewarta794cd62015-06-16 13:13:10 -070071 void CopyFrom(const Error& error);
Darin Petkove5bc2cb2011-12-07 14:47:32 +010072
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073 // Sets the DBus |error| and returns true if Error represents failure.
74 // Leaves |error| unchanged, and returns false, otherwise.
Paul Stewarta794cd62015-06-16 13:13:10 -070075 bool ToDBusError(::DBus::Error* error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070076
Peter Qiu93b243d2015-06-26 11:05:23 -070077 // Sets the Chromeos |error| and returns true if Error represents failure.
78 // Leaves error unchanged, and returns false otherwise.
79 bool ToChromeosError(chromeos::ErrorPtr* error) const;
80
Darin Petkove4c0ace2011-08-24 10:32:46 -070081 Type type() const { return type_; }
Paul Stewarta794cd62015-06-16 13:13:10 -070082 const std::string& message() const { return message_; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070083
84 bool IsSuccess() const { return type_ == kSuccess; }
Eric Shienbrood9a245532012-03-07 14:20:39 -050085 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
86 bool IsOngoing() const { return type_ == kOperationInitiated; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070087
Paul Stewart71b9ed52014-01-29 08:53:06 -080088 static std::string GetDBusResult(Type type);
Darin Petkove4c0ace2011-08-24 10:32:46 -070089 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070090
Paul Stewart34f424e2015-01-16 15:30:20 -080091 // Log an error message from |from_here|. If |error| is non-NULL, also
92 // populate it.
Paul Stewarta794cd62015-06-16 13:13:10 -070093 static void PopulateAndLog(const tracked_objects::Location& from_here,
94 Error* error, Type type,
95 const std::string& message);
Paul Stewartbe005172011-11-02 18:10:29 -070096
Chris Masone8fe2c7e2011-06-09 15:51:19 -070097 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070098 struct Info {
Paul Stewarta794cd62015-06-16 13:13:10 -070099 const char* dbus_result; // Error type name.
100 const char* message; // Default Error type message.
Darin Petkove4c0ace2011-08-24 10:32:46 -0700101 };
102
103 static const Info kInfos[kNumErrors];
Peter Qiu93b243d2015-06-26 11:05:23 -0700104 static const Info kChromeosInfos[kNumErrors];
105 static const char kChromeosErrorDomain[];
Darin Petkove4c0ace2011-08-24 10:32:46 -0700106
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700107 Type type_;
108 std::string message_;
Peter Qiu93b243d2015-06-26 11:05:23 -0700109 tracked_objects::Location location_;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700110
111 DISALLOW_COPY_AND_ASSIGN(Error);
112};
113
114} // namespace shill
115
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500116// stream operator provided to facilitate logging
Paul Stewarta794cd62015-06-16 13:13:10 -0700117std::ostream& operator<<(std::ostream& stream, const shill::Error& error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500118
Ben Chanc45688b2014-07-02 23:50:45 -0700119#endif // SHILL_ERROR_H_