Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetLatestErrorMessage feature #138

Merged
merged 4 commits into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions include/CErrorMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#ifndef __C_CLIENT_ERROR_H__
#define __C_CLIENT_ERROR_H__

#include "CCommon.h"

#ifdef __cplusplus
extern "C" {
#endif

ROCKETMQCLIENT_API const char *GetLatestErrorMessage(); // Return the last error message
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all files in this pr have wrong format, pls format them


#ifdef __cplusplus
};
#endif
#endif //__C_CLIENT_ERROR_H__
44 changes: 44 additions & 0 deletions src/common/MQClientErrorContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include "MQClientErrorContainer.h"




namespace rocketmq {
MQClientErrorContainer * MQClientErrorContainer::s_instance = nullptr;

MQClientErrorContainer * MQClientErrorContainer::instance() {
if (!s_instance)
s_instance = new MQClientErrorContainer();
return s_instance;
}

void MQClientErrorContainer::setErr(std::string str) {
boost::lock_guard<boost::mutex> lock(this->mutex);
this->m_err = str;
}

std::string MQClientErrorContainer::getErr() {
boost::lock_guard<boost::mutex> lock(this->mutex);
return this->m_err;
}
}

43 changes: 43 additions & 0 deletions src/common/MQClientErrorContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MQCLIENTERRORCONTAINER_H_INCLUDE
#define MQCLIENTERRORCONTAINER_H_INCLUDE
#include <string>
#include <exception>
#include <boost/thread/mutex.hpp>
#include <boost/thread/lock_guard.hpp>

namespace rocketmq {
class MQClientErrorContainer {

public:
static MQClientErrorContainer * instance();

void setErr(std::string str);

std::string getErr();

private:
std::string m_err;
static MQClientErrorContainer * s_instance;
boost::mutex mutex;
};
}

#endif
33 changes: 33 additions & 0 deletions src/extern/CErrorMessage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "CErrorMessage.h"
#include "CCommon.h"
#include "MQClientErrorContainer.h"

#ifdef __cplusplus
extern "C" {
#endif
using namespace rocketmq;

const char *GetLatestErrorMessage() {
return MQClientErrorContainer::instance()->getErr().c_str();
}

#ifdef __cplusplus
};
#endif
9 changes: 9 additions & 0 deletions src/extern/CProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "CBatchMessage.h"
#include "CCommon.h"
#include "CMQException.h"
#include <string.h>
#include <typeinfo>
#include "MQClientErrorContainer.h"
#include "CMessage.h"
#include "CSendResult.h"
#include "DefaultMQProducer.h"
Expand Down Expand Up @@ -99,6 +102,7 @@ int StartProducer(CProducer* producer) {
try {
((DefaultMQProducer*)producer)->start();
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PRODUCER_START_FAILED;
}
return OK;
Expand Down Expand Up @@ -154,6 +158,7 @@ int SendMessageSync(CProducer* producer, CMessage* msg, CSendResult* result) {
strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1);
result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PRODUCER_SEND_SYNC_FAILED;
}
return OK;
Expand Down Expand Up @@ -216,6 +221,7 @@ int SendMessageAsync(CProducer* producer,
delete cSendCallback;
cSendCallback = NULL;
}
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PRODUCER_SEND_ASYNC_FAILED;
}
return OK;
Expand Down Expand Up @@ -245,6 +251,7 @@ int SendMessageOnewayOrderly(CProducer* producer, CMessage* msg, QueueSelectorCa
SelectMessageQueue selectMessageQueue(selector);
defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg);
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PRODUCER_SEND_ONEWAY_FAILED;
}
return OK;
Expand All @@ -271,6 +278,7 @@ int SendMessageOrderlyAsync(CProducer* producer,
} catch (exception& e) {
printf("%s\n", e.what());
// std::count<<e.what( )<<std::endl;
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PRODUCER_SEND_ORDERLYASYNC_FAILED;
}
return OK;
Expand All @@ -297,6 +305,7 @@ int SendMessageOrderly(CProducer* producer,
strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1);
result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PRODUCER_SEND_ORDERLY_FAILED;
}
return OK;
Expand Down
5 changes: 5 additions & 0 deletions src/extern/CPullConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "CMessageExt.h"
#include "CPullConsumer.h"
#include "CCommon.h"
#include "MQClientErrorContainer.h"

using namespace rocketmq;
using namespace std;
Expand Down Expand Up @@ -48,6 +49,7 @@ int StartPullConsumer(CPullConsumer* consumer) {
try {
((DefaultMQPullConsumer*)consumer)->start();
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PULLCONSUMER_START_FAILED;
}
return OK;
Expand Down Expand Up @@ -150,6 +152,7 @@ int FetchSubscriptionMessageQueues(CPullConsumer* consumer, const char* topic, C
} catch (MQException& e) {
*size = 0;
*mqs = NULL;
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PULLCONSUMER_FETCH_MQ_FAILED;
}
return OK;
Expand All @@ -174,6 +177,7 @@ CPullResult Pull(CPullConsumer* consumer,
try {
cppPullResult = ((DefaultMQPullConsumer*)consumer)->pull(messageQueue, subExpression, offset, maxNums);
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
cppPullResult.pullStatus = BROKER_TIMEOUT;
}

Expand Down Expand Up @@ -228,6 +232,7 @@ int ReleasePullResult(CPullResult pullResult) {
try {
delete ((PullResult*)pullResult.pData);
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return NULL_POINTER;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/extern/CPushConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CPushConsumer.h"
#include "CCommon.h"
#include <map>
#include "MQClientErrorContainer.h"

using namespace rocketmq;
using namespace std;
Expand Down Expand Up @@ -107,6 +108,7 @@ int StartPushConsumer(CPushConsumer* consumer) {
try {
((DefaultMQPushConsumer*)consumer)->start();
} catch (exception& e) {
MQClientErrorContainer::instance()->setErr(string(e.what()));
return PUSHCONSUMER_START_FAILED;
}
return OK;
Expand Down