feat: improve error handling

This commit is contained in:
LordMZTE 2022-11-07 01:01:36 +01:00
parent dbe98d0b2b
commit af1141b85d
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
7 changed files with 23 additions and 16 deletions

View File

@ -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;

View File

@ -1,3 +1,4 @@
#include <stdexcept>
#include <sys/types.h>
namespace client {

View File

@ -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;

View File

@ -1,3 +1,4 @@
#include <stdexcept>
#include <sys/types.h>
namespace data {

View File

@ -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!";
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;
}
return 0;
}

View File

@ -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;

View File

@ -1,3 +1,4 @@
#include <stdexcept>
#include <sys/types.h>
namespace server {