xref: /freebsd/contrib/kyua/utils/fs/exceptions.cpp (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/fs/exceptions.hpp"
30 
31 #include <cstring>
32 
33 #include "utils/format/macros.hpp"
34 
35 namespace fs = utils::fs;
36 
37 
38 /// Constructs a new error with a plain-text message.
39 ///
40 /// \param message The plain-text error message.
41 fs::error::error(const std::string& message) :
42     std::runtime_error(message)
43 {
44 }
45 
46 
47 /// Destructor for the error.
48 fs::error::~error(void) throw()
49 {
50 }
51 
52 
53 /// Constructs a new invalid_path_error.
54 ///
55 /// \param textual_path Textual representation of the invalid path.
56 /// \param reason Description of the error in the path.
57 fs::invalid_path_error::invalid_path_error(const std::string& textual_path,
58                                            const std::string& reason) :
59     error(F("Invalid path '%s': %s") % textual_path % reason),
60     _textual_path(textual_path)
61 {
62 }
63 
64 
65 /// Destructor for the error.
66 fs::invalid_path_error::~invalid_path_error(void) throw()
67 {
68 }
69 
70 
71 /// Returns the invalid path related to the exception.
72 ///
73 /// \return The textual representation of the invalid path.
74 const std::string&
75 fs::invalid_path_error::invalid_path(void) const
76 {
77     return _textual_path;
78 }
79 
80 
81 /// Constructs a new join_error.
82 ///
83 /// \param textual_path1_ Textual representation of the first path.
84 /// \param textual_path2_ Textual representation of the second path.
85 /// \param reason Description of the error in the join operation.
86 fs::join_error::join_error(const std::string& textual_path1_,
87                            const std::string& textual_path2_,
88                            const std::string& reason) :
89     error(F("Cannot join paths '%s' and '%s': %s") % textual_path1_ %
90           textual_path2_ % reason),
91     _textual_path1(textual_path1_),
92     _textual_path2(textual_path2_)
93 {
94 }
95 
96 
97 /// Destructor for the error.
98 fs::join_error::~join_error(void) throw()
99 {
100 }
101 
102 
103 /// Gets the first path that caused the error in a join operation.
104 ///
105 /// \return The textual representation of the path.
106 const std::string&
107 fs::join_error::textual_path1(void) const
108 {
109     return _textual_path1;
110 }
111 
112 
113 /// Gets the second path that caused the error in a join operation.
114 ///
115 /// \return The textual representation of the path.
116 const std::string&
117 fs::join_error::textual_path2(void) const
118 {
119     return _textual_path2;
120 }
121 
122 
123 /// Constructs a new error based on an errno code.
124 ///
125 /// \param message_ The message describing what caused the error.
126 /// \param errno_ The error code.
127 fs::system_error::system_error(const std::string& message_, const int errno_) :
128     error(F("%s: %s") % message_ % std::strerror(errno_)),
129     _original_errno(errno_)
130 {
131 }
132 
133 
134 /// Destructor for the error.
135 fs::system_error::~system_error(void) throw()
136 {
137 }
138 
139 
140 
141 /// \return The original errno code.
142 int
143 fs::system_error::original_errno(void) const throw()
144 {
145     return _original_errno;
146 }
147 
148 
149 /// Constructs a new error with a plain-text message.
150 ///
151 /// \param message The plain-text error message.
152 fs::unsupported_operation_error::unsupported_operation_error(
153     const std::string& message) :
154     error(message)
155 {
156 }
157 
158 
159 /// Destructor for the error.
160 fs::unsupported_operation_error::~unsupported_operation_error(void) throw()
161 {
162 }
163