1 /* 2 * Copyright 2021 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Niels Sascha Reedijk, niels.reedijk@gmail.com 7 */ 8 9 10 #include <ErrorsExt.h> 11 12 #include <iostream> 13 #include <sstream> 14 15 #include <DataIO.h> 16 17 using namespace BPrivate::Network; 18 19 20 BError::BError(const char* origin) 21 : 22 fOrigin(BString(origin)) 23 { 24 } 25 26 27 BError::BError(BString origin) 28 : 29 fOrigin(std::move(origin)) 30 { 31 } 32 33 34 BError::~BError() noexcept = default; 35 36 37 BError::BError(const BError& error) = default; 38 39 40 BError::BError(BError&& error) noexcept = default; 41 42 43 BError& BError::operator=(const BError& error) = default; 44 45 46 BError& BError::operator=(BError&& error) noexcept = default; 47 48 49 const char* 50 BError::Origin() const noexcept 51 { 52 return fOrigin.String(); 53 } 54 55 56 BString 57 BError::DebugMessage() const 58 { 59 BString debugMessage; 60 debugMessage << "[" << Origin() << "] " << Message(); 61 return debugMessage; 62 } 63 64 65 void 66 BError::WriteToStream(std::ostream& stream) const 67 { 68 stream << DebugMessage().String() << std::endl; 69 } 70 71 72 size_t 73 BError::WriteToOutput(BDataIO* output) const 74 { 75 std::stringstream stream; 76 WriteToStream(stream); 77 ssize_t result = output->Write(stream.str().c_str(), stream.str().length() + 1); 78 if (result < 0) 79 throw BSystemError("BDataIO::Write()", result); 80 return static_cast<size_t>(result); 81 } 82 83 84 void 85 BError::_ReservedError1() 86 { 87 } 88 89 90 void 91 BError::_ReservedError2() 92 { 93 } 94 95 96 void 97 BError::_ReservedError3() 98 { 99 } 100 101 102 void 103 BError::_ReservedError4() 104 { 105 } 106 107 108 /* BRuntimeError */ 109 BRuntimeError::BRuntimeError(const char* origin, const char* message) 110 : 111 BError(origin), 112 fMessage(BString(message)) 113 { 114 } 115 116 117 BRuntimeError::BRuntimeError(const char* origin, BString message) 118 : 119 BError(origin), 120 fMessage(std::move(message)) 121 { 122 } 123 124 125 BRuntimeError::BRuntimeError(BString origin, BString message) 126 : 127 BError(std::move(origin)), 128 fMessage(std::move(message)) 129 { 130 } 131 132 133 BRuntimeError::BRuntimeError(const BRuntimeError& other) = default; 134 135 136 BRuntimeError::BRuntimeError(BRuntimeError&& other) noexcept = default; 137 138 139 BRuntimeError& BRuntimeError::operator=(const BRuntimeError& other) = default; 140 141 142 BRuntimeError& BRuntimeError::operator=(BRuntimeError&& other) noexcept = default; 143 144 145 const char* 146 BRuntimeError::Message() const noexcept 147 { 148 return fMessage.String(); 149 } 150 151 152 /* BSystemError */ 153 BSystemError::BSystemError(const char* origin, status_t error) 154 : 155 BError(origin), 156 fErrorCode(error) 157 { 158 } 159 160 161 BSystemError::BSystemError(BString origin, status_t error) 162 : 163 BError(std::move(origin)), 164 fErrorCode(error) 165 { 166 } 167 168 169 BSystemError::BSystemError(const BSystemError& other) = default; 170 171 172 BSystemError::BSystemError(BSystemError&& other) noexcept = default; 173 174 175 BSystemError& BSystemError::operator=(const BSystemError& other) = default; 176 177 178 BSystemError& BSystemError::operator=(BSystemError&& other) noexcept = default; 179 180 181 const char* 182 BSystemError::Message() const noexcept 183 { 184 return strerror(fErrorCode); 185 } 186 187 188 BString 189 BSystemError::DebugMessage() const 190 { 191 BString debugMessage; 192 debugMessage << "[" << Origin() << "] " << Message() << " (" << fErrorCode << ")"; 193 return debugMessage; 194 } 195 196 197 status_t 198 BSystemError::Error() noexcept 199 { 200 return fErrorCode; 201 } 202