blob: 2507d08b34ae125ec702a457770ac810ee17b61d [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
8#include <string>
9
Ben Chancc67c522014-09-03 07:19:18 -070010#include <base/macros.h>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011
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,
Paul Stewart71b9ed52014-01-29 08:53:06 -080025 kIncorrectPin,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070026 kInProgress,
27 kInternalError,
Paul Stewart71b9ed52014-01-29 08:53:06 -080028 kInvalidApn,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029 kInvalidArguments,
30 kInvalidNetworkName,
31 kInvalidPassphrase,
32 kInvalidProperty,
Darin Petkove4c0ace2011-08-24 10:32:46 -070033 kNoCarrier,
34 kNotConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070035 kNotFound,
Darin Petkove4c0ace2011-08-24 10:32:46 -070036 kNotImplemented,
37 kNotOnHomeNetwork,
38 kNotRegistered,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070039 kNotSupported,
Darin Petkove4c0ace2011-08-24 10:32:46 -070040 kOperationAborted,
Paul Stewart71b9ed52014-01-29 08:53:06 -080041 kOperationInitiated,
Darin Petkove4c0ace2011-08-24 10:32:46 -070042 kOperationTimeout,
43 kPassphraseRequired,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070044 kPermissionDenied,
Paul Stewart71b9ed52014-01-29 08:53:06 -080045 kPinBlocked,
46 kPinRequired,
47 kWrongState,
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
Paul Stewart71b9ed52014-01-29 08:53:06 -080074 static std::string GetDBusResult(Type type);
Darin Petkove4c0ace2011-08-24 10:32:46 -070075 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 {
Paul Stewart71b9ed52014-01-29 08:53:06 -080083 const char *dbus_result; // Error type name.
Darin Petkove4c0ace2011-08-24 10:32:46 -070084 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
Ben Chanc45688b2014-07-02 23:50:45 -0700100#endif // SHILL_ERROR_H_