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