blob: 6dd256a6709e48fe31697b2b6c991ab0d6d53d5a [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
5#ifndef SHILL_ERROR_
6#define SHILL_ERROR_
7
8#include <string>
9
10#include <base/basictypes.h>
11
12namespace DBus {
13class Error;
14} // namespace DBus
15
16namespace shill {
17
18class Error {
19 public:
20 enum Type {
Darin Petkove4c0ace2011-08-24 10:32:46 -070021 kSuccess = 0, // No error.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050022 kOperationFailed, // failure, otherwise unspecified
Darin Petkove4c0ace2011-08-24 10:32:46 -070023 kAlreadyConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070024 kAlreadyExists,
25 kInProgress,
26 kInternalError,
27 kInvalidArguments,
28 kInvalidNetworkName,
29 kInvalidPassphrase,
30 kInvalidProperty,
Darin Petkove4c0ace2011-08-24 10:32:46 -070031 kNoCarrier,
32 kNotConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033 kNotFound,
Darin Petkove4c0ace2011-08-24 10:32:46 -070034 kNotImplemented,
35 kNotOnHomeNetwork,
36 kNotRegistered,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070037 kNotSupported,
Darin Petkove4c0ace2011-08-24 10:32:46 -070038 kOperationAborted,
39 kOperationTimeout,
40 kPassphraseRequired,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050041 kIncorrectPin,
42 kPinRequired,
43 kPinBlocked,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070044 kPermissionDenied,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070045 kNumErrors
46 };
47
Darin Petkove4c0ace2011-08-24 10:32:46 -070048 Error(); // Success by default.
49 explicit Error(Type type); // Uses the default message for |type|.
50 Error(Type type, const std::string &message);
51 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070052
Darin Petkove4c0ace2011-08-24 10:32:46 -070053 void Populate(Type type); // Uses the default message for |type|.
54 void Populate(Type type, const std::string &message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070055
Gaurav Shah1b7a6162011-11-09 11:41:01 -080056 void Reset();
57
Darin Petkove5bc2cb2011-12-07 14:47:32 +010058 void CopyFrom(const Error &error);
59
mukesh agrawal7a4e4002011-09-06 11:26:05 -070060 // Sets the DBus |error| and returns true if Error represents failure.
61 // Leaves |error| unchanged, and returns false, otherwise.
62 bool ToDBusError(::DBus::Error *error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070063
64 Type type() const { return type_; }
65 const std::string &message() const { return message_; }
66
67 bool IsSuccess() const { return type_ == kSuccess; }
68 bool IsFailure() const { return !IsSuccess(); }
69
70 static std::string GetName(Type type);
71 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070072
Paul Stewartbe005172011-11-02 18:10:29 -070073 // Log an error message. If |error| is non-NULL, also populate it.
74 static void PopulateAndLog(Error *error, Type type,
75 const std::string &message);
76
Chris Masone8fe2c7e2011-06-09 15:51:19 -070077 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070078 struct Info {
79 const char *name; // Error type name.
80 const char *message; // Default Error type message.
81 };
82
83 static const Info kInfos[kNumErrors];
84 static const char kInterfaceName[];
85
Chris Masone8fe2c7e2011-06-09 15:51:19 -070086 Type type_;
87 std::string message_;
88
89 DISALLOW_COPY_AND_ASSIGN(Error);
90};
91
92} // namespace shill
93
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050094// stream operator provided to facilitate logging
95std::ostream &operator<<(std::ostream &stream, const shill::Error &error);
96
Chris Masone8fe2c7e2011-06-09 15:51:19 -070097#endif // SHILL_ERROR_