blob: bb289e2951468b04731c76b97f02275606902ead [file] [log] [blame]
swissChili6d02af42021-08-05 19:49:01 -07001#include "error.h"
2#include <string.h>
3#include <stdlib.h>
4#include <stdarg.h>
5#include <stdio.h>
6
7char *ehsprintf(const char *msg, ...)
8{
9 char *buf = malloc(1024);
10 va_list list;
11 va_start(list, msg);
12 vsnprintf(buf, 1023, msg, list);
13 va_end(list);
14
15 return buf;
16}
17
18void ereport(struct error err)
19{
20 if (err.loc.file && err.loc.line)
21 fprintf(stderr, "\033[31merror at\033[0m %s:%d\n", err.loc.file, err.loc.line);
22 else
23 fprintf(stderr, "\033[31merror\033[0m\n");
24
25 if (err.message)
26 fprintf(stderr, "%s\n", err.message);
27 else
28 {
29 switch (err.code)
30 {
31 case EEXPECTED:
32 fprintf(stderr, "Expected something but it was not found.\n");
33 break;
34 case EINVALID:
35 fprintf(stderr, "Invalid input.\n");
36 break;
37 case ENOTFOUND:
38 fprintf(stderr, "External resource not found.\n");
39 break;
40 default:
41 fprintf(stderr, "Unknown error %d\n", err.code);
42 }
43 }
44}
swissChili36f2c692021-08-08 14:31:44 -070045
46void edebug(struct error err, char *file, int line, const char *func, const char *why)
47{
48 if (!err.safe_state)
49 {
50 fprintf(stderr, "\033[43m%s at\033[0m %s:%d %s\n", why, file, line, func);
51 ereport(err);
52 }
53}