00001 /* errCatch - help catch errors so that errAborts aren't 00002 * fatal, and warn's don't necessarily get printed immediately. 00003 * Note that error conditions caught this way will tend to 00004 * leak resources unless there are additional wrappers. 00005 * 00006 * Typical usage is 00007 * errCatch = errCatchNew(); 00008 * if (errCatchStart(errCatch)) 00009 * doFlakyStuff(); 00010 * errCatchEnd(errCatch); 00011 * if (errCatch->gotError) 00012 * warn(errCatch->message->string); 00013 * errCatchFree(&errCatch); 00014 * cleanupFlakyStuff(); 00015 */ 00016 #ifndef ERRCATCH_H 00017 #define ERRCATCH_H 00018 00019 #ifndef DYSTRING_H 00020 #include "dystring.h" 00021 #endif 00022 00023 struct errCatch 00024 /* Something to help catch errors. */ 00025 { 00026 struct errCatch *next; /* Next in stack. */ 00027 jmp_buf jmpBuf; /* Where to jump back to for recovery. */ 00028 struct dyString *message; /* Error message if any */ 00029 boolean gotError; /* Some sort of error was caught. */ 00030 }; 00031 00032 struct errCatch *errCatchNew(); 00033 /* Return new error catching structure. */ 00034 00035 void errCatchFree(struct errCatch **pErrCatch); 00036 /* Free up resources associated with errCatch */ 00037 00038 #define errCatchStart(e) (errCatchPushHandlers(e) && setjmp(e->jmpBuf) == 0) 00039 /* Little wrapper around setjmp. This returns TRUE 00040 * on the main execution thread, FALSE after abort. */ 00041 00042 00043 boolean errCatchPushHandlers(struct errCatch *errCatch); 00044 /* Push error handlers. Not usually called directly. 00045 * but rather through errCatchStart() macro. Always 00046 * returns TRUE. */ 00047 00048 void errCatchEnd(struct errCatch *errCatch); 00049 /* Restore error handlers and pop self off of catching stack. */ 00050 00051 boolean errCatchFinish(struct errCatch **pErrCatch); 00052 /* Finish up error catching. Report error if there is a 00053 * problem and return FALSE. If no problem return TRUE. 00054 * This handles errCatchEnd and errCatchFree. */ 00055 #endif /* ERRCATCH_H */ 00056
1.5.2