Skip to content

Commit 50143d9

Browse files
committed
feat: encapsulate IPrintExceptionImpl in internals namespace; enhance exception handling macros
1 parent 30fa2aa commit 50143d9

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/helper/APIHelper.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void ToString(Local<Object> const& value, std::ostringstream& oss) {
128128
}
129129
}
130130

131-
131+
namespace internals {
132132
void IPrintExceptionImpl(
133133
std::string const& exceptionFullName,
134134
std::string const& exceptionMessage,
@@ -143,6 +143,7 @@ void IPrintExceptionImpl(
143143
ptr->getLogger().error("Exception plugin: {}", plugin);
144144
ptr->getLogger().error("Exception Type: {}\n{}\n{}", exceptionFullName, exceptionMessage, stackTrace);
145145
}
146+
} // namespace internals
146147

147148

148149
} // namespace jse

src/helper/APIHelper.h

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ template <typename T>
1818
return EngineScope::currentEngine()->isInstanceOf<T>(value);
1919
}
2020

21+
2122
// ============
2223
// 异常处理
2324
// ============
25+
#define ERR_WRONG_ARG_TYPE "Wrong argument type, please check the documentation" /* 参数类型错误,请查阅文档 */
26+
#define ERR_TOO_FEW_ARGS "Too few arguments" /* 参数数量不足 */
27+
#define ERR_WRONG_ARGS_COUNT "Wrong arguments count" /* 参数数量错误 */
28+
29+
namespace internals {
2430
void IPrintExceptionImpl(
2531
std::string const& exceptionFullName, // 异常全名
2632
std::string const& exceptionMessage, // 异常信息
@@ -29,44 +35,59 @@ void IPrintExceptionImpl(
2935
std::string const& funcFullName, // 函数全名
3036
std::string const& plugin // 插件名
3137
);
38+
}
3239

33-
#define PRINT_SCRIPT_EXCEPTION(EXC_NAME, EXC_INSTANCE) \
34-
IPrintExceptionImpl( \
40+
#define PRINT_SCRIPT_EXCEPTION_WITH_FUNC(EXC_NAME, EXC_INSTANCE, FUNC, FUNC_FULL) \
41+
internals::IPrintExceptionImpl( \
3542
EXC_NAME, \
3643
(EXC_INSTANCE).message(), \
3744
(EXC_INSTANCE).stacktrace(), \
38-
__func__, \
39-
__FUNCTION__, \
45+
FUNC, \
46+
FUNC_FULL, \
4047
ENGINE_DATA()->mFileName \
4148
)
4249

43-
#define ICATCH_IMPL(RET_CODE) \
50+
// 默认使用当前函数名的宏
51+
#define PRINT_SCRIPT_EXCEPTION(EXC_NAME, EXC_INSTANCE) \
52+
PRINT_SCRIPT_EXCEPTION_WITH_FUNC(EXC_NAME, EXC_INSTANCE, __func__, __FUNCTION__)
53+
54+
// 通用异常捕获实现
55+
#define ICATCH_IMPL_GENERIC(FUNC, FUNC_FULL, ...) \
4456
catch (script::Exception const& err) { \
45-
PRINT_SCRIPT_EXCEPTION("script::Exception", err); \
46-
RET_CODE; \
57+
PRINT_SCRIPT_EXCEPTION_WITH_FUNC("script::Exception", err, FUNC, FUNC_FULL); \
58+
__VA_ARGS__; \
4759
} \
4860
catch (std::exception const& err) { \
49-
PRINT_SCRIPT_EXCEPTION("std::exception", script::Exception(err.what())); \
50-
RET_CODE; \
61+
PRINT_SCRIPT_EXCEPTION_WITH_FUNC("std::exception", script::Exception(err.what()), FUNC, FUNC_FULL); \
62+
__VA_ARGS__; \
5163
} \
5264
catch (...) { \
53-
PRINT_SCRIPT_EXCEPTION("unknown", script::Exception("Unknown exception")); \
54-
RET_CODE; \
65+
PRINT_SCRIPT_EXCEPTION_WITH_FUNC("unknown", script::Exception("Unknown exception"), FUNC, FUNC_FULL); \
66+
__VA_ARGS__; \
5567
}
5668

57-
#define ERR_WRONG_ARG_TYPE "Wrong argument type, please check the documentation" /* 参数类型错误,请查阅文档 */
58-
#define ERR_TOO_FEW_ARGS "Too few arguments" /* 参数数量不足 */
59-
#define ERR_WRONG_ARGS_COUNT "Wrong arguments count" /* 参数数量错误 */
60-
61-
/* 仅捕获异常 */
62-
#define CatchNotReturn ICATCH_IMPL()
69+
/* 仅捕获异常(默认) */
70+
#define CatchNotReturn ICATCH_IMPL_GENERIC(__func__, __FUNCTION__)
6371

64-
/* 捕获异常并返回自定义类型 */
65-
#define CatchAndReturn(RET_VALUE) ICATCH_IMPL(return (RET_VALUE))
72+
/* 捕获异常并返回自定义类型(默认) */
73+
#define CatchAndReturn(RET_VALUE) ICATCH_IMPL_GENERIC(__func__, __FUNCTION__, return (RET_VALUE))
6674

67-
/* 捕获异常并返回 undefined */
75+
/* 捕获异常并返回 undefined(默认) */
6876
#define Catch CatchAndReturn(Local<Value>())
6977

78+
// ============
79+
// Lambda专用宏
80+
// ============
81+
/* 捕获异常并返回自定义类型(需指定函数信息) */
82+
#define CatchLambdaAndReturn(FUNC, FUNC_FULL, RET_VALUE) ICATCH_IMPL_GENERIC(FUNC, FUNC_FULL, return (RET_VALUE))
83+
84+
/* 捕获异常并返回 undefined(需指定函数信息) */
85+
#define CatchLambda(FUNC, FUNC_FULL) CatchLambdaAndReturn(FUNC, FUNC_FULL, Local<Value>())
86+
87+
/* 仅捕获异常(需指定函数信息) */
88+
#define CatchLambdaNotReturn(FUNC, FUNC_FULL) ICATCH_IMPL_GENERIC(FUNC, FUNC_FULL)
89+
90+
7091
/* 捕获异常并抛回脚本层 */
7192
#define CatchAndThrow \
7293
catch (script::Exception const& e) { \

0 commit comments

Comments
 (0)