simplified error output

This commit is contained in:
hugogogo
2026-05-06 15:58:31 +02:00
parent 7f06d505d1
commit 8a479c3c3b
6 changed files with 27 additions and 61 deletions

View File

@@ -121,43 +121,21 @@ void print_state()
print_context_polynom();
}
int stop_errors(e_program_error err, const char *details, ...)
int stop_errors(const char *description, ...)
{
// the base error message
const char *msg = "error: error type is out of range";
// map error codes to messages
const char *error_messages[ERROR_SENTINEL] = {
[ERROR_BASE] = "undefined error",
[ARGUMENTS_ERROR] = "arguments error",
[ERROR_UNKNOWN_TOKEN] = "LEXER - unknown token",
[ERROR_NUMBER_TOO_BIG] = "LEXER - number is too big",
[ERROR_PARSING] = "PARSER - too much terms to parse",
[ERROR_TOKEN_POSITION] = "PARSER - token position is not good in grammar",
[ERROR_VAR_DIFF] = "PARSER - expression must only contain one variable",
[ERROR_TOKEN_COUNT] = "LEXER - token count error",
[ERROR_TERM_COUNT] = "PARSER - term count error",
};
// override msg if err is in the error_messages array
if (err >= ERROR_BASE && err < ERROR_SENTINEL)
{
msg = error_messages[err];
}
// print context
// print context (if debug mode)
print_state();
// print the base message
ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);
ft_putstr_fd("\e[1;31mERROR:\e[0m ", STDERR_FILENO);
// print the formatted details
// print the formatted description
va_list args;
va_start(args, details);
ft_vdprintf(STDERR_FILENO, details, args);
va_start(args, description);
ft_vdprintf(STDERR_FILENO, description, args);
va_end(args);
ft_putchar_fd('\n', STDERR_FILENO);
// print the base message
ft_dprintf(STDERR_FILENO, " (errno[%d] : %s)\n", errno, strerror(errno));
exit(err);
exit(EXIT_FAILURE);
}