| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without modification, are |
| 5 | // permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 8 | // conditions and the following disclaimer. |
| 9 | // |
| 10 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 11 | // of conditions and the following disclaimer in the documentation and/or other |
| 12 | // materials provided with the distribution. |
| 13 | // |
| 14 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 15 | // used to endorse or promote products derived from this software without specific |
| 16 | // prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 19 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 21 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 25 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 26 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "error.h" |
| 29 | |
| 30 | #include <lmdb.h> |
| 31 | #include <string> |
| 32 | |
| 33 | namespace { |
| 34 | struct category final : std::error_category |
| 35 | { |
| 36 | virtual const char* name() const noexcept override final |
| 37 | { |
| 38 | return "lmdb::error_category()"; |
| 39 | } |
| 40 | |
| 41 | virtual std::string message(int value) const override final |
| 42 | { |
| 43 | char const* const msg = mdb_strerror(value); |
| 44 | if (msg) |
| 45 | return msg; |
| 46 | return "Unknown lmdb::error_category() value"; |
| 47 | } |
| 48 | |
| 49 | virtual std::error_condition default_error_condition(int value) const noexcept override final |
| 50 | { |
| 51 | switch (value) |
| 52 | { |
| 53 | case MDB_KEYEXIST: |
| 54 | case MDB_NOTFOUND: |
| 55 | break; // map to nothing generic |
| 56 | case MDB_PAGE_NOTFOUND: |
| 57 | case MDB_CORRUPTED: |
| 58 | return std::errc::bad_address; |
| 59 | case MDB_PANIC: |
| 60 | case MDB_VERSION_MISMATCH: |
| 61 | case MDB_INVALID: |
| 62 | break; // map to nothing generic |
| 63 | case MDB_MAP_FULL: |
| 64 | return std::errc::no_buffer_space; |
| 65 | case MDB_DBS_FULL: |
| 66 | break; // map to nothing generic |
| 67 | case MDB_READERS_FULL: |
| 68 | case MDB_TLS_FULL: |
| 69 | return std::errc::no_lock_available; |
| 70 | case MDB_TXN_FULL: |
| 71 | case MDB_CURSOR_FULL: |
| 72 | case MDB_PAGE_FULL: |
| 73 | case MDB_MAP_RESIZED: |
| 74 | break; // map to nothing generic |
| 75 | case MDB_INCOMPATIBLE: |
| 76 | return std::errc::invalid_argument; |
| 77 | case MDB_BAD_RSLOT: |
| 78 | case MDB_BAD_TXN: |
| 79 | case MDB_BAD_VALSIZE: |
| 80 | case MDB_BAD_DBI: |
| 81 | return std::errc::invalid_argument; |
| 82 | default: |
| 83 | return std::error_condition{value, std::generic_category()}; |
| 84 | } |
| 85 | return std::error_condition{value, *this}; |
| 86 | } |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | namespace lmdb |
| 91 | { |
| 92 | std::error_category const& error_category() noexcept |
| 93 | { |
| 94 | static const category instance{}; |
| 95 | return instance; |
| 96 | } |
| 97 | } |
| 98 |