This repository has been archived on 2022-01-16. You can view files and clone it, but cannot push or open issues or pull requests.
corelibs/src/types/error/error.c

93 lines
2.9 KiB
C

/*
* This file is part of corelibs. (https://git.redxen.eu/corelibs)
* Copyright (c) 2021 Alex-David Denes
*
* corelibs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* corelibs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with corelibs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "types/error/error.h"
#include <stdint.h>
#include <stdio.h>
static const char *corelibs_error_string (cl_error_lang, cl_error_t);
enum {
CORELIBS_ERROR_LANG_EN = 0,
CORELIBS_ERROR_LANG_COUNT, // Must be last
};
enum {
CORELIBS_ERROR_ERR_OK = 0,
CORELIBS_ERROR_ERR_UNK,
CORELIBS_ERROR_ERR_UNDEF,
CORELIBS_ERROR_ERR_INVAL,
CORELIBS_ERROR_ERR_MEM_ALLOC,
CORELIBS_ERROR_ERR_MEM_NULL,
CORELIBS_ERROR_ERR_MEM_OOB,
CORELIBS_ERROR_ERR_DATA_IMMUT,
CORELIBS_ERROR_ERR_DATA_INVAL,
CORELIBS_ERROR_ERR_DATA_INCOMPAT,
CORELIBS_ERROR_ERR_ARGS_MIS,
CORELIBS_ERROR_ERR_COUNT, // Must be last
};
static const char *const cl_error_strings[CORELIBS_ERROR_LANG_COUNT][CORELIBS_ERROR_ERR_COUNT] = {
[CORELIBS_ERROR_LANG_EN] = {
// General
[CORELIBS_ERROR_ERR_OK] = "OK",
[CORELIBS_ERROR_ERR_UNK] = "Unknown error",
[CORELIBS_ERROR_ERR_UNDEF] = "Undefined or missing variable",
[CORELIBS_ERROR_ERR_INVAL] = "Invalid action",
// Memory
[CORELIBS_ERROR_ERR_MEM_ALLOC] = "Allocation failed",
[CORELIBS_ERROR_ERR_MEM_NULL] = "NULL address",
[CORELIBS_ERROR_ERR_MEM_OOB] = "Out of bounds",
// Data
[CORELIBS_ERROR_ERR_DATA_IMMUT] = "Immutable data",
[CORELIBS_ERROR_ERR_DATA_INCOMPAT] = "Incompatible data",
[CORELIBS_ERROR_ERR_DATA_INVAL] = "Invalid data",
// Arguments
[CORELIBS_ERROR_ERR_ARGS_MIS] = "Missing argument(s)",
},
};
struct corelibs_error_tree const cl_error = {
.string = corelibs_error_string,
.err = {
.ok = CORELIBS_ERROR_ERR_OK,
.unk = CORELIBS_ERROR_ERR_UNK,
.undef = CORELIBS_ERROR_ERR_UNDEF,
.mem = {
.null = CORELIBS_ERROR_ERR_MEM_NULL,
.alloc = CORELIBS_ERROR_ERR_MEM_ALLOC,
.oob = CORELIBS_ERROR_ERR_MEM_OOB,
},
.data = {
.immut = CORELIBS_ERROR_ERR_DATA_IMMUT,
.incompat = CORELIBS_ERROR_ERR_DATA_INCOMPAT,
.inval = CORELIBS_ERROR_ERR_DATA_INVAL,
},
.args = {
.mis = CORELIBS_ERROR_ERR_ARGS_MIS,
},
},
};
static const char *
corelibs_error_string (const cl_error_lang lang, const cl_error_t err) {
if (lang >= CORELIBS_ERROR_LANG_COUNT || err < 0 || err >= CORELIBS_ERROR_ERR_COUNT) return NULL;
return cl_error_strings[lang][err];
}