Plan 9 from Bell Labs’s /usr/web/sources/contrib/pac/sys/doc/netpbm/ms/error.ms

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.TL
Netpbm User Manual
.SH 1
Error Handling
.LP
.SH 2
Netpbm Programming Library Errors
.LP
.LP
As part of Netpbm's mission to make writing graphics programs quick
and easy, Netpbm recognizes that no programmer likes to deal with
error conditions.  Therefore, very few Netpbm programming library
functions return error information.  There are no return codes to
check.  If for some reason a function can't do what was asked of it,
it doesn't return at all.
.LP
Netpbm's response to encountering an error is called "throwing
an error."
.LP
The typical way a Netpbm function throws an error (for example,
when you attempt to open a non-existent file with \fBpm_openr()\fR)
is that the function writes an error message to the Standard Error
file and then causes the program to terminate with an exit() system
call.  The function doesn't do any explicit cleanup, because everything
a library function sets up gets cleaned up by normal process
termination.
.LP
In many cases, that simply isn't acceptable.  If you're calling
Netpbm functions from inside a server program, you'd want the program
to recognize that the immediate task failed, but keep running to do
other work.
.LP
So as an alternative, you can replace that program exit with a
longjmp instead.  A longjmp is a classic Unix exception handling
concept.  See the documentation of the standard C library
\fBsetjmp()\fR and \fBlongjmp()\fR functions.
.LP
In short, you identify a point in your programs for execution to
hyperjump to from whatever depths of whatever functions it may be in
at the time it detects an exception.  That hyperjump is called a
longjmp.  The longjmp unwinds the stack and puts the program in the
same state as if the subroutines had returned all the way up to the
function that contains the jump point.  A longjmp does not in itself
undo things like memory allocations.  But when you have a Netpbm
function do a longjmp, it also cleans up everything it started.
.LP
To select this form of throwing an error, use the
\fBpm_setjmpbuf()\fR function.  This alternative is not available
before Netpbm 10.27 (March 2005).
.LP
Issuing of the error message is a separate thing.  Regardless
of whether a library routine exits the program or executes a longjmp,
it issues an error message first.
.LP
You can customize the error message behavior too.  By default, a
Netpbm function issues an error message by writing it to the Standard
Error file, formatted into a single line with the program name prefixed.
But you can register your own error message function to run instead with
\fBpm_setErrorMsgFn()\fR.
.SH 3
pm_setjmpbuf()
.LP
.LP
pm_setjmpbuf() sets up the process so that when future calls to the
Netpbm programming library throw an error, they execute a longjmp
instead of causing the process to exit as they would by default.
.LP
This is not analogous to \fBsetjmp()\fR.  You do a
setjmp() first, then tell the Netpbm programming library with
\fBpm_setjmpbuf()\fR to use the result.
.LP
Example:
.DS L
  #include <setjmp.h>
  #include <pam.h>
  jmp_buf jmpbuf;
  int rc;
  rc = setjmp(jmpbuf);
  if (rc == 0) {
    struct pam pam;
    pm_setjmpbuf(&jmpbuf);
    
    pnm_readpam(stdin, &pam, PAM_STRUCT_SIZE(tuple_type));
    printf("pnm_readpam() succeeded!\n");
  } else {
    printf("pnm_readpam() failed.  You should have seen "
           "messages to Standard Error telling you why.\n");
  }
.DE
.LP
This example should look really strange to you if you haven't read
the documentation of \fBsetjmp()\fR.  Remember that there is a
hyperjump such that the program is executing the \fBpnm_readpam()\fR
and then suddenly is returning a second time from the setjmp()!
.LP
Even \fBpm_error()\fR works this way -- if you set up a longjmp with
\fBpm_setjmpbuf()\fR and then call \fBpm_error()\fR, \fBpm_error()\fR
will, after issuing your error message, execute the longjmp.
.LP
\fBpm_setjmpbuf()\fR was new in Netpbm 10.27 (March 2005).  Before
that, Netpbm programming library functions always throw an error by
exiting the program.
.SH 2
User Detected Errors
.LP
.LP
The Netpbm programming library provides a function for you to throw
an error explicitly: \fBpm_error()\fR.  \fBpm_error()\fR does
nothing but throw an error, and does so the same way any Netpbm
library function you call would.  \fBpm_error()\fR is more convenient
than most standard C facilities for handling errors.
.LP
If you don't want to throw an error, but just want to issue an
error message, use \fBpm_errormsg()\fR.  It issues the message in the
same way as \fBpm_error()\fR but returns normally instead of longjmping
or exiting the program.  
.LP
Note that \fBlibnetpbm\fR distinguishes between an error message
and an informational message (use \fBpm_errormsg()\fR for the former;
\fBpm_message()\fR for the latter).  The only practical difference is
which user message function it calls.  So if you don't register any
user message function, you won't see any difference, but a program is
still more maintainable and easier to read when you use the
appropriate one of these.
.SH 3
pm_error()
.LP
.SH 4
Overview
.LP
.LP
\fBvoid pm_error(\fR
\fBchar *\fR \fIfmt\fR\fB,\fR
\fB... );\fR
.SH 4
Example
.LP
.DS L
if (argc-1 < 3)
    pm_error("You must specify at least 3 arguments.  "
             "You specified" only %d", argc-1);
.DE
.LP
\fBpm_error()\fR is a \fBprintf()\fR style routine that 
simply throws an error.  It issues an error message exactly like
\fBpm_errormsg()\fR would in the process.
.SH 3
pm_errormsg()
.LP
.SH 4
Overview
.LP
.LP
\fBvoid pm_errormsg(\fR
\fBchar *\fR \fIfmt\fR\fB,\fR
\fB... );\fR
.SH 4
Example
.LP
.DS L
if (rc = -1)
    pm_errormsg("Could not open file.  errno=%d", errno);
    return -1;
.DE
.LP
\fBpm_errormsg()\fR is a \fBprintf()\fR style routine that 
issues an error message.  By default, it writes the message to Standard
Error, but you can register a user error message routine to be called
instead, and that might do something such as write the message into a 
log file.  See \fBpm_setusererrormsgfn\fR.
.LP
There is very little advantage to using this over traditional C
services, but it issues a message in the same way as \fBlibnetpbm\fR
library functions do, so the common handling might be valuable.
.LP
Note that the arguments specify the message text, not any formatting
of it.  Formatting is handled by \fBpm_errormsg()\fR.  So don't put any
newlines or tabs in it.
.SH 3
pm_setusererrormsgfn()
.LP
.SH 4
Overview
.LP
.LP
\fBvoid pm_setusererrormsgfn(pm_usererrormsgfn *\fR \fIfunction\fR\fB);\fR
.SH 4
Example
.LP
.DS L
    static pm_usererrormsgfn logfilewrite;
    static void
    logfilewrite(const char * const msg) {
        fprintf(myerrorlog, "Netpbm error: %s", msg);
    }
    pm_setusererrormsgfn(&logfilewrite);
    
    pm_errormsg("Message for the error log");
.DE
.LP
\fBpm_setusererrormsg()\fR registers a handler for error messages,
called a user error message routine.  Any library function that wants
to issue an error message in the future will call that function with
the message as an argument.
.LP
The argument the user error message routine gets is English text
designed for human reading.  It is just the text of the message; there
is no attempt at formatting in it (so you won't see any newline or tab
characters).
.LP
You can remove the user error message routine, so that the library
issues future error messages in its default way (write to Standard Error)
by specifying a null pointer for \fIfunction\fR.
.LP
The user error message routine does not handle informational messages.
It handles only error messages.  See 
\fBpm_setusermessagefn()\fR.
.SH 2
Error Handling In Netpbm Programs
.LP
.LP
Most Netpbm programs respond to encountering an error by issuing a
message describing the error to the Standard Error file and then
exiting with exit status 1.
.LP
Netpbm programs generally do not follow the Unix convention of very
terse error messages.  Conventional Unix programs produce error
messages as if they had to pay by the word.  Netpbm programs tend to
give a complete description of the problem in human-parseable English.
These messages are often many terminal lines long.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.