blob: d75d244d5dd6551a7a7da8b79acf05253bcec8b3 [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
Peter Qiu93b243d2015-06-26 11:05:23 -070073 // Sets the Chromeos |error| and returns true if Error represents failure.
74 // Leaves error unchanged, and returns false otherwise.
75 bool ToChromeosError(chromeos::ErrorPtr* error) const;
76
Darin Petkove4c0ace2011-08-24 10:32:46 -070077 Type type() const { return type_; }
Paul Stewarta794cd62015-06-16 13:13:10 -070078 const std::string& message() const { return message_; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070079
80 bool IsSuccess() const { return type_ == kSuccess; }
Eric Shienbrood9a245532012-03-07 14:20:39 -050081 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
82 bool IsOngoing() const { return type_ == kOperationInitiated; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070083
Paul Stewart71b9ed52014-01-29 08:53:06 -080084 static std::string GetDBusResult(Type type);
Darin Petkove4c0ace2011-08-24 10:32:46 -070085 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070086
Paul Stewart34f424e2015-01-16 15:30:20 -080087 // Log an error message from |from_here|. If |error| is non-NULL, also
88 // populate it.
Paul Stewarta794cd62015-06-16 13:13:10 -070089 static void PopulateAndLog(const tracked_objects::Location& from_here,
90 Error* error, Type type,
91 const std::string& message);
Paul Stewartbe005172011-11-02 18:10:29 -070092
Chris Masone8fe2c7e2011-06-09 15:51:19 -070093 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070094 struct Info {
Paul Stewarta794cd62015-06-16 13:13:10 -070095 const char* dbus_result; // Error type name.
96 const char* message; // Default Error type message.
Darin Petkove4c0ace2011-08-24 10:32:46 -070097 };
98
99 static const Info kInfos[kNumErrors];
Darin Petkove4c0ace2011-08-24 10:32:46 -0700100
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700101 Type type_;
102 std::string message_;
Peter Qiu93b243d2015-06-26 11:05:23 -0700103 tracked_objects::Location location_;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700104
105 DISALLOW_COPY_AND_ASSIGN(Error);
106};
107
108} // namespace shill
109
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500110// stream operator provided to facilitate logging
Paul Stewarta794cd62015-06-16 13:13:10 -0700111std::ostream& operator<<(std::ostream& stream, const shill::Error& error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500112
Ben Chanc45688b2014-07-02 23:50:45 -0700113#endif // SHILL_ERROR_H_