Apache Mesos
errorbase.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_ERROR_BASE_HPP__
14 #define __STOUT_ERROR_BASE_HPP__
15 
16 #include <errno.h>
17 
18 #include <string>
19 
20 #include <stout/os/strerror.hpp>
21 
22 // A useful type that can be used to represent a Try that has
23 // failed. You can also use 'ErrnoError' to append the error message
24 // associated with the current 'errno' to your own error message.
25 //
26 // Examples:
27 //
28 // Result<int> result = Error("uninitialized");
29 // Try<std::string> = Error("uninitialized");
30 //
31 // void foo(Try<std::string> t) {}
32 //
33 // foo(Error("some error here"));
34 
35 class Error
36 {
37 public:
38  explicit Error(const std::string& _message) : message(_message) {}
39 
40  bool operator==(const Error& that) const
41  {
42  return message == that.message;
43  }
44 
45  const std::string message;
46 };
47 
48 
49 class ErrnoError : public Error
50 {
51 public:
52  ErrnoError() : ErrnoError(errno) {}
53 
54  explicit ErrnoError(int _code) : Error(os::strerror(_code)), code(_code) {}
55 
56  explicit ErrnoError(const std::string& message)
57  : ErrnoError(errno, message) {}
58 
59  ErrnoError(int _code, const std::string& message)
60  : Error(message + ": " + os::strerror(_code)), code(_code) {}
61 
62  const int code;
63 };
64 
65 #endif // __STOUT_ERROR_BASE_HPP__
std::string strerror(int errno_)
A thread-safe version of strerror.
Definition: strerror.hpp:30
Definition: errorbase.hpp:35
Error(const std::string &_message)
Definition: errorbase.hpp:38
ErrnoError(int _code, const std::string &message)
Definition: errorbase.hpp:59
Definition: errorbase.hpp:49
ErrnoError(const std::string &message)
Definition: errorbase.hpp:56
ErrnoError()
Definition: errorbase.hpp:52
bool operator==(const Error &that) const
Definition: errorbase.hpp:40
const std::string message
Definition: errorbase.hpp:45
const int code
Definition: errorbase.hpp:62
ErrnoError(int _code)
Definition: errorbase.hpp:54