<commit_before>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2019 <NAME>, Videolabs, VideoLAN
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * 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>
#include <stdexcept>

#include <sqlite3.h>

namespace medialibrary
{

namespace sqlite
{
namespace errors
{

class Generic : public std::runtime_error
{
public:
    Generic( const std::string& msg ) : std::runtime_error( msg.c_str() ) {}
};

/**
 * These errors happen before the request gets executed. Usually because of a
 * syntax error, or an invalid bind index
 */
class Prepare : public Generic
{
public:
    Prepare( const char* req, const char* msg, int extendedCode )
        : Generic( std::string( "Failed to compile/prepare request [" )
                   + req + "]: " + msg + " (error code: " +
                   std::to_string( extendedCode ) + ")" )
    {
    }
};

/**
 * This is the general type for runtime error, ie. when the request is being
 * executed.
 */
class Runtime : public Generic
{
public:
    Runtime( const char* req, const char* errMsg, int extendedCode )
        : Generic( std::string( "Failed to run request [" ) + req + "]: " + errMsg +
                   "(" + std::to_string( extendedCode ) + ")" )
        , m_errorCode( extendedCode )
    {
    }

    Runtime( const std::string& msg, int errCode )
        : Generic( msg )
        , m_errorCode( errCode )
    {
    }

    int code() const
    {
        return m_errorCode & 0xFF;
    }

private:
    int m_errorCode;
};

class ConstraintViolation : public Runtime
{
public:
    ConstraintViolation( const char* req, const char* err, int errCode )
        : Runtime( std::string( "Request [" ) + req + "] aborted due to "
                    "constraint violation (" + err + ")", errCode )
    {
    }
};

class GenericError : public Runtime
{
public:
    GenericError( const char* req, const char* errMsg, int extendedCode )
        : Runtime( req, errMsg, extendedCode )
    {
    }
};

class ErrorMissingColSeq : public GenericError
{
public:
    ErrorMissingColSeq( const char* req, const char* errMsg, int extendedCode )
        : GenericError( req, errMsg, extendedCode )
    {
    }
};

class ErrorRetry : public GenericError
{
public:
    ErrorRetry( const char* req, const char* errMsg, int extendedCode )
        : GenericError( req, errMsg, extendedCode )
    {
    }<commit_msg>sqlite: Errors: Add a comment about the GenericError class<commit_after>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2019 <NAME>, Videolabs, VideoLAN
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * 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>
#include <stdexcept>

#include <sqlite3.h>

namespace medialibrary
{

namespace sqlite
{
namespace errors
{

class Generic : public std::runtime_error
{
public:
    Generic( const std::string& msg ) : std::runtime_error( msg.c_str() ) {}
};

/**
 * These errors happen before the request gets executed. Usually because of a
 * syntax error, or an invalid bind index
 */
class Prepare : public Generic
{
public:
    Prepare( const char* req, const char* msg, int extendedCode )
        : Generic( std::string( "Failed to compile/prepare request [" )
                   + req + "]: " + msg + " (error code: " +
                   std::to_string( extendedCode ) + ")" )
    {
    }
};

/**
 * This is the general type for runtime error, ie. when the request is being
 * executed.
 */
class Runtime : public Generic
{
public:
    Runtime( const char* req, const char* errMsg, int extendedCode )
        : Generic( std::string( "Failed to run request [" ) + req + "]: " + errMsg +
                   "(" + std::to_string( extendedCode ) + ")" )
        , m_errorCode( extendedCode )
    {
    }

    Runtime( const std::string& msg, int errCode )
        : Generic( msg )
        , m_errorCode( errCode )
    {
    }

    int code() const
    {
        return m_errorCode & 0xFF;
    }

private:
    int m_errorCode;
};

class ConstraintViolation : public Runtime
{
public:
    ConstraintViolation( const char* req, const char* err, int errCode )
        : Runtime( std::string( "Request [" ) + req + "] aborted due to "
                    "constraint violation (" + err + ")", errCode )
    {
    }
};

/*
 * /!\ Warning /!\
 * This is not a generic error in the sense of the exception types
 * hierarchy. It's the SQLITE_ERROR counterpart.
 */
class GenericError : public Runtime
{
public:
    GenericError( const char* req, const char* errMsg, int extendedCode )
        : Runtime( req, errMsg, extendedCode )
    {
    }
};

class ErrorMissingColSeq : public GenericError
{
public:
    ErrorMissingColSeq( const char* req, const char* errMsg, int extendedCode )
        : GenericError( req, errMsg, extendedCode )
    {
    }
};

class ErrorRetry : public GenericError
{
public:
    ErrorRetry( const char* req, const char* errMsg, int extendedCode )
        : GenericError( req, errMsg, extendedCode )
    {
    }
{'fraction_non_alphanumeric': 0.16295000728756742, 'fraction_numerical': 0.006704562017198659, 'mean_word_length': 3.1918142944410506, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2332590', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1829, 'n_words': 705}