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

Replace std::cerr and exit(1) with throw spffl::exception_t #37

Merged
merged 5 commits into from
Apr 30, 2023
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
12 changes: 11 additions & 1 deletion cli/spiff/spiff_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdio.h>
#include <string.h>

#include "spffl_exception.h"

#include "cmd_interp.h"

#include "handlers.h"
Expand Down Expand Up @@ -237,7 +239,7 @@ void main_usage(char *argv0) {
exit(1);
}

int main(int argc, char **argv) {
static int try_main(int argc, char **argv) {
char *exename = argv[0];
argc--, argv++;

Expand Down Expand Up @@ -267,3 +269,11 @@ int main(int argc, char **argv) {
std::cerr << exename << ": subcommand \"" << argv[0] << "\" not found.\n";
return 1;
}

int main(int argc, char **argv) {
try {
try_main(argc, argv);
} catch (spffl::exception_t e) {
std::cerr << e.what() << "\n";
}
}
1 change: 1 addition & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required (VERSION 3.24)
project(base)

add_library(${PROJECT_NAME}
spffl_exception.cpp
tokenize.cpp
line_scan.cpp
)
9 changes: 9 additions & 0 deletions src/base/spffl_exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "spffl_exception.h"

namespace spffl {

exception_t::exception_t(const std::string &msg) : msg(msg) {}

const char *exception_t::what() const noexcept { return this->msg.c_str(); }

} // namespace spffl
21 changes: 21 additions & 0 deletions src/base/spffl_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef SPFFL_EXCEPTION_H
#define SPFFL_EXCEPTION_H

#include <exception>
#include <sstream>
#include <string>

namespace spffl {

class exception_t : public std::exception {
public:
exception_t(const std::string &msg);
virtual const char *what() const noexcept;

private:
std::string msg;
};

} // namespace spffl

#endif // SPFFL_EXCEPTION_H
Loading