src: Move exception handling code to one function

This commit is contained in:
Tatsuhiro Tsujikawa 2015-10-17 23:34:05 +09:00
parent 11cb4ea214
commit eff3dfd50b
6 changed files with 25 additions and 52 deletions

View File

@ -456,14 +456,5 @@ int main(int argc, char **argv) {
} // namespace nghttp2
int main(int argc, char **argv) {
try {
return nghttp2::main(argc, argv);
} catch (std::bad_alloc &) {
fputs("Out of memory\n", stderr);
} catch (std::exception &x) {
fputs("Exception caught: ", stderr);
fputs(x.what(), stderr);
fputs("\n", stderr);
}
return EXIT_FAILURE;
return nghttp2::run_app(nghttp2::main, argc, argv);
}

View File

@ -49,6 +49,8 @@ extern "C" {
#include "comp_helper.h"
}
#include "template.h"
namespace nghttp2 {
typedef struct { int dump_header_table; } inflate_config;
@ -281,14 +283,5 @@ int main(int argc, char **argv) {
} // namespace nghttp2
int main(int argc, char **argv) {
try {
return nghttp2::main(argc, argv);
} catch (std::bad_alloc &) {
fputs("Out of memory\n", stderr);
} catch (std::exception &x) {
fputs("Exception caught: ", stderr);
fputs(x.what(), stderr);
fputs("\n", stderr);
}
return EXIT_FAILURE;
return nghttp2::run_app(nghttp2::main, argc, argv);
}

View File

@ -2714,14 +2714,5 @@ int main(int argc, char **argv) {
} // namespace nghttp2
int main(int argc, char **argv) {
try {
return nghttp2::main(argc, argv);
} catch (std::bad_alloc &) {
fputs("Out of memory\n", stderr);
} catch (std::exception &x) {
fputs("Exception caught: ", stderr);
fputs(x.what(), stderr);
fputs("\n", stderr);
}
return EXIT_FAILURE;
return nghttp2::run_app(nghttp2::main, argc, argv);
}

View File

@ -384,14 +384,5 @@ int main(int argc, char **argv) {
} // namespace nghttp2
int main(int argc, char **argv) {
try {
return nghttp2::main(argc, argv);
} catch (std::bad_alloc &) {
fputs("Out of memory\n", stderr);
} catch (std::exception &x) {
fputs("Exception caught: ", stderr);
fputs(x.what(), stderr);
fputs("\n", stderr);
}
return EXIT_FAILURE;
return nghttp2::run_app(nghttp2::main, argc, argv);
}

View File

@ -2551,15 +2551,4 @@ int main(int argc, char **argv) {
} // namespace shrpx
int main(int argc, char **argv) {
try {
return shrpx::main(argc, argv);
} catch (std::bad_alloc &) {
fputs("Out of memory\n", stderr);
} catch (std::exception &x) {
fputs("Exception caught: ", stderr);
fputs(x.what(), stderr);
fputs("\n", stderr);
}
return EXIT_FAILURE;
}
int main(int argc, char **argv) { return run_app(shrpx::main, argc, argv); }

View File

@ -28,6 +28,8 @@
#include "nghttp2_config.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <array>
#include <functional>
@ -196,6 +198,22 @@ inline std::unique_ptr<char[]> strcopy(const std::unique_ptr<char[]> &val) {
return strcopy(val.get());
}
inline int run_app(std::function<int(int, char **)> app, int argc,
char **argv) {
try {
return app(argc, argv);
} catch (const std::bad_alloc &) {
fputs("Out of memory\n", stderr);
} catch (const std::exception &x) {
fputs("Exception caught: ", stderr);
fputs(x.what(), stderr);
fputs("\n", stderr);
} catch (...) {
fputs("Custom exception caught\n", stderr);
}
return EXIT_FAILURE;
}
} // namespace nghttp2
#endif // TEMPLATE_H