Commit 8c77b460 authored by jan.koester's avatar jan.koester
Browse files

better error handling

parent dfa901f7
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ namespace blogi {
        void save(const std::string key,const std::vector<char> value) override;
        void load(const std::string key,std::vector<char>  &value) override;
    private:
        bool reconnect();
        redisContext *_RedisCTX;
        std::string   _pw;
    };
+17 −8
Original line number Diff line number Diff line
@@ -541,7 +541,7 @@ namespace blogi {
                libhttppp::HttpResponse curres;
                curres.setVersion(HTTPVERSION(1.1));
                std::vector<char> value;

                try{
                    if(n>0){
                        _store->load(suuid,value);
                        curres.setContentType(res[0][0]);
@@ -550,6 +550,15 @@ namespace blogi {
                        curres.setState(HTTP404);
                        curres.send(req,nullptr,0);
                    }
                }catch(libhttppp::HTTPException &e){
                    if(Args->config->getRedisPassword()){
                        _store = new RedisStore(Args->config->getRedisHost(),Args->config->getRedisPort(),Args->config->getRedisPassword());
                    }else{
                        _store = new RedisStore(Args->config->getRedisHost(),Args->config->getRedisPort());
                    }
                    curres.setState(HTTP500);
                    curres.send(req,e.what(),strlen(e.what()));
                }

                curres.send(req, value.data(), value.size());
                return true;
+10 −29
Original line number Diff line number Diff line
@@ -53,53 +53,34 @@ blogi::RedisStore::RedisStore(const char *host,int port,const char *password){

}
void blogi::RedisStore::save(const std::string key, const std::vector<char> value){
    int tries=0;
REDISSAVE:
    libhttppp::HTTPException exp;
    redisReply* reply = (redisReply*) redisCommand(_RedisCTX,"SET %s %b",key.c_str(),value.data(),value.size());
    if (reply && reply->type==REDIS_REPLY_ERROR) {
        if(reconnect() && ++tries < 5)
            goto REDISSAVE;
        freeReplyObject(reply);
        libhttppp::HTTPException exp;
        exp[libhttppp::HTTPException::Error] << "media plugin err: " << _RedisCTX->errstr;
        throw exp;
    }
    freeReplyObject(reply);
    reply = (redisReply*) redisCommand(_RedisCTX, "save");
    if (reply && reply->type==REDIS_REPLY_ERROR) {
        if(reconnect() && ++tries < 5)
            goto REDISSAVE;
        freeReplyObject(reply);
        libhttppp::HTTPException exp;
        exp[libhttppp::HTTPException::Error] << "media plugin err: " << _RedisCTX->errstr;
        throw exp;
    }
}

void blogi::RedisStore::load(const std::string key,std::vector<char> &value) {
    int tries=0;
REDISLOAD:
    redisReply* reply = (redisReply*) redisCommand(_RedisCTX, "GET %s",key.c_str());
    if(reply && reply->type!=REDIS_REPLY_ERROR){
        std::copy(reply->str,reply->str+reply->len,std::inserter<std::vector<char>>(value,value.begin()));
    }else{
        if(reconnect() && ++tries < 5)
            goto REDISLOAD;
        freeReplyObject(reply);
        libhttppp::HTTPException exp;
        exp[libhttppp::HTTPException::Warning] << "media plugin err: " << _RedisCTX->errstr;
        exp[libhttppp::HTTPException::Error] << "media plugin err: " << _RedisCTX->errstr;
        throw exp;
    }
    freeReplyObject(reply);
}
bool blogi::RedisStore::reconnect(){
    int c = redisReconnect(_RedisCTX);
    if( c != REDIS_ERR ){
        if(!_pw.empty()){
            redisReply *reply = (redisReply*)redisCommand(_RedisCTX, "AUTH %s", _pw.c_str());
            if (reply->type == REDIS_REPLY_ERROR) {
                freeReplyObject(reply);
                return false;
            }else{
                freeReplyObject(reply);
                return true;
            }
        }
        return true;
    }
    return false;
}