blob: 001be8a4bf13d9a3d398441493804c908f447793 [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,
Eric Shienbrood9a245532012-03-07 14:20:39 -050025 kOperationInitiated,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070026 kInProgress,
27 kInternalError,
28 kInvalidArguments,
29 kInvalidNetworkName,
30 kInvalidPassphrase,
31 kInvalidProperty,
Darin Petkove4c0ace2011-08-24 10:32:46 -070032 kNoCarrier,
33 kNotConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070034 kNotFound,
Darin Petkove4c0ace2011-08-24 10:32:46 -070035 kNotImplemented,
36 kNotOnHomeNetwork,
37 kNotRegistered,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070038 kNotSupported,
Darin Petkove4c0ace2011-08-24 10:32:46 -070039 kOperationAborted,
40 kOperationTimeout,
41 kPassphraseRequired,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050042 kIncorrectPin,
43 kPinRequired,
44 kPinBlocked,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070045 kPermissionDenied,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070046 kNumErrors
47 };
48
Darin Petkove4c0ace2011-08-24 10:32:46 -070049 Error(); // Success by default.
50 explicit Error(Type type); // Uses the default message for |type|.
51 Error(Type type, const std::string &message);
52 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070053
Darin Petkove4c0ace2011-08-24 10:32:46 -070054 void Populate(Type type); // Uses the default message for |type|.
55 void Populate(Type type, const std::string &message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070056
Gaurav Shah1b7a6162011-11-09 11:41:01 -080057 void Reset();
58
Darin Petkove5bc2cb2011-12-07 14:47:32 +010059 void CopyFrom(const Error &error);
60
mukesh agrawal7a4e4002011-09-06 11:26:05 -070061 // Sets the DBus |error| and returns true if Error represents failure.
62 // Leaves |error| unchanged, and returns false, otherwise.
63 bool ToDBusError(::DBus::Error *error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070064
65 Type type() const { return type_; }
66 const std::string &message() const { return message_; }
67
68 bool IsSuccess() const { return type_ == kSuccess; }
Eric Shienbrood9a245532012-03-07 14:20:39 -050069 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
70 bool IsOngoing() const { return type_ == kOperationInitiated; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070071
72 static std::string GetName(Type type);
73 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070074
Paul Stewartbe005172011-11-02 18:10:29 -070075 // Log an error message. If |error| is non-NULL, also populate it.
76 static void PopulateAndLog(Error *error, Type type,
77 const std::string &message);
78
Chris Masone8fe2c7e2011-06-09 15:51:19 -070079 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070080 struct Info {
81 const char *name; // Error type name.
82 const char *message; // Default Error type message.
83 };
84
85 static const Info kInfos[kNumErrors];
Darin Petkove4c0ace2011-08-24 10:32:46 -070086
Chris Masone8fe2c7e2011-06-09 15:51:19 -070087 Type type_;
88 std::string message_;
89
90 DISALLOW_COPY_AND_ASSIGN(Error);
91};
92
93} // namespace shill
94
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050095// stream operator provided to facilitate logging
96std::ostream &operator<<(std::ostream &stream, const shill::Error &error);
97
Chris Masone8fe2c7e2011-06-09 15:51:19 -070098#endif // SHILL_ERROR_