Context

The context is used to facilitate error handling. The context struct itself should not be considered public and is subject to change.

Constants

The err field of the context is set to one of the following enum values:

#include <ndtypes.h>

enum ndt_error {
  NDT_Success,
  NDT_ValueError,
  NDT_TypeError,
  NDT_InvalidArgumentError,
  NDT_NotImplementedError,
  NDT_LexError,
  NDT_ParseError,
  NDT_OSError,
  NDT_RuntimeError,
  NDT_MemoryError
};

Static contexts

NDT_STATIC_CONTEXT(ctx);

This creates a static context, usually a local variable in a function. Error messages may be dynamically allocated, so ndt_context_del must be called on static contexts, too.

Functions

ndt_context_t *ndt_context_new(void);
void ndt_context_del(ndt_context_t *ctx);

Create an initialized context or delete a context. It is safe to call ndt_context_del on both dynamic and static contexts.

void ndt_err_format(ndt_context_t *ctx, enum ndt_error err, const char *fmt, ...);

Set a context’s error constant and error message. fmt may contain the same format specifiers as printf.

int ndt_err_occurred(const ndt_context_t *ctx);

Check if an error has occurred.

void ndt_err_clear(ndt_context_t *ctx);

Clear an error.

void *ndt_memory_error(ndt_context_t *ctx);

Convenience function. Set NDT_MemoryError and return NULL;

const char *ndt_err_as_string(enum ndt_error err);

Get the string representation of an error constant.

const char *ndt_context_msg(ndt_context_t *ctx);

Get the current error string. It is safe to call this function if no error has occurred, in which case the string is Success.

ndt_err_fprint(FILE *fp, ndt_context_t *ctx);

Print an error to fp. Mostly useful for debugging.