Error handling(3)

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

  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_RAWNOTSUPP 8
#define BRLERR_KEYSNOTSUPP 9
#define BRLERR_CONNREFUSED 10
#define BRLERR_OPNOTSUPP 11
#define BRLERR_GAIERR 12
#define BRLERR_LIBCERR 13
#define BRLERR_UNKNOWNTTY 14
#define BRLERR_PROTOCOL_VERSION 15
#define BRLERR_EOF 16
#define BRLERR_TOORECURSE 17
#define BRLERR_DRIVERERROR 18
#define brlapi_errno (*brlapi_errno_location ())

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

  Functions
void brlapi_perror (const char *s)
int * brlapi_errno_location (void)
const char * brlapi_strerror (int err)
const char * brlapi_packetType (brl_type_t ptype)
brlapi_errorHandler_t brlapi_setErrorHandler (brlapi_errorHandler_t handler)

  Variables
const char * brlapi_errlist []
const int brlapi_nerr
int brlapi_errno

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 error handler prints it and exit()s the program, since it means something went really wrong and that we shouldn't go further.

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

Define Documentation

  #define brlapi_errno (*brlapi_errno_location ())

cheat about the brlapi_errno C token

  #define BRLERR_CONNREFUSED 10

Connection refused

  #define BRLERR_DRIVERERROR 18

Packet returned by driver too large

  #define BRLERR_EOF 16

Unexpected end of file

  #define BRLERR_GAIERR 12

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_KEYSNOTSUPP 9

Reading of key codes not supported by loaded driver

  #define BRLERR_LIBCERR 13

Libc error

  #define BRLERR_NOMEM 1

Not enough memory

  #define BRLERR_OPNOTSUPP 11

Operation not supported

  #define BRLERR_PROTOCOL_VERSION 15

Bad protocol version

  #define BRLERR_RAWMODEBUSY 3

Already a connection using RAW mode

  #define BRLERR_RAWNOTSUPP 8

Raw mode not supported by loaded driver

  #define BRLERR_SUCCESS 0

Success

  #define BRLERR_TOORECURSE 17

Too many levels of recursion

  #define BRLERR_TTYBUSY 2

Already a connection running in this tty

  #define BRLERR_UNKNOWN_INSTRUCTION 4

Not implemented in protocol

  #define BRLERR_UNKNOWNTTY 14

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_errorHandler_t)(int err, brl_type_t type, const void *buf, size_t size)

Type for error handlers

Type to give to error handlers which are to be given to brlapi_setErrorHandler()

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

Function Documentation

  int* brlapi_errno_location (void)

Get per-thread errno location

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

This gets the thread specific location of global variable brlapi_errno

  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_errno, and acts just like perror()

  brlapi_errorHandler_t brlapi_setErrorHandler (brlapi_errorHandler_t handler)

Set a new exception handler

brlapi_setErrorHandler() replaces the previous error handler by the handler parameter. The previous error handler is returned so that chaining error handlers might be possible.

The default handler just prints the error and exit()s

  const char* brlapi_strerror (int err)

Get plain error message

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

Variable Documentation

  const char* brlapi_errlist[]

Error message list

These are the string constants used by brlapi_perror()

  int brlapi_errno

Global variable brlapi_errno

brlapi_errno is a global left-value containing the last error code. It is not reset to BRLERR_SUCCESS on success.

  const int brlapi_nerr

Number of error messages