feat: improve error handling
This commit is contained in:
parent
dbe98d0b2b
commit
af1141b85d
|
@ -4,14 +4,14 @@
|
|||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/msg.h>
|
||||
|
||||
namespace client {
|
||||
void sendMsg(int msqid, data::RequestMapMsg *msg) {
|
||||
if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno);
|
||||
exit(1);
|
||||
throw std::runtime_error(std::string("send error ") + strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,8 +19,7 @@ std::string recvMsg(int msqid) {
|
|||
data::ResponseMapMsg msg;
|
||||
if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long),
|
||||
data::clientbound_msg, 0) < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "receive error " << strerror(errno);
|
||||
exit(1);
|
||||
throw std::runtime_error(std::string("receive error ") + strerror(errno));
|
||||
}
|
||||
|
||||
return std::string(&msg.data[0], msg.datalen);
|
||||
|
@ -33,8 +32,7 @@ void map(key_t key) {
|
|||
auto msqid = msgget(key, 0664);
|
||||
|
||||
if (msqid < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "msgget fail";
|
||||
exit(0);
|
||||
throw std::runtime_error("msgget fail. is the server running?");
|
||||
}
|
||||
|
||||
data::RequestMapMsg req;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <stdexcept>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace client {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "data.hpp"
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
#include <sys/ipc.h>
|
||||
|
||||
namespace data {
|
||||
|
@ -8,8 +9,7 @@ key_t getIpcKeyFromExeName(char *argv0) {
|
|||
auto k = ftok(argv0, 'X');
|
||||
|
||||
if (k < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "failed to get IPC key";
|
||||
exit(1);
|
||||
throw std::runtime_error("failed to get IPC key");
|
||||
}
|
||||
|
||||
return k;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <stdexcept>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace data {
|
||||
|
|
17
src/main.cpp
17
src/main.cpp
|
@ -3,11 +3,11 @@
|
|||
#include "server.hpp"
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
void main2(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "not enough arguments!";
|
||||
return 1;
|
||||
throw std::runtime_error("not enough arguments!");
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "serve") == 0) {
|
||||
|
@ -17,9 +17,16 @@ int main(int argc, char *argv[]) {
|
|||
} else if (strcmp(argv[1], "resourceloc") == 0) {
|
||||
client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0]));
|
||||
} else {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "unknown command!";
|
||||
return 1;
|
||||
throw std::runtime_error("unknown command!");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
main2(argc, argv);
|
||||
return 0;
|
||||
} catch (const std::exception &e) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,7 @@ void run(key_t key) {
|
|||
auto msqid = msgget(key, 0664 | IPC_CREAT);
|
||||
|
||||
if (msqid < 0) {
|
||||
BOOST_LOG_TRIVIAL(fatal) << "msgget fail";
|
||||
exit(1);
|
||||
throw std::runtime_error("msgget fail");
|
||||
}
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "msqid " << msqid;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <stdexcept>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace server {
|
||||
|
|
Loading…
Reference in a new issue