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
17using namespace BPrivate::Network;
18
19
20BError::BError(const char* origin)
21	:
22	fOrigin(BString(origin))
23{
24}
25
26
27BError::BError(BString origin)
28	:
29	fOrigin(std::move(origin))
30{
31}
32
33
34BError::~BError() noexcept = default;
35
36
37BError::BError(const BError& error) = default;
38
39
40BError::BError(BError&& error) noexcept = default;
41
42
43BError& BError::operator=(const BError& error) = default;
44
45
46BError& BError::operator=(BError&& error) noexcept = default;
47
48
49const char*
50BError::Origin() const noexcept
51{
52	return fOrigin.String();
53}
54
55
56BString
57BError::DebugMessage() const
58{
59	BString debugMessage;
60	debugMessage << "[" << Origin() << "] " << Message();
61	return debugMessage;
62}
63
64
65void
66BError::WriteToStream(std::ostream& stream) const
67{
68	stream << DebugMessage().String() << std::endl;
69}
70
71
72size_t
73BError::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
84void
85BError::_ReservedError1()
86{
87}
88
89
90void
91BError::_ReservedError2()
92{
93}
94
95
96void
97BError::_ReservedError3()
98{
99}
100
101
102void
103BError::_ReservedError4()
104{
105}
106
107
108/* BRuntimeError */
109BRuntimeError::BRuntimeError(const char* origin, const char* message)
110	:
111	BError(origin),
112	fMessage(BString(message))
113{
114}
115
116
117BRuntimeError::BRuntimeError(const char* origin, BString message)
118	:
119	BError(origin),
120	fMessage(std::move(message))
121{
122}
123
124
125BRuntimeError::BRuntimeError(BString origin, BString message)
126	:
127	BError(std::move(origin)),
128	fMessage(std::move(message))
129{
130}
131
132
133BRuntimeError::BRuntimeError(const BRuntimeError& other) = default;
134
135
136BRuntimeError::BRuntimeError(BRuntimeError&& other) noexcept = default;
137
138
139BRuntimeError& BRuntimeError::operator=(const BRuntimeError& other) = default;
140
141
142BRuntimeError& BRuntimeError::operator=(BRuntimeError&& other) noexcept = default;
143
144
145const char*
146BRuntimeError::Message() const noexcept
147{
148	return fMessage.String();
149}
150
151
152/* BSystemError */
153BSystemError::BSystemError(const char* origin, status_t error)
154	:
155	BError(origin),
156	fErrorCode(error)
157{
158}
159
160
161BSystemError::BSystemError(BString origin, status_t error)
162	:
163	BError(std::move(origin)),
164	fErrorCode(error)
165{
166}
167
168
169BSystemError::BSystemError(const BSystemError& other) = default;
170
171
172BSystemError::BSystemError(BSystemError&& other) noexcept = default;
173
174
175BSystemError& BSystemError::operator=(const BSystemError& other) = default;
176
177
178BSystemError& BSystemError::operator=(BSystemError&& other) noexcept = default;
179
180
181const char*
182BSystemError::Message() const noexcept
183{
184	return strerror(fErrorCode);
185}
186
187
188BString
189BSystemError::DebugMessage() const
190{
191	BString debugMessage;
192	debugMessage << "[" << Origin() << "] " << Message() << " (" << fErrorCode << ")";
193	return debugMessage;
194}
195
196
197status_t
198BSystemError::Error() noexcept
199{
200	return fErrorCode;
201}
202