Browse Source

Exception strings include msg from type and custom msg

master
Pradana Aumars 1 year ago
parent
commit
f5230ae3d5
  1. 25
      src/exception_t.c
  2. 2
      src/exception_t.h

25
src/exception_t.c

@ -1,4 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include "exception_t.h"
@ -6,12 +9,13 @@ exception_t*
exception_init() {
exception_t *e = (exception_t*) malloc(sizeof(exception_t));
e->type = NO_ERROR;
e->msg = NULL;
e->msg = malloc(CHAR_MAX);
return e;
}
void
exception_free(exception_t *e) {
free(e->msg);
free(e);
}
@ -19,3 +23,22 @@ int
exception_null(const exception_t *e) {
return e->type == NO_ERROR;
}
char*
exception_prestr(const exception_t *e) {
switch(e->type) {
case NO_ERROR: return "No error\n";
case NO_FILE: return "No file found";
case XML_FILE_MISSING: return "XML file missing";
case XML_READ_FILE_FAIL: return "XML file reading failed";
case MD5SUM_GEN_FAIL: return "MD5sum generation failed";
case MD5SUM_VERIFY_FAIL: return "MD5sum verification failed";
}
}
char*
exception_str(const exception_t *e, char* msg) {
if (e->type != NO_ERROR) sprintf(msg, "%s: %s\n", exception_prestr(e), e->msg);
else sprintf(msg, "%s\n", exception_prestr(e));
return msg;
}

2
src/exception_t.h

@ -25,6 +25,6 @@ int
exception_null(const exception_t *e);
char*
exception_str(exception_t *e);
exception_str(const exception_t *e, char* msg);
#endif