SqliteErrors.h 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#pragma once

#include <string>
#include <exception>
27
#include <stdexcept>
28

29 30
#include <sqlite3.h>

31 32 33
namespace medialibrary
{

34 35 36 37 38
namespace sqlite
{
namespace errors
{

39
class Generic : public std::runtime_error
40 41
{
public:
42
    Generic( const char* req, const char* msg, int extendedCode )
43 44
        : std::runtime_error( std::string( "Failed to compile/prepare request [" ) + req
                                           + "]: " + msg + "(" + std::to_string( extendedCode ) + ")" )
45 46
    {
    }
47 48
    Generic( const std::string& msg )
        : std::runtime_error( msg )
49 50 51 52
    {
    }
};

53
class GenericExecution : public Generic
54 55
{
public:
56
    GenericExecution( const char* req, const char* errMsg, int errCode, int extendedCode )
57
        : Generic( std::string( "Failed to run request [" ) + req + "]: " + errMsg +
58
                   "(" + std::to_string( extendedCode ) + ")" )
59
        , m_errorCode( errCode )
60 61 62
    {
    }

63 64 65
    GenericExecution( const std::string& msg, int errCode )
        : Generic( msg )
        , m_errorCode( errCode )
66
    {
67 68 69 70 71
    }

    int code() const
    {
        return m_errorCode;
72 73 74
    }

private:
75 76 77 78 79 80 81
    int m_errorCode;
};

class ConstraintViolation : public Generic
{
public:
    ConstraintViolation( const std::string& req, const std::string& err )
82
        : Generic( std::string( "Request [" ) + req + "] aborted due to "
83 84 85 86 87 88 89 90 91 92 93 94 95
                    "constraint violation (" + err + ")" )
    {
    }
};

class ColumnOutOfRange : public Generic
{
public:
    ColumnOutOfRange( unsigned int idx, unsigned int nbColumns )
        : Generic( "Attempting to extract column at index " + std::to_string( idx ) +
                   " from a request with " + std::to_string( nbColumns ) + " columns" )
    {
    }
96 97
};

98
static inline bool isInnocuous( int errCode )
99
{
100
    switch ( errCode )
101 102 103
    {
    case SQLITE_IOERR:
    case SQLITE_NOMEM:
104 105
    case SQLITE_BUSY:
    case SQLITE_READONLY:
106 107 108 109 110
        return true;
    }
    return false;
}

111 112 113 114 115
static inline bool isInnocuous( const GenericExecution& ex )
{
    return isInnocuous( ex.code() );
}

116

117 118
} // namespace errors
} // namespace sqlite
119 120

} // namespace medialibrary