|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <string> |
| 12 | + |
| 13 | +namespace gloo { |
| 14 | +namespace transport { |
| 15 | +namespace tcp { |
| 16 | + |
| 17 | +class Error { |
| 18 | + public: |
| 19 | + // Constant instance that indicates success. |
| 20 | + static const Error kSuccess; |
| 21 | + |
| 22 | + /* implicit */ Error() : valid_(false) {} |
| 23 | + |
| 24 | + virtual ~Error() = default; |
| 25 | + |
| 26 | + // Converting to boolean means checking if there is an error. This |
| 27 | + // means we don't need to use an `std::optional` and allows for a |
| 28 | + // snippet like the following: |
| 29 | + // |
| 30 | + // if (error) { |
| 31 | + // // Deal with it. |
| 32 | + // } |
| 33 | + // |
| 34 | + operator bool() const { |
| 35 | + return valid_; |
| 36 | + } |
| 37 | + |
| 38 | + // Returns an explanatory string. |
| 39 | + // Like `std::exception` but returns a `std::string`. |
| 40 | + virtual std::string what() const; |
| 41 | + |
| 42 | + protected: |
| 43 | + explicit Error(bool valid) : valid_(valid) {} |
| 44 | + |
| 45 | + private: |
| 46 | + const bool valid_; |
| 47 | +}; |
| 48 | + |
| 49 | +class SystemError : public Error { |
| 50 | + public: |
| 51 | + explicit SystemError(const char* syscall, int error) |
| 52 | + : Error(true), syscall_(syscall), error_(error) {} |
| 53 | + |
| 54 | + std::string what() const override; |
| 55 | + |
| 56 | + private: |
| 57 | + const char* syscall_; |
| 58 | + const int error_; |
| 59 | +}; |
| 60 | + |
| 61 | +class ShortReadError : public Error { |
| 62 | + public: |
| 63 | + ShortReadError(ssize_t expected, ssize_t actual) |
| 64 | + : Error(true), expected_(expected), actual_(actual) {} |
| 65 | + |
| 66 | + std::string what() const override; |
| 67 | + |
| 68 | + private: |
| 69 | + const ssize_t expected_; |
| 70 | + const ssize_t actual_; |
| 71 | +}; |
| 72 | + |
| 73 | +class ShortWriteError : public Error { |
| 74 | + public: |
| 75 | + ShortWriteError(ssize_t expected, ssize_t actual) |
| 76 | + : Error(true), expected_(expected), actual_(actual) {} |
| 77 | + |
| 78 | + std::string what() const override; |
| 79 | + |
| 80 | + private: |
| 81 | + const ssize_t expected_; |
| 82 | + const ssize_t actual_; |
| 83 | +}; |
| 84 | + |
| 85 | +} // namespace tcp |
| 86 | +} // namespace transport |
| 87 | +} // namespace gloo |
0 commit comments