Added internal errors and refined the error handling somewhat.

This commit is contained in:
Pietro Gagliardi 2019-05-13 21:30:18 -04:00
parent 45f69cf058
commit 7c128e7bcd
3 changed files with 43 additions and 23 deletions

View File

@ -4,6 +4,26 @@
#include "ui.h" #include "ui.h"
#include "uipriv.h" #include "uipriv.h"
#define internalErrorPrefix "libui internal error"
// TODO add debugging advice?
#define internalErrorSuffix "This likely means there is a bug in libui itself. Contact the libui authors."
void uiprivInternalError(const char *fmt, ...)
{
va_list ap;
char buf[256];
int n;
va_start(ap, fmt);
n = vsnprintf(buf, 256, fmt, ap);
va_end(ap);
if (n < 0)
uiprivReportError(internalErrorPrefix, "internal error string has encoding error", internalErrorSuffix, true);
if (n >= 256)
uiprivReportError(internalErrorPrefix, "internal error string too long", internalErrorSuffix, true);
uiprivReportError(internalErrorPrefix, buf, internalErrorSuffix, true);
}
static const char *messages[] = { static const char *messages[] = {
[uiprivProgrammerErrorWrongStructSize] = "wrong size %zu for %s", [uiprivProgrammerErrorWrongStructSize] = "wrong size %zu for %s",
[uiprivProgrammerErrorIndexOutOfRange] = "index %d out of range in %s()", [uiprivProgrammerErrorIndexOutOfRange] = "index %d out of range in %s()",
@ -15,22 +35,19 @@ static void prepareProgrammerError(char *buf, int size, unsigned int which, va_l
{ {
int n; int n;
if (which >= uiprivNumProgrammerErrors) { if (which >= uiprivNumProgrammerErrors)
// TODO uiprivInternalError("bad programmer error value %u", which);
}
n = vsnprintf(buf, size, messages[which], ap); n = vsnprintf(buf, size, messages[which], ap);
if (n < 0) { if (n < 0)
// TODO uiprivInternalError("programmer error string for %u has encoding error", which);
} if (n >= size)
if (n >= size) { uiprivInternalError("programmer error string for %u too long (%d)", which, n);
// TODO
buf[size - 4] = '.';
buf[size - 3] = '.';
buf[size - 2] = '.';
buf[size - 1] = '\0';
}
} }
#define programmerErrorPrefix "libui programmer error"
// TODO add debugging advice?
#define programmerErrorSuffix "This likely means you are using libui incorrectly. Check your source code and try again. If you have received this warning in error, contact the libui authors."
void uiprivProgrammerError(unsigned int which, ...) void uiprivProgrammerError(unsigned int which, ...)
{ {
va_list ap; va_list ap;
@ -39,5 +56,5 @@ void uiprivProgrammerError(unsigned int which, ...)
va_start(ap, which); va_start(ap, which);
prepareProgrammerError(buf, 256, which, ap); prepareProgrammerError(buf, 256, which, ap);
va_end(ap); va_end(ap);
uiprivSysProgrammerError(buf); uiprivReportError(programmerErrorPrefix, buf, programmerErrorSuffix, false);
} }

View File

@ -11,6 +11,7 @@ extern int uiprivInitReturnError(uiInitError *err, const char *msg);
extern int uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...); extern int uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...);
// errors.c // errors.c
extern void uiprivInternalError(const char *fmt, ...);
enum { enum {
uiprivProgrammerErrorWrongStructSize, // arguments: size_t badSize, const char *structName uiprivProgrammerErrorWrongStructSize, // arguments: size_t badSize, const char *structName
uiprivProgrammerErrorIndexOutOfRange, // arguments: int badIndex, __func__ uiprivProgrammerErrorIndexOutOfRange, // arguments: int badIndex, __func__
@ -21,10 +22,7 @@ enum {
uiprivNumProgrammerErrors, uiprivNumProgrammerErrors,
}; };
extern void uiprivProgrammerError(unsigned int which, ...); extern void uiprivProgrammerError(unsigned int which, ...);
extern void uiprivSysProgrammerError(const char *msg); extern void uiprivReportError(const char *prefix, const char *msg, const char *suffix, bool internal);
#define uiprivProgrammerErrorPrefix "libui programmer error"
// TODO add debugging advice?
#define uiprivProgrammerErrorAdvice "This likely means you are using libui incorrectly. Check your source code and try again. If you have received this warning in error, contact the libui authors."
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -115,12 +115,17 @@ void uiQueueMain(void (*f)(void *data), void *data)
// - possibly others, all on stackoverflow.com (and maybe once on Apple's own forums?); I forget now // - possibly others, all on stackoverflow.com (and maybe once on Apple's own forums?); I forget now
static void debugBreak(void); static void debugBreak(void);
void uiprivSysProgrammerError(const char *msg) void uiprivReportError(const char *prefix, const char *msg, const char *suffix, bool internal)
{ {
NSLog(@"*** %s: %s. %s", uiprivProgrammerErrorPrefix, msg, uiprivProgrammerErrorAdvice); NSExceptionName exceptionName;
// TODO either find an appropriate exception for each message or use a custom exception name
[NSException raise:NSInvalidArgumentException NSLog(@"*** %s: %s. %s", prefix, msg, suffix);
format:@"%s: %s", uiprivProgrammerErrorPrefix, msg]; exceptionName = NSInternalInconsistencyException;
if (!internal)
// TODO either find an appropriate exception for each possible message or use a custom exception name
exceptionName = NSInvalidArgumentException;
[NSException raise:exceptionName
format:@"%s: %s", prefix, msg];
debugBreak(); debugBreak();
abort(); // we shouldn't reach here abort(); // we shouldn't reach here
} }