exceptions.cpp revision 1.1 1 1.1 jmmv // Copyright 2011 Google Inc.
2 1.1 jmmv // All rights reserved.
3 1.1 jmmv //
4 1.1 jmmv // Redistribution and use in source and binary forms, with or without
5 1.1 jmmv // modification, are permitted provided that the following conditions are
6 1.1 jmmv // met:
7 1.1 jmmv //
8 1.1 jmmv // * Redistributions of source code must retain the above copyright
9 1.1 jmmv // notice, this list of conditions and the following disclaimer.
10 1.1 jmmv // * Redistributions in binary form must reproduce the above copyright
11 1.1 jmmv // notice, this list of conditions and the following disclaimer in the
12 1.1 jmmv // documentation and/or other materials provided with the distribution.
13 1.1 jmmv // * Neither the name of Google Inc. nor the names of its contributors
14 1.1 jmmv // may be used to endorse or promote products derived from this software
15 1.1 jmmv // without specific prior written permission.
16 1.1 jmmv //
17 1.1 jmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 1.1 jmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 1.1 jmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 1.1 jmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 1.1 jmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 1.1 jmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 1.1 jmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 jmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 jmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 jmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 1.1 jmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 jmmv
29 1.1 jmmv #include <cassert>
30 1.1 jmmv
31 1.1 jmmv #include <lua.hpp>
32 1.1 jmmv
33 1.1 jmmv #include "c_gate.hpp"
34 1.1 jmmv #include "exceptions.hpp"
35 1.1 jmmv #include "state.ipp"
36 1.1 jmmv
37 1.1 jmmv
38 1.1 jmmv /// Constructs a new error with a plain-text message.
39 1.1 jmmv ///
40 1.1 jmmv /// \param message The plain-text error message.
41 1.1 jmmv lutok::error::error(const std::string& message) :
42 1.1 jmmv std::runtime_error(message)
43 1.1 jmmv {
44 1.1 jmmv }
45 1.1 jmmv
46 1.1 jmmv
47 1.1 jmmv /// Destructor for the error.
48 1.1 jmmv lutok::error::~error(void) throw()
49 1.1 jmmv {
50 1.1 jmmv }
51 1.1 jmmv
52 1.1 jmmv
53 1.1 jmmv /// Constructs a new error.
54 1.1 jmmv ///
55 1.1 jmmv /// \param api_function_ The name of the API function that caused the error.
56 1.1 jmmv /// \param message The plain-text error message provided by Lua.
57 1.1 jmmv lutok::api_error::api_error(const std::string& api_function_,
58 1.1 jmmv const std::string& message) :
59 1.1 jmmv error(message),
60 1.1 jmmv _api_function(api_function_)
61 1.1 jmmv {
62 1.1 jmmv }
63 1.1 jmmv
64 1.1 jmmv
65 1.1 jmmv /// Destructor for the error.
66 1.1 jmmv lutok::api_error::~api_error(void) throw()
67 1.1 jmmv {
68 1.1 jmmv }
69 1.1 jmmv
70 1.1 jmmv
71 1.1 jmmv /// Constructs a new api_error with the message on the top of the Lua stack.
72 1.1 jmmv ///
73 1.1 jmmv /// \pre There is an error message on the top of the stack.
74 1.1 jmmv /// \post The error message is popped from the stack.
75 1.1 jmmv ///
76 1.1 jmmv /// \param state_ The Lua state.
77 1.1 jmmv /// \param api_function_ The name of the Lua API function that caused the error.
78 1.1 jmmv ///
79 1.1 jmmv /// \return A new api_error with the popped message.
80 1.1 jmmv lutok::api_error
81 1.1 jmmv lutok::api_error::from_stack(state& state_, const std::string& api_function_)
82 1.1 jmmv {
83 1.1 jmmv lua_State* raw_state = lutok::state_c_gate(state_).c_state();
84 1.1 jmmv
85 1.1 jmmv assert(lua_isstring(raw_state, -1));
86 1.1 jmmv const std::string message = lua_tostring(raw_state, -1);
87 1.1 jmmv lua_pop(raw_state, 1);
88 1.1 jmmv return lutok::api_error(api_function_, message);
89 1.1 jmmv }
90 1.1 jmmv
91 1.1 jmmv
92 1.1 jmmv /// Gets the name of the Lua API function that caused this error.
93 1.1 jmmv ///
94 1.1 jmmv /// \return The name of the function.
95 1.1 jmmv const std::string&
96 1.1 jmmv lutok::api_error::api_function(void) const
97 1.1 jmmv {
98 1.1 jmmv return _api_function;
99 1.1 jmmv }
100 1.1 jmmv
101 1.1 jmmv
102 1.1 jmmv /// Constructs a new error.
103 1.1 jmmv ///
104 1.1 jmmv /// \param filename_ The file that count not be found.
105 1.1 jmmv lutok::file_not_found_error::file_not_found_error(
106 1.1 jmmv const std::string& filename_) :
107 1.1 jmmv error("File '" + filename_ + "' not found"),
108 1.1 jmmv _filename(filename_)
109 1.1 jmmv {
110 1.1 jmmv }
111 1.1 jmmv
112 1.1 jmmv
113 1.1 jmmv /// Destructor for the error.
114 1.1 jmmv lutok::file_not_found_error::~file_not_found_error(void) throw()
115 1.1 jmmv {
116 1.1 jmmv }
117 1.1 jmmv
118 1.1 jmmv
119 1.1 jmmv /// Gets the name of the file that could not be found.
120 1.1 jmmv ///
121 1.1 jmmv /// \return The name of the file.
122 1.1 jmmv const std::string&
123 1.1 jmmv lutok::file_not_found_error::filename(void) const
124 1.1 jmmv {
125 1.1 jmmv return _filename;
126 1.1 jmmv }
127