Error handling(3)

NAME
Error handling - How to handle errors which might very well happen.

  Data Structures
struct brlapi_error_t

  Defines
#define BRLERR_SUCCESS 0
#define BRLERR_NOMEM 1
#define BRLERR_TTYBUSY 2
#define BRLERR_RAWMODEBUSY 3
#define BRLERR_UNKNOWN_INSTRUCTION 4
#define BRLERR_ILLEGAL_INSTRUCTION 5
#define BRLERR_INVALID_PARAMETER 6
#define BRLERR_INVALID_PACKET 7
#define BRLERR_CONNREFUSED 8
#define BRLERR_OPNOTSUPP 9
#define BRLERR_GAIERR 10
#define BRLERR_LIBCERR 11
#define BRLERR_UNKNOWNTTY 12
#define BRLERR_PROTOCOL_VERSION 13
#define BRLERR_EOF 14
#define BRLERR_EMPTYKEY 15
#define BRLERR_DRIVERERROR 16
#define brlapi_error (*brlapi_error_location())
#define brlapi_errno (brlapi_error.brlerrno)
#define brlapi_libcerrno (brlapi_error.exterrno.libcerrno)
#define brlapi_gaierrno (brlapi_error.exterrno.gaierrno)
#define brlapi_errfun (brlapi_error.errfun)

  Typedefs
typedef uint32_t brl_type_t
typedef void(* brlapi_exceptionHandler_t )(int err, brl_type_t type, const void *buf, size_t size)

  Functions
void brlapi_perror (const char *s)
brlapi_error_t * brlapi_error_location (void)
const char * brlapi_strerror (const brlapi_error_t *error)
const char * brlapi_packetType (brl_type_t ptype)
int brlapi_strexception (char *buf, size_t n, int err, brl_type_t type, const void *packet, size_t size)
brlapi_exceptionHandler_t brlapi_setExceptionHandler (brlapi_exceptionHandler_t handler)

  Variables
const char * brlapi_errlist []
const int brlapi_nerr
brlapi_error_t brlapi_error
int brlapi_errno
int brlapi_libcerrno
int brlapi_gaierrno
const char * brlapi_errfun

Detailed Description

When a function fails for some normal error, brlapi_errno will hold an error code to explain why it failed. It is good practice to print it somewhere for the user or the programmer to understand why it failed.

When something really wrong was done somewhere, like an out-of-bounds argument, which is really a bug in either the implementation of BrlAPI or in the application, an exception may be sent by the server. As soon as libbrlapi receives it, an exception handler prints it and abort()s he program, since it means something went really wrong and that we shouldn't go further.

This is hopefully a really seldom event, but in the case of an editor, for instance, one would surely want to have files saved before actually abort()ing. brlapi_setExceptionHandler() lets the programmer define another exception handler which would save files and then call the default handler brlapi_defaultExceptionHandler().

Define Documentation

  #define brlapi_errfun (brlapi_error.errfun)

Cheat about the brlapi_errfun C token

  #define brlapi_errno (brlapi_error.brlerrno)

Cheat about the brlapi_errno C token

  #define brlapi_error (*brlapi_error_location())

Cheat about the brlapi_error C token

  #define brlapi_gaierrno (brlapi_error.exterrno.gaierrno)

Cheat about the brlapi_gaierrno C token

  #define brlapi_libcerrno (brlapi_error.exterrno.libcerrno)

Cheat about the brlapi_libcerrno C token

  #define BRLERR_CONNREFUSED 8

Connection refused

  #define BRLERR_DRIVERERROR 16

Packet returned by driver too large

  #define BRLERR_EMPTYKEY 15

Key file empty

  #define BRLERR_EOF 14

Unexpected end of file

  #define BRLERR_GAIERR 10

Getaddrinfo error

  #define BRLERR_ILLEGAL_INSTRUCTION 5

Forbiden in current mode

  #define BRLERR_INVALID_PACKET 7

Invalid size

  #define BRLERR_INVALID_PARAMETER 6

Out of range or have no sense

  #define BRLERR_LIBCERR 11

Libc error

  #define BRLERR_NOMEM 1

Not enough memory

  #define BRLERR_OPNOTSUPP 9

Operation not supported

  #define BRLERR_PROTOCOL_VERSION 13

Bad protocol version

  #define BRLERR_RAWMODEBUSY 3

A connection is already using RAW mode

  #define BRLERR_SUCCESS 0

Success

  #define BRLERR_TTYBUSY 2

A connection is already running in this tty

  #define BRLERR_UNKNOWN_INSTRUCTION 4

Not implemented in protocol

  #define BRLERR_UNKNOWNTTY 12

Couldn't find out the tty number

Typedef Documentation

  typedef uint32_t brl_type_t

Type for packet type. Only unsigned can cross networks, 32bits

  typedef void(* brlapi_exceptionHandler_t)(int err, brl_type_t type, const void *buf, size_t size)

Type for error handlers

Type of error handlers which are to be given to brlapi_setExceptionHandler().

Parameters:
    err is a BRLERR_ error code;
    type is the type of the guilty packet;
    buf points to the content of the guilty packet (might be a little bit truncated);
    size gives the guilty packet's size.

Function Documentation

  brlapi_error_t* brlapi_error_location (void)

Get per-thread error location

In multithreaded software, brlapi_error is thread-specific, so api.h cheats about the brlapi_error token and actually calls brlapi_error_location().

This gets the thread specific location of global variable brlapi_error

  const char* brlapi_packetType (brl_type_t ptype)

Get plain packet type

brlapi_packetType() returns the plain packet type name corresponding to its argument.

  void brlapi_perror (const char * s)

Print a BrlAPI error message

brlapi_perror() reads brlapi_error, and acts just like perror().

  brlapi_exceptionHandler_t brlapi_setExceptionHandler (brlapi_exceptionHandler_t handler)

Set a new exception handler

brlapi_setExceptionHandler() replaces the previous exception handler with the handler parameter. The previous exception handler is returned to make chaining error handlers possible.

The default handler just prints the exception and exit()s.

  const char* brlapi_strerror (const brlapi_error_t * error)

Get plain error message

brlapi_strerror() returns the plain error message corresponding to its argument.

  int brlapi_strexception (char * buf, size_t n, int err, brl_type_t type, const void * packet, size_t size)

Describes an exception

brlapi_strexception() puts a text describing the given exception in buf.

The beginning of the guilty packet is dumped as a sequence of hex bytes.

Returns:
    the size of the text describing the exception, following snprintf()'s semantics.

Variable Documentation

  const char* brlapi_errfun

Shorthand for brlapi_error.errfun

  const char* brlapi_errlist[]

Error message list

These are the string constants used by brlapi_perror().

  int brlapi_errno

Shorthand for brlapi_error.errno

  brlapi_error_t brlapi_error

Global variable brlapi_error

brlapi_error is a global left-value containing the last error information. Its errno field is not reset to BRLERR_SUCCESS on success.

This information may be copied in brlapi_error_t variables for later use with the brlapi_strerror function.

  int brlapi_gaierrno

Shorthand for brlapi_error.exterrno.gaierrno

  int brlapi_libcerrno

Shorthand for brlapi_error.exterrno.libcerrno

  const int brlapi_nerr

Number of error messages