From e1db398049adf8ea37cebfc8f816be12f9ba131c Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 9 Sep 2016 01:13:23 +0200 Subject: *: centralize error handling --- Makefile.am | 2 -- src/blogc/datetime-parser.c | 1 - src/blogc/datetime-parser.h | 1 - src/blogc/errors.c | 40 ------------------------------------- src/blogc/errors.h | 24 ---------------------- src/blogc/file.c | 1 - src/blogc/file.h | 1 - src/blogc/loader.c | 1 - src/blogc/loader.h | 1 - src/blogc/main.c | 5 ++--- src/blogc/renderer.c | 3 +-- src/blogc/source-parser.c | 1 - src/blogc/source-parser.h | 1 - src/blogc/template-parser.c | 1 - src/blogc/template-parser.h | 1 - src/common/error.c | 29 +++++++++++++++++++++++++++ src/common/error.h | 13 +++++++++++- tests/blogc/check_datetime_parser.c | 1 - tests/blogc/check_loader.c | 1 - tests/blogc/check_renderer.c | 1 - tests/blogc/check_source_parser.c | 1 - tests/blogc/check_template_parser.c | 1 - 22 files changed, 44 insertions(+), 87 deletions(-) delete mode 100644 src/blogc/errors.c delete mode 100644 src/blogc/errors.h diff --git a/Makefile.am b/Makefile.am index 8012ce5..bf07f5c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -35,7 +35,6 @@ noinst_HEADERS = \ src/blogc/content-parser.h \ src/blogc/datetime-parser.h \ src/blogc/debug.h \ - src/blogc/errors.h \ src/blogc/file.h \ src/blogc/loader.h \ src/blogc/renderer.h \ @@ -77,7 +76,6 @@ check_PROGRAMS = \ libblogc_la_SOURCES = \ src/blogc/content-parser.c \ src/blogc/datetime-parser.c \ - src/blogc/errors.c \ src/blogc/file.c \ src/blogc/loader.c \ src/blogc/renderer.c \ diff --git a/src/blogc/datetime-parser.c b/src/blogc/datetime-parser.c index 2fd3b4f..d7481e5 100644 --- a/src/blogc/datetime-parser.c +++ b/src/blogc/datetime-parser.c @@ -17,7 +17,6 @@ #include #include "datetime-parser.h" -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/blogc/datetime-parser.h b/src/blogc/datetime-parser.h index 08ad3c9..fc06b62 100644 --- a/src/blogc/datetime-parser.h +++ b/src/blogc/datetime-parser.h @@ -9,7 +9,6 @@ #ifndef _DATETIME_H #define _DATETIME_H -#include "errors.h" #include "../common/error.h" char* blogc_convert_datetime(const char *orig, const char *format, diff --git a/src/blogc/errors.c b/src/blogc/errors.c deleted file mode 100644 index 6447a4a..0000000 --- a/src/blogc/errors.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2015-2016 Rafael G. Martins - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#include -#include -#include "errors.h" -#include "../common/error.h" - - -void -blogc_error_print(bc_error_t *err) -{ - if (err == NULL) - return; - - switch((blogc_error_type_t) err->type) { - case BLOGC_ERROR_SOURCE_PARSER: - fprintf(stderr, "blogc: error: source: %s\n", err->msg); - break; - case BLOGC_ERROR_TEMPLATE_PARSER: - fprintf(stderr, "blogc: error: template: %s\n", err->msg); - break; - case BLOGC_ERROR_LOADER: - fprintf(stderr, "blogc: error: loader: %s\n", err->msg); - break; - case BLOGC_ERROR_FILE: - fprintf(stderr, "blogc: error: file: %s\n", err->msg); - break; - case BLOGC_WARNING_DATETIME_PARSER: - fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); - break; - default: - fprintf(stderr, "blogc: error: %s\n", err->msg); - } -} diff --git a/src/blogc/errors.h b/src/blogc/errors.h deleted file mode 100644 index 427d9e7..0000000 --- a/src/blogc/errors.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2015-2016 Rafael G. Martins - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifndef _ERRORS_H -#define _ERRORS_H - -#include "../common/error.h" - -typedef enum { - BLOGC_ERROR_SOURCE_PARSER = 1, - BLOGC_ERROR_TEMPLATE_PARSER, - BLOGC_ERROR_LOADER, - BLOGC_ERROR_FILE, - BLOGC_WARNING_DATETIME_PARSER, -} blogc_error_type_t; - -void blogc_error_print(bc_error_t *err); - -#endif /* _ERRORS_H */ diff --git a/src/blogc/file.c b/src/blogc/file.c index 94148fe..cede962 100644 --- a/src/blogc/file.c +++ b/src/blogc/file.c @@ -13,7 +13,6 @@ #include #include #include "file.h" -#include "errors.h" #include "../common/error.h" #include "../common/utf8.h" #include "../common/utils.h" diff --git a/src/blogc/file.h b/src/blogc/file.h index c509874..8213573 100644 --- a/src/blogc/file.h +++ b/src/blogc/file.h @@ -11,7 +11,6 @@ #include #include -#include "errors.h" #include "../common/error.h" #define BLOGC_FILE_CHUNK_SIZE 1024 diff --git a/src/blogc/loader.c b/src/blogc/loader.c index 93d9eef..e8de75d 100644 --- a/src/blogc/loader.c +++ b/src/blogc/loader.c @@ -16,7 +16,6 @@ #include "source-parser.h" #include "template-parser.h" #include "loader.h" -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/blogc/loader.h b/src/blogc/loader.h index c8a7230..2d3ef89 100644 --- a/src/blogc/loader.h +++ b/src/blogc/loader.h @@ -9,7 +9,6 @@ #ifndef _LOADER_H #define _LOADER_H -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/blogc/main.c b/src/blogc/main.c index 8563c68..beb67d6 100644 --- a/src/blogc/main.c +++ b/src/blogc/main.c @@ -26,7 +26,6 @@ #include "template-parser.h" #include "loader.h" #include "renderer.h" -#include "errors.h" #include "../common/error.h" #include "../common/utf8.h" #include "../common/utils.h" @@ -224,7 +223,7 @@ main(int argc, char **argv) bc_slist_t *s = blogc_source_parse_from_files(config, sources, &err); if (err != NULL) { - blogc_error_print(err); + bc_error_print(err); rv = 2; goto cleanup2; } @@ -251,7 +250,7 @@ main(int argc, char **argv) bc_slist_t* l = blogc_template_parse_from_file(template, &err); if (err != NULL) { - blogc_error_print(err); + bc_error_print(err); rv = 2; goto cleanup3; } diff --git a/src/blogc/renderer.c b/src/blogc/renderer.c index 7c29818..e0f1da6 100644 --- a/src/blogc/renderer.c +++ b/src/blogc/renderer.c @@ -13,7 +13,6 @@ #include #include #include "datetime-parser.h" -#include "errors.h" #include "template-parser.h" #include "renderer.h" #include "../common/error.h" @@ -47,7 +46,7 @@ blogc_format_date(const char *date, bc_trie_t *global, bc_trie_t *local) bc_error_t *err = NULL; char *rv = blogc_convert_datetime(date, date_format, &err); if (err != NULL) { - blogc_error_print(err); + bc_error_print(err); bc_error_free(err); return bc_strdup(date); } diff --git a/src/blogc/source-parser.c b/src/blogc/source-parser.c index 7cb929e..a3d1e09 100644 --- a/src/blogc/source-parser.c +++ b/src/blogc/source-parser.c @@ -11,7 +11,6 @@ #include "content-parser.h" #include "source-parser.h" -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/blogc/source-parser.h b/src/blogc/source-parser.h index d920775..4ecbe28 100644 --- a/src/blogc/source-parser.h +++ b/src/blogc/source-parser.h @@ -10,7 +10,6 @@ #define _SOURCE_PARSER_H #include -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/blogc/template-parser.c b/src/blogc/template-parser.c index acf8f7c..7994a9b 100644 --- a/src/blogc/template-parser.c +++ b/src/blogc/template-parser.c @@ -11,7 +11,6 @@ #include #include "template-parser.h" -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/blogc/template-parser.h b/src/blogc/template-parser.h index 3ed3b07..e17a9a9 100644 --- a/src/blogc/template-parser.h +++ b/src/blogc/template-parser.h @@ -10,7 +10,6 @@ #define _TEMPLATE_PARSER_H #include -#include "errors.h" #include "../common/error.h" #include "../common/utils.h" diff --git a/src/common/error.c b/src/common/error.c index dbc1d81..78cdc86 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -101,6 +101,35 @@ bc_error_parser(int type, const char *src, size_t src_len, } +// error handling is centralized here for the sake of simplicity :/ +void +bc_error_print(bc_error_t *err) +{ + if (err == NULL) + return; + + switch(err->type) { + case BLOGC_ERROR_SOURCE_PARSER: + fprintf(stderr, "blogc: error: source: %s\n", err->msg); + break; + case BLOGC_ERROR_TEMPLATE_PARSER: + fprintf(stderr, "blogc: error: template: %s\n", err->msg); + break; + case BLOGC_ERROR_LOADER: + fprintf(stderr, "blogc: error: loader: %s\n", err->msg); + break; + case BLOGC_ERROR_FILE: + fprintf(stderr, "blogc: error: file: %s\n", err->msg); + break; + case BLOGC_WARNING_DATETIME_PARSER: + fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); + break; + default: + fprintf(stderr, "blogc: error: %s\n", err->msg); + } +} + + void bc_error_free(bc_error_t *err) { diff --git a/src/common/error.h b/src/common/error.h index 17843ed..f2845df 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -11,9 +11,20 @@ #include +// error handling is centralized here for the sake of simplicity :/ +typedef enum { + + // errors for src/blogc + BLOGC_ERROR_SOURCE_PARSER = 100, + BLOGC_ERROR_TEMPLATE_PARSER, + BLOGC_ERROR_LOADER, + BLOGC_ERROR_FILE, + BLOGC_WARNING_DATETIME_PARSER, +} bc_error_type_t; + typedef struct { char *msg; - int type; + bc_error_type_t type; } bc_error_t; bc_error_t* bc_error_new(int type, const char *msg); diff --git a/tests/blogc/check_datetime_parser.c b/tests/blogc/check_datetime_parser.c index e8791d1..4023018 100644 --- a/tests/blogc/check_datetime_parser.c +++ b/tests/blogc/check_datetime_parser.c @@ -14,7 +14,6 @@ #include #include "../../src/common/error.h" #include "../../src/blogc/datetime-parser.h" -#include "../../src/blogc/errors.h" static void diff --git a/tests/blogc/check_loader.c b/tests/blogc/check_loader.c index 8f5ccd2..0ec0a09 100644 --- a/tests/blogc/check_loader.c +++ b/tests/blogc/check_loader.c @@ -15,7 +15,6 @@ #include #include "../../src/common/error.h" #include "../../src/common/utils.h" -#include "../../src/blogc/errors.h" #include "../../src/blogc/template-parser.h" #include "../../src/blogc/loader.h" diff --git a/tests/blogc/check_renderer.c b/tests/blogc/check_renderer.c index 1ed5dea..3b85b45 100644 --- a/tests/blogc/check_renderer.c +++ b/tests/blogc/check_renderer.c @@ -15,7 +15,6 @@ #include #include "../../src/common/error.h" #include "../../src/common/utils.h" -#include "../../src/blogc/errors.h" #include "../../src/blogc/renderer.h" #include "../../src/blogc/source-parser.h" #include "../../src/blogc/template-parser.h" diff --git a/tests/blogc/check_source_parser.c b/tests/blogc/check_source_parser.c index 8753687..16acd96 100644 --- a/tests/blogc/check_source_parser.c +++ b/tests/blogc/check_source_parser.c @@ -13,7 +13,6 @@ #include #include "../../src/common/error.h" #include "../../src/common/utils.h" -#include "../../src/blogc/errors.h" #include "../../src/blogc/source-parser.h" diff --git a/tests/blogc/check_template_parser.c b/tests/blogc/check_template_parser.c index c520e94..3dfa423 100644 --- a/tests/blogc/check_template_parser.c +++ b/tests/blogc/check_template_parser.c @@ -13,7 +13,6 @@ #include #include "../../src/common/error.h" #include "../../src/common/utils.h" -#include "../../src/blogc/errors.h" #include "../../src/blogc/template-parser.h" -- cgit v1.2.3-18-g5258