Skip to content

Commit 06da19e

Browse files
committed
Almost done with the error related functions
1 parent 3687151 commit 06da19e

File tree

2 files changed

+34
-92
lines changed

2 files changed

+34
-92
lines changed

doc/user_manual.md

Lines changed: 32 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -124,125 +124,65 @@ struct cjlib_json_error
124124

125125
# Error related functions
126126
Cjlib provides the errors by manipulating an internal (private) variable in the implementation of cjlib_error.c file. This is done to protect it from mistaken actions. To access the private variable that has the information about the error that occurred, the library provides the following functions. Each of the following functions is **thread safe**, as it uses mutex, to allow multiple threads to check for errors (if this is necessary).
127-
128127
___
129-
## cjlib_json_error_init
128+
## cjlib_json_get_error
130129
___
131130
### Description
132-
It is used to make the required initializations on the memory where the internal structure representing the errors is stored.
133-
134-
### Function signature
135-
```C
136-
int cjlib_json_error_init(void)
137-
```
138-
139-
### Parameters
140-
No parameters are required.
141-
142-
### Return
143-
An integer indicating whether the initialization succeeded. If succeed, 0 is returned, otherwise -1.
131+
The cjlib_json_get error is leveraged to request the error produced by a function. It is typically used after a function fails to get more details about the error that occurred internally in the library. Each of the library functions returns a subset of the available errors; thus, there is no need to check for all the errors for each function. The possible errors from each library function are documented.
144132

145-
### Errors
146-
No specific error is returned.
147-
148-
### Example
149-
```C
150-
#include "cjlib_error.h"
151-
#include <stdio.h>
152-
#include <stdlib.h>
153-
154-
int main(void) {
155-
if (-1 == cjlib_json_error_init()) {
156-
(void) printf("Failed to initialize JSON errors\n");
157-
exit(EXIT_FAILURE);
158-
}
159-
return EXIT_SUCCESS;
160-
}
161-
```
162-
163-
___
164-
## cjlib_json_error_destroy
165-
___
166-
### Description
167-
Is used to release the resources that was allocated for the internal error structure.
133+
The user may ignore the errors retrieved from the internal error structure but must always check if a library function is erroneous. Otherwise, unexpected behavior may occur, breaking the program.
168134

169135
### Function signature
170136
```C
171-
void cjlib_json_error_destroy(void);
137+
void cjlib_json_get_error(struct cjlib_json_error *restrict dst);
172138
```
173139
174140
### Parameters
175-
No parameters are required.
141+
**dst**: A pointer to the memory region to store the details about the error occured.
176142
177143
### Return
178-
Nothing is returned.
144+
--
179145
180146
### Errors
181-
No specific error is returned.
147+
--
182148
183149
### Example
184150
```C
185151
#include "cjlib_error.h"
186152
#include <stdio.h>
187153
#include <stdlib.h>
188154
189-
int main(void) {
190-
// Initialize the componentes of the error structure.
191-
if (-1 == cjlib_json_error_init()) {
192-
(void) printf("Failed to initialize JSON errors\n");
193-
exit(EXIT_FAILURE);
155+
int main(void) {
156+
// Oh!, an error occured during a hypithetical function from the library.
157+
// Try to get details on where and why the error were occured.
158+
159+
struct cjlib_json_error error_details;
160+
cjlib_json_get_error(&error_details);
161+
162+
// OK, lets see if the library have some more helpful details, like which element caused the error.
163+
164+
if (NULL != error_details.c_property_name) {
165+
(void) printf("Errornus propery name: %s\n", error_details.c_property_name);
194166
}
195167
196-
// Code...
168+
// Lets see if we can retrieve the value of the property.
169+
if (NULL != error_details.c_property_value) {
170+
(void) printf("Errornus property value: %s\n", error_details.c_property_value);
171+
}
197172
198-
// Release the components.
199-
cjlib_json_error_destroy();
173+
switch (error_details.c_error_code) {
174+
case INVALID_TYPE:
175+
(void) printf("Ah!, there is a type that is not valid for the JSON standard!\n")
176+
break;
177+
case MISSING_SEPERATOR:
178+
(void) printf("Somewhere in the the JSON file, there must be a separator: but there is not\n")
179+
180+
...
181+
}
182+
200183
return EXIT_SUCCESS
201184
}
202-
```
203185
204-
___
205-
## cjlib_setup_error
206-
___
207-
### Description
208-
209-
### Function signature
210-
```C
211-
212-
```
213-
214-
### Parameters
215-
216-
### Return
217-
--
218-
219-
### Errors
220-
--
221-
222-
### Example
223-
```C
224-
```
225-
226-
___
227-
## cjlib_json_get_error
228-
___
229-
### Description
230-
231-
### Function signature
232-
```C
233-
234-
```
235-
236-
### Parameters
237-
238-
### Return
239-
--
240-
241-
### Errors
242-
--
243-
244-
### Example
245-
```C
246186
```
247187

248188
___

src/cjlib.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ void cjlib_json_close(struct cjlib_json *restrict src)
185185
cjlib_json_destroy(src);
186186
fclose(src->c_fp);
187187
(void) memset(src, 0x0, sizeof(struct cjlib_json));
188+
189+
cjlib_json_error_destroy();
188190
}
189191

190192
static CJLIB_ALWAYS_INLINE bool is_number(const char *restrict src)

0 commit comments

Comments
 (0)