squareball  0.2.0.50-8649
A general-purpose library for C99
sb-error.h File Reference

Error reporting infrastructure. More...

#include <stdarg.h>
#include <stdlib.h>
#include "sb-mem.h"

Go to the source code of this file.

Data Structures

struct  sb_error_type_t
 
typedef char *(* sb_error_to_string_func_t) (void *data)
 
typedef struct _sb_error_t sb_error_t
 
sb_error_tsb_error_new_from_type (sb_error_type_t *type, void *data)
 
const char * sb_error_get_type_name (sb_error_t *err)
 
const void * sb_error_get_data (sb_error_t *err)
 
const char * sb_error_to_string (sb_error_t *err)
 
void sb_error_free (sb_error_t *err)
 

Detailed Description

Error reporting infrastructure.

This implementation is heavily inspired by Golang's error reporting. The main error object, that is used internally by squareball, and is available for users, is just a string. See squareball/sb-strerror.h.

This header provides all the infrastructure needed to manipulate error objects in a generic way, without caring about what is stored in the error object, and to create custom error types.

Typedef Documentation

typedef struct _sb_error_t sb_error_t

Opaque data structure that represents an error object.

typedef char*(* sb_error_to_string_func_t) (void *data)

Function signature for the function that is used to convert a custom error data to string.

This should only be used when creating a custom error type.

Parameters
dataA pointer to custom data stored in the object.
Returns
A newly-allocated string.

Function Documentation

void sb_error_free ( sb_error_t err)

Function that frees memory allocated for an error object.

Parameters
errAn error object.
Examples:
hello_configparser.c, hello_custom_error_type.c, hello_dir_create.c, hello_file_read.c, and hello_file_write.c.
const void* sb_error_get_data ( sb_error_t err)

Function that returns a pointer to data stored in an error object.

Parameters
errAn error object.
Returns
A pointer to data object.
const char* sb_error_get_type_name ( sb_error_t err)

Function that returns the name of the error type.

Parameters
errAn error object.
Returns
A string.
sb_error_t* sb_error_new_from_type ( sb_error_type_t type,
void *  data 
)

Function that creates a new error object from a custom error type and data.

Parameters
typeA pointer to a custom data type (sb_error_type_t) object.
dataA pointer to data object.
Returns
A newly-allocated error object.
Examples:
hello_custom_error_type.c.
const char* sb_error_to_string ( sb_error_t err)

Function that returns a string representation of the data stored in an error object.

Parameters
errAn error object.
Returns
A string.
Examples:
hello_configparser.c, hello_custom_error_type.c, hello_dir_create.c, hello_file_read.c, and hello_file_write.c.