1 // Copyright 2011 Google Inc. 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 <cassert> 30 31 #include <lua.hpp> 32 33 #include "c_gate.hpp" 34 #include "exceptions.hpp" 35 #include "state.ipp" 36 37 38 /// Constructs a new error with a plain-text message. 39 /// 40 /// \param message The plain-text error message. 41 lutok::error::error(const std::string& message) : 42 std::runtime_error(message) 43 { 44 } 45 46 47 /// Destructor for the error. 48 lutok::error::~error(void) throw() 49 { 50 } 51 52 53 /// Constructs a new error. 54 /// 55 /// \param api_function_ The name of the API function that caused the error. 56 /// \param message The plain-text error message provided by Lua. 57 lutok::api_error::api_error(const std::string& api_function_, 58 const std::string& message) : 59 error(message), 60 _api_function(api_function_) 61 { 62 } 63 64 65 /// Destructor for the error. 66 lutok::api_error::~api_error(void) throw() 67 { 68 } 69 70 71 /// Constructs a new api_error with the message on the top of the Lua stack. 72 /// 73 /// \pre There is an error message on the top of the stack. 74 /// \post The error message is popped from the stack. 75 /// 76 /// \param state_ The Lua state. 77 /// \param api_function_ The name of the Lua API function that caused the error. 78 /// 79 /// \return A new api_error with the popped message. 80 lutok::api_error 81 lutok::api_error::from_stack(state& state_, const std::string& api_function_) 82 { 83 lua_State* raw_state = lutok::state_c_gate(state_).c_state(); 84 85 assert(lua_isstring(raw_state, -1)); 86 const std::string message = lua_tostring(raw_state, -1); 87 lua_pop(raw_state, 1); 88 return lutok::api_error(api_function_, message); 89 } 90 91 92 /// Gets the name of the Lua API function that caused this error. 93 /// 94 /// \return The name of the function. 95 const std::string& 96 lutok::api_error::api_function(void) const 97 { 98 return _api_function; 99 } 100 101 102 /// Constructs a new error. 103 /// 104 /// \param filename_ The file that count not be found. 105 lutok::file_not_found_error::file_not_found_error( 106 const std::string& filename_) : 107 error("File '" + filename_ + "' not found"), 108 _filename(filename_) 109 { 110 } 111 112 113 /// Destructor for the error. 114 lutok::file_not_found_error::~file_not_found_error(void) throw() 115 { 116 } 117 118 119 /// Gets the name of the file that could not be found. 120 /// 121 /// \return The name of the file. 122 const std::string& 123 lutok::file_not_found_error::filename(void) const 124 { 125 return _filename; 126 } 127