swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1 | #include "error.h" |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | char *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 | |
| 18 | void 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 | } |