1 /*-------------------------------------------------------------------------
4 * POSTGRES exception handling code.
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/error/Attic/exc.c,v 1.36 2001/01/24 19:43:15 momjian Exp $
14 * XXX this code needs improvement--check for state violations and
15 * XXX reset after handling an exception.
16 * XXX Probably should be merged with elog.c.
18 *-------------------------------------------------------------------------
24 #include "storage/ipc.h"
25 #include "utils/exc.h"
30 static void ExcUnCaught(Exception *excP, ExcDetail detail, ExcData data,
32 static void ExcPrint(Exception *excP, ExcDetail detail, ExcData data,
38 static bool ExceptionHandlingEnabled = false;
40 char *ExcFileName = NULL;
41 Index ExcLineNumber = 0;
43 ExcFrame *ExcCurFrameP = NULL;
45 static ExcProc *ExcUnCaughtP = NULL;
52 * EnableExceptionHandling
53 * Enables/disables the exception handling system.
56 * This must be called before any exceptions occur. I.e., call this first!
57 * This routine will not return if an error is detected.
58 * This does not follow the usual Enable... protocol.
59 * This should be merged more closely with the error logging and tracing
66 * Excection handling should be supported by the language, thus there should
67 * be no need to explicitly enable exception processing.
69 * This function should probably not be called, ever. Currently it does
70 * almost nothing. If there is a need for this intialization and checking.
71 * then this function should be converted to the new-style Enable code and
72 * called by all the other module Enable functions.
75 EnableExceptionHandling(bool on)
77 if (on == ExceptionHandlingEnabled)
79 /* XXX add logging of failed state */
81 /* ExitPostgres(FatalExitStatus); */
96 ExceptionHandlingEnabled = on;
100 ExcPrint(Exception *excP,
105 /* this buffer is only used if errno has a bogus value: */
106 char errorstr_buf[32];
107 const char *errorstr;
113 /* Save error str before calling any function that might change errno */
114 errorstr = strerror(errno);
116 * Some strerror()s return an empty string for out-of-range errno.
117 * This is ANSI C spec compliant, but not exactly useful.
119 if (errorstr == NULL || *errorstr == '\0')
121 sprintf(errorstr_buf, "error %d", errno);
122 errorstr = errorstr_buf;
125 fflush(stdout); /* In case stderr is buffered */
128 fprintf(stderr, "%s", message);
129 else if (excP->message != NULL)
130 fprintf(stderr, "%s", excP->message);
132 fprintf(stderr, "UNNAMED EXCEPTION %p", excP);
134 fprintf(stderr, " (%ld) [%s]\n", detail, errorstr);
150 ExcSetUnCaught(ExcProc *newP)
152 ExcProc *oldP = ExcUnCaughtP;
162 ExcUnCaught(Exception *excP,
167 ExcPrint(excP, detail, data, message);
169 ExcAbort(excP, detail, data, message);
173 ExcRaise(Exception *excP,
183 if (ExcUnCaughtP != NULL)
184 (*ExcUnCaughtP) (excP, detail, data, message);
186 ExcUnCaught(excP, detail, data, message);
191 efp->detail = detail;
193 efp->message = message;
195 ExcCurFrameP = efp->link;
197 siglongjmp(efp->context, 1);