Source

arana-main / arana / error.h

/*
 * error.h
 *
 * (c) Copyright 2009 by Armin Ronacher.
 */

#ifndef _ARANA_ERROR_H
#define _ARANA_ERROR_H

/* error catching. raising is in object.h */
#define AR_RESCUE(t, r, c) arana_rescue(AR_ISIG, (ArTryFunc)(t), (ArRescueFunc)(r), c)

/* shortcuts for error creation */
#define AR_ERROR(msg) arana_error_new(AR_ISIG, AR_STRING(msg))
#define AR_INTERNAL_ERROR(msg) arana_internal_error_new(AR_ISIG, AR_STRING(msg))
#define AR_SYNTAX_ERROR(msg, filename, lineno, column) arana_syntax_error_new(AR_ISIG, AR_STRING(msg), AR_STRING(filename), lineno, column)
#define AR_TYPE_ERROR(msg) arana_type_error_new(AR_ISIG, AR_STRING(msg))
#define AR_ARGUMENT_ERROR(arg, pos, msg) arana_argument_error_new(AR_ISIG, arg, pos, AR_STRING(msg))
#define AR_MEMORY_ERROR(msg) arana_memory_error_new(AR_ISIG, AR_STRING(msg))
#define AR_INDEX_ERROR(index, msg) arana_index_error_new(AR_ISIG, index, AR_STRING(msg))
#define AR_NAME_ERROR(name) arana_name_error_new(AR_ISIG, name)
#define AR_ATTRIBUTE_ERROR(attr, msg) arana_attribute_error_new(AR_ISIG, attr, AR_STRING(msg))
#define AR_STOP_ITERATION() arana_stop_iteration_error_new(AR_ISIG)
#define AR_IO_ERROR(message) arana_io_error_new(AR_ISIG, AR_STRING(message))

/* error creation helper */
#define AR_ERROR_HEAD AR_OBJECT_HEAD; AR message;
#define AR_ERROR_INIT(err, msg) (((ArError *)(err))->message = msg)

struct _ArTraceback {
    const char *filename;
    const char *function;
    int lineno;
    struct _ArTraceback *next;
};

typedef struct _ArError {
    AR_ERROR_HEAD;
} ArError;

typedef struct _ArInternalError {
    AR_ERROR_HEAD;
} ArInternalError;

typedef struct _ArMemoryError {
    AR_ERROR_HEAD;
} ArMemoryError;

typedef struct _ArSyntaxError {
    AR_ERROR_HEAD;
    AR filename;
    int lineno;
    int column;
} ArSyntaxError;

typedef struct _ArTypeError {
    AR_ERROR_HEAD;
} ArTypeError;

typedef struct _ArIndexError {
    AR_ERROR_HEAD;
    Ar_size_t index;
} ArIndexError;

typedef struct _ArNameError {
    AR_ERROR_HEAD;
    const char *name;
} ArNameError;

typedef struct _ArAttributeError {
    AR_ERROR_HEAD;
    const char *attr;
} ArAttributeError;

typedef struct _ArArgumentError {
    AR_ERROR_HEAD;
    const char *arg_name;
    Ar_size_t arg_pos;
} ArArgumentError;

typedef struct _ArStopIteration {
    AR_ERROR_HEAD;
} ArStopIteration;

typedef struct _ArIOError {
    AR_ERROR_HEAD;
} ArIOError;

typedef void * (*ArTryFunc)(AR_SIG, void *closure);
typedef void * (*ArRescueFunc)(AR_SIG, AR error, void *closure);

AR_API_FUNC(void) arana_traceback_push(AR_SIG, const char *filename,
                                       int lineno, const char *function);
AR_API_FUNC(void) arana_traceback_pop(AR_SIG);
AR_API_FUNC(void) arana_traceback_print(AR_SIG);
AR_API_FUNC(void) arana_error_jump(AR_SIG);
AR_API_FUNC(void) arana_default_error_handler(AR_SIG);
AR_API_FUNC(void *) arana_rescue(AR_SIG, ArTryFunc try_func,
                                ArRescueFunc rescue_func, void *closure);
AR_API_FUNC(AR) arana_create_error_module(AR_SIG);

AR_API_FUNC(AR) arana_new_error(AR_SIG, AR message);
AR_API_FUNC(AR) arana_internal_error_new(AR_SIG, AR message);
AR_API_FUNC(AR) arana_syntax_error_new(AR_SIG, AR message, AR filename,
                                       int lineno, int column);
AR_API_FUNC(AR) arana_type_error_new(AR_SIG, AR message);
AR_API_FUNC(AR) arana_argument_error_new(AR_SIG, const char *name,
                                         Ar_size_t pos, AR message);
AR_API_FUNC(AR) arana_memory_error_new(AR_SIG, AR message);
AR_API_FUNC(AR) arana_index_error_new(AR_SIG, Ar_size_t index, AR message);
AR_API_FUNC(AR) arana_name_error_new(AR_SIG, const char *name);
AR_API_FUNC(AR) arana_attribute_error_new(AR_SIG, const char *attr,
                                          AR message);
AR_API_FUNC(AR) arana_stop_iteration_error_new(AR_SIG);
AR_API_FUNC(AR) arana_io_error_new(AR_SIG, AR message);
AR_API_FUNC(void) arana_out_of_memory(AR_SIG);

AR_API_DATA(ArObjectType) ArErrorType;
AR_API_DATA(ArObjectType) ArInternalErrorType;
AR_API_DATA(ArObjectType) ArSyntaxErrorType;
AR_API_DATA(ArObjectType) ArTypeErrorType;
AR_API_DATA(ArObjectType) ArArgumentErrorType;
AR_API_DATA(ArObjectType) ArMemoryErrorType;
AR_API_DATA(ArObjectType) ArIndexErrorType;
AR_API_DATA(ArObjectType) ArNameErrorType;
AR_API_DATA(ArObjectType) ArAttributeErrorType;
AR_API_DATA(ArObjectType) ArStopIterationType;
AR_API_DATA(ArObjectType) ArIOErrorType;

#endif /* _ARANA_ERROR_H */