blob: e22b011d2fc874b37991bc4ee5dde4e2dfacefa9 [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,
Ben Chan151d4472013-09-06 13:29:46 -070046 kWrongState,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070047 kPermissionDenied,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070048 kNumErrors
49 };
50
Darin Petkove4c0ace2011-08-24 10:32:46 -070051 Error(); // Success by default.
52 explicit Error(Type type); // Uses the default message for |type|.
53 Error(Type type, const std::string &message);
54 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070055
Darin Petkove4c0ace2011-08-24 10:32:46 -070056 void Populate(Type type); // Uses the default message for |type|.
57 void Populate(Type type, const std::string &message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070058
Gaurav Shah1b7a6162011-11-09 11:41:01 -080059 void Reset();
60
Darin Petkove5bc2cb2011-12-07 14:47:32 +010061 void CopyFrom(const Error &error);
62
mukesh agrawal7a4e4002011-09-06 11:26:05 -070063 // Sets the DBus |error| and returns true if Error represents failure.
64 // Leaves |error| unchanged, and returns false, otherwise.
65 bool ToDBusError(::DBus::Error *error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070066
67 Type type() const { return type_; }
68 const std::string &message() const { return message_; }
69
70 bool IsSuccess() const { return type_ == kSuccess; }
Eric Shienbrood9a245532012-03-07 14:20:39 -050071 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); }
72 bool IsOngoing() const { return type_ == kOperationInitiated; }
Darin Petkove4c0ace2011-08-24 10:32:46 -070073
74 static std::string GetName(Type type);
75 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070076
Paul Stewartbe005172011-11-02 18:10:29 -070077 // Log an error message. If |error| is non-NULL, also populate it.
78 static void PopulateAndLog(Error *error, Type type,
79 const std::string &message);
80
Chris Masone8fe2c7e2011-06-09 15:51:19 -070081 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070082 struct Info {
83 const char *name; // Error type name.
84 const char *message; // Default Error type message.
85 };
86
87 static const Info kInfos[kNumErrors];
Darin Petkove4c0ace2011-08-24 10:32:46 -070088
Chris Masone8fe2c7e2011-06-09 15:51:19 -070089 Type type_;
90 std::string message_;
91
92 DISALLOW_COPY_AND_ASSIGN(Error);
93};
94
95} // namespace shill
96
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050097// stream operator provided to facilitate logging
98std::ostream &operator<<(std::ostream &stream, const shill::Error &error);
99
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700100#endif // SHILL_ERROR_