C 標準庫 errno + strerror() 僅提供通用錯誤碼,無法表達業務邏輯錯誤、元件內部狀態、多層級嚴重度。發布供第三方使用的 Linux 共用庫/元件時,完善的回傳訊息機制能大幅提升開發者體驗 (DX),減少除錯時間。
+----------------+----------------+----------------+----------------+
| 類型 (4 bits) | 元件 ID (8 bits) | 保留 (4 bits) | 具體錯誤碼 (16 bits) |
+----------------+----------------+----------------+----------------+
31-28 27-20 19-16 15-0
| 類型 | 十六進位前綴 | 十進位範圍 | 語意 |
|---|---|---|---|
| 成功 | 0x1xxxxxxx | 正數 | 正常返回 |
| 資訊 | 0x2xxxxxxx | 正數 | 非錯誤狀態通知 |
| 警告 | 0x8xxxxxxx | 負數 | 潛在問題但繼續執行 |
| 一般錯誤 | 0x9xxxxxxx | 負數 | 操作失敗,需處理 |
| 嚴重錯誤 | 0xAxxxxxxx | 負數 | 系統/元件損壞,需立即中止 |
| 語意 | 具體碼 | 完整 32 位編碼 | 十六進位 |
|---|---|---|---|
| 成功 | 0 | 0x100B0000 | 0x100B0000 |
| 交易定義錯誤 | 1 | 0x900B0001 | 警告類 |
| 交易提交錯誤 | 2 | 0x900B0002 | 一般錯誤 |
| 交易回滾錯誤 | 3 | 0xA00B0003 | 嚴重錯誤 |
每個元件一個靜態陣列,索引對應具體錯誤碼 (低 16 位)。
/* txn_security_messages.c */
static const char *txn_security_msgs[] = {
[0] = "成功返回",
[1] = "交易定義錯誤:參數不合法或缺少必要欄位",
[2] = "交易提交錯誤:資料庫鎖衝突或約束違反",
[3] = "交易回滾錯誤:日誌損壞或資源耗盡",
[4] = NULL /* 結束標記 */
};
/* 導出符號供核心註冊機制查找 */
const char **get_txn_security_messages(void) {
return txn_security_msgs;
}
/* component_registry.c */
#include
#include
#include
#include
#define MAX_COMPONENTS 256
static const char **msg_tables[MAX_COMPONENTS] = {NULL};
/* 元件初始化時呼叫 (GCC 建構函數屬性) */
__attribute__((constructor))
void register_component_messages(int component_id, const char *symbol_name) {
void *handle = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL);
if (!handle) return;
const char **(*getter)(void) = dlsym(handle, symbol_name);
if (getter) {
msg_tables[component_id] = getter();
}
dlclose(handle);
}
/* 公開 API:依錯誤碼取得文字說明 */
const char *component_strerror(int ret_code) {
if (ret_code == 0) return "Success"; // 通用成功
int err_code = ret_code & 0xFFFF; // 低 16 位
int comp_id = (ret_code >> 16) & 0xFF; // 第 2 字節
if (comp_id < MAX_COMPONENTS && msg_tables[comp_id] &&
err_code < 65536 && msg_tables[comp_id][err_code]) {
return msg_tables[comp_id][err_code];
}
return "Unknown error code";
}
/* txn_security.c */
#include "component_registry.h"
#define TXN_COMP_ID 0x0B
/* 定義錯誤碼常數 */
#define TXN_SUCCESS 0x100B0000
#define TXN_ERR_DEFINITION 0x900B0001
#define TXN_ERR_COMMIT 0x900B0002
#define TXN_ERR_ROLLBACK 0xA00B0003
/* 註冊訊息表 (建構函數自動執行) */
__attribute__((constructor))
static void init_txn_messages() {
register_component_messages(TXN_COMP_ID, "get_txn_security_messages");
}
/* 業務函數範例 */
int txn_commit(txn_handle *h) {
if (!h || !h->valid) return TXN_ERR_DEFINITION;
if (db_commit(h->db) != 0) return TXN_ERR_COMMIT;
return TXN_SUCCESS;
}
#include
#include "txn_security.h"
#include "component_registry.h"
int main() {
txn_handle *h = txn_begin();
int ret = txn_commit(h);
if (ret != TXN_SUCCESS) {
fprintf(stderr, "交易失敗 [0x%08X]: %s\n", ret, component_strerror(ret));
// 輸出: 交易失敗 [0x900B0002]: 交易提交錯誤:資料庫鎖衝突或約束違反
}
return 0;
}
訊息表改為 const char *msg_table[][LOCALE_COUNT],依 LANG 環境變數選擇語言欄。
訊息表存儲格式字串,component_strerror 支援 va_list 參數格式化:
[2] = "交易提交失敗:錯誤碼 %d, SQLSTATE %s"
成功碼保留 0,失敗碼高位編碼元件/類型,低 16 位可映射標準 errno 值 (如 EINVAL=22),component_strerror 先查自訂表,再 fallback strerror(err_code)。
使用 __thread 或 pthread_once 初始化快取,避免重複 dlsym 開銷。
dlopen 即可用。component_strerror 暴露為 C API,SWIG/ctypes/cgo 包裝。優雅的回傳訊息機制是專業 Linux 元件的標誌。透過位域編碼錯誤碼、靜態訊息表、動態註冊、統一查詢 API,即可實現類型安全、元件隔離、國際化就緒、零配置使用的錯誤處理體驗。這不僅提升開發效率,更是元件商業化交付的質量門檻。