++ed by:

37 PAUSE users
19 non-PAUSE users.

Tony Cook


error.c - error reporting code for Imager


  // user code:
  int new_fatal; // non-zero if errors are fatal
  int old_fatal = i_set_failure_fatal(new_fatal);
  i_set_argv0("name of your program");
  extern void error_cb(char const *);
  i_error_cb old_ecb;
  old_ecb = i_set_error_cb(error_cb);
  i_failed_cb old_fcb;
  extern void failed_cb(char **errors);
  old_fcb = i_set_failed_cb(failed_cb);
  if (!i_something(...)) {
    char **errors = i_errors();

  // imager code:
  undef_int i_something(...) {
    if (!some_lower_func(...)) {
      return i_failed("could not something");
    return 1;
  undef_int some_lower_func(...) {
    if (somethingelse_failed()) {
      i_push_error("could not somethingelse");
      return 0;
    return 1;


This module provides the C level error handling functionality for Imager.

A few functions return or pass in an i_errmsg *, this is list of error structures, terminated by an entry with a NULL msg value, each of which contains a msg and an error code. Even though these aren't passed as i_errmsg const * pointers, don't modify the strings or the pointers.

The interface as currently defined isn't thread safe, unfortunately.

This code uses Imager's mymalloc() for memory allocation, so out of memory errors are always fatal.


These functions form the interface that a user of Imager sees (from C). The Perl level won't use all of this.

im_errors(ctx) =synopsis i_errmsg *errors = im_errors(aIMCTX); =synopsis i_errmsg *errors = i_errors();

Returns a pointer to the first element of an array of error messages, terminated by a NULL pointer. The highest level message is first.

Also callable as i_errors().


These functions are called by Imager to report errors through the above interface.

It may be desirable to have functions to mark the stack and reset to the mark.

im_clear_error(ctx) =synopsis im_clear_error(aIMCTX); =synopsis i_clear_error(); =category Error handling

Clears the error stack.

Called by any Imager function before doing any other processing.

Also callable as i_clear_error().

im_push_error(ctx, code, message) =synopsis i_push_error(0, "Yep, it's broken"); =synopsis i_push_error(errno, "Error writing"); =synopsis im_push_error(aIMCTX, 0, "Something is wrong"); =category Error handling

Called by an Imager function to push an error message onto the stack.

No message is pushed if the stack is full (since this means someone forgot to call i_clear_error(), or that a function that doesn't do error handling is calling function that does.).

im_push_errorvf(ctx, code, format, args) =synopsis va_args args; =synopsis va_start(args, lastarg); =synopsis im_push_errorvf(ctx, code, format, args); =category Error handling

Intended for use by higher level functions, takes a varargs pointer and a format to produce the finally pushed error message.

Does not support perl specific format codes.

Also callable as i_push_errorvf(code, format, args)

i_push_errorf(int code, char const *fmt, ...) =synopsis i_push_errorf(errno, "Cannot open file %s: %d", filename, errno); =category Error handling

A version of i_push_error() that does printf() like formatting.

Does not support perl specific format codes.

im_push_errorf(ctx, code, char const *fmt, ...) =synopsis im_push_errorf(aIMCTX, errno, "Cannot open file %s: %d", filename, errno); =category Error handling

A version of im_push_error() that does printf() like formatting.

Does not support perl specific format codes.

i_failed(char const *msg)

Called by Imager code to indicate that a top-level has failed.

msg can be NULL, in which case no error is pushed.

Calls the current failed callback, if any.

Aborts the program with an error, if failures have been set to be fatal.

Returns zero if it does not abort.

im_assert_fail(file, line, message)

Called when an im_assert() assertion fails.

Only available when Imager is built with assertions.


This interface isn't thread safe.


Tony Cook <tony@develop-help.com>

Stack concept by Arnar Mar Hrafnkelsson <addi@umich.edu>