@@ -77,4 +77,62 @@ screaming snake case. For example, `ResourceWarning` becomes `CTB_RESOURCE_WARNI
7777 ├── SecurityWarning
7878 ├── ParseWarning
7979 └── UserWarning
80+ ```
81+
82+ ## Adding your own error / warning
83+ It is easy to add your own error / warning code. Let's say we want to add ` CustomError1 ` ,
84+ simply follow the steps:
85+ <Steps >
86+ ### Add error code at ` include/c_traceback/error_codes.h `
87+ Add custom error code at the bottom of ` typedef enum CTB_Error ` :
88+ ```c { 14 }
89+ typedef enum CTB_Error
90+ {
91+
92+ CTB_SUCCESS = 0 ,
93+ CTB_UNKNOWN_ERROR = - 1 ,
94+
95+ ...
96+
97+ /* --- Dynamic Link Errors (900-999) --- */
98+ CTB_DYNAMIC_LINK_ERROR = 900 ,
99+ CTB_LIBRARY_LOAD_ERROR ,
100+
101+ /* --- 1000+: application-specific --- */
102+ CUSTOM_ERROR_1 = 1000 ,
103+ }
104+ ```
105+
106+ ### Add name mapping at ` src/error_codes.c `
107+ Add the mapping at the bottom of ` error_to_string(CTB_Error error) ` :
108+ ```c { 15 }
109+ const char * error_to_string(CTB_Error error)
110+ {
111+ switch (error )
112+ {
113+ case CTB_SUCCESS: return " Success" ;
114+ case CTB_UNKNOWN_ERROR : return " Unknown Error" ;
115+
116+ ...
117+
118+ /* Dynamic Link */
119+ case CTB_DYNAMIC_LINK_ERROR : return " DynamicLinkError" ;
120+ case CTB_LIBRARY_LOAD_ERROR : return " LibraryLoadError" ;
121+
122+ /* Application */
123+ case CUSTOM_ERROR_1 : return " CustomError1" ;
124+
125+ default : return " Unrecognized Error Code" ;
126+ }
127+ }
128+ ```
129+ </Steps >
130+ Now you should be able to use it directly:
131+ ``` ansi
132+ [0;31m────────────────────────────────────────────────────────────────────────────────[0m
133+ [1;31mTraceback[0m [0;31m(most recent call last):[0m
134+ [38;5;240m(#00)[0m [38;5;240mFile "[0m[38;5;240m/home/alvinng/Desktop/project/[0m[1;36mproject.c[0m[38;5;240m", line[0m [1;36m6[0m [38;5;240min[0m [1;36mmain[0m:
135+ [0;31m<Error thrown here>[0m
136+ [1;31mCustomError1[0m
137+ [0;31m────────────────────────────────────────────────────────────────────────────────[0m
80138```
0 commit comments