Skip to content

Commit 10e6014

Browse files
committed
Add "Adding your own error / warning"
1 parent 8e8d989 commit 10e6014

3 files changed

Lines changed: 63 additions & 7 deletions

File tree

content/docs/API/error_types.mdx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
────────────────────────────────────────────────────────────────────────────────
133+
Traceback (most recent call last):
134+
(#00) File "/home/alvinng/Desktop/project/project.c", line 6 in main:
135+
<Error thrown here>
136+
CustomError1
137+
────────────────────────────────────────────────────────────────────────────────
80138
```

content/docs/index.mdx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
title: Get started
33
---
44

5-
import {Steps} from 'nextra/components'
6-
75
# Get started
86

97
Below is a quick guide for installing C Traceback into your project.
@@ -14,7 +12,7 @@ Before putting C Traceback into your project, you can first try the
1412
examples.
1513
Simply follow the steps below.
1614

17-
Note: `CMake` is required.
15+
Note: `CMake` is required for building examples.
1816

1917
<Steps>
2018
### Install CMake if needed
@@ -65,16 +63,15 @@ Note: `CMake` is required.
6563

6664
## Setting up a simple project
6765

68-
Adding C Traceback to your project is simple. However, the actual method
66+
Adding C Traceback to your project is simple. However, the actual setup
6967
depends on your directory structure. For demonstration, let's setup
7068
a simple project together:
7169

7270
<FileTree>
7371
<FileTree.Folder name="project" defaultOpen>
7472
<FileTree.File name="project.c" />
7573
<FileTree.File name="CMakeLists.txt" />
76-
<FileTree.Folder name="c_traceback">
77-
</FileTree.Folder>
74+
<FileTree.Folder name="c_traceback" />
7875
</FileTree.Folder>
7976
</FileTree>
8077

mdx-components.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useMDXComponents as getThemeComponents} from 'nextra-theme-docs';
2-
import {Callout, FileTree, Tabs} from "nextra/components";
2+
import {Callout, FileTree, Tabs, Steps} from "nextra/components";
33

44
import { Badge } from "components/ui/badge";
55

@@ -14,5 +14,6 @@ export function useMDXComponents() {
1414
Callout: Callout,
1515
FileTree: FileTree,
1616
Tabs: Tabs,
17+
Steps: Steps,
1718
}
1819
}

0 commit comments

Comments
 (0)