blob: ca1adee00938ceacf630d45a30b804cf02537f9d [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,
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040045 kInvalidApn,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070046 kPermissionDenied,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070047 kNumErrors
48 };
49
Darin Petkove4c0ace2011-08-24 10:32:46 -070050 Error(); // Success by default.
51 explicit Error(Type type); // Uses the default message for |type|.
52 Error(Type type, const std::string &message);
53 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070054
Darin Petkove4c0ace2011-08-24 10:32:46 -070055 void Populate(Type type); // Uses the default message for |type|.
56 void Populate(Type type, const std::string &message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070057
Gaurav Shah1b7a6162011-11-09 11:41:01 -080058 void Reset();
59
Darin Petkove5bc2cb2011-12-07 14:47:32 +010060 void CopyFrom(const Error &error);
61
mukesh agrawal7a4e4002011-09-06 11:26:05 -070062 // Sets the DBus |error| and returns true if Error represents failure.
63 // Leaves |error| unchanged, and returns false, otherwise.
64 bool ToDBusError(::DBus::Error *error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070065
66 Type type() const { return type_; }
67 const std::string &message() const { return message_; }
68
69 bool IsSuccess() const { return type_ == kSuccess; }
Eric Shienbrood9a245532012-03-07 14:20:39 -050070 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
71 bool IsOngoing() const { return type_ == kOperationInitiated; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070072
73 static std::string GetName(Type type);
74 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070075
Paul Stewartbe005172011-11-02 18:10:29 -070076 // Log an error message. If |error| is non-NULL, also populate it.
77 static void PopulateAndLog(Error *error, Type type,
78 const std::string &message);
79
Chris Masone8fe2c7e2011-06-09 15:51:19 -070080 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070081 struct Info {
82 const char *name; // Error type name.
83 const char *message; // Default Error type message.
84 };
85
86 static const Info kInfos[kNumErrors];
Darin Petkove4c0ace2011-08-24 10:32:46 -070087
Chris Masone8fe2c7e2011-06-09 15:51:19 -070088 Type type_;
89 std::string message_;
90
91 DISALLOW_COPY_AND_ASSIGN(Error);
92};
93
94} // namespace shill
95
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050096// stream operator provided to facilitate logging
97std::ostream &operator<<(std::ostream &stream, const shill::Error &error);
98
Chris Masone8fe2c7e2011-06-09 15:51:19 -070099#endif // SHILL_ERROR_