Exception strings include msg from type and custom msg

This commit is contained in:
Pradana Aumars 2021-06-12 11:04:02 +02:00
parent fce41edbe5
commit f5230ae3d5
2 changed files with 25 additions and 2 deletions

View File

@ -1,4 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include "exception_t.h" #include "exception_t.h"
@ -6,12 +9,13 @@ exception_t*
exception_init() { exception_init() {
exception_t *e = (exception_t*) malloc(sizeof(exception_t)); exception_t *e = (exception_t*) malloc(sizeof(exception_t));
e->type = NO_ERROR; e->type = NO_ERROR;
e->msg = NULL; e->msg = malloc(CHAR_MAX);
return e; return e;
} }
void void
exception_free(exception_t *e) { exception_free(exception_t *e) {
free(e->msg);
free(e); free(e);
} }
@ -19,3 +23,22 @@ int
exception_null(const exception_t *e) { exception_null(const exception_t *e) {
return e->type == NO_ERROR; 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;
}

View File

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