aboutsummaryrefslogtreecommitdiff
path: root/lib/error_handler.c
blob: 5130f3a57edc6e25020a5e3aab514be1a5cc015d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "spm.h"

int spmerrno = 0;
static char spmerrbuf[255];
static char spmerrbuf_reason[255];
const char *SPM_ERR_STRING[] = {
        "Success",
        "No root record",
        "Dangerous root path",
        "Package not found",
        "Invalid package",
        "Bad package checksum",
        "Failed to fetch package",
        "Manifest has no header",
        "Manifest has no data",
        "Parsing error",
        "Not implemented",
        "Failed to fetch data",
        NULL,
};

/**
 * Append error text to an existing spmerrno error string
 * @param reason message text
 */
void spmerrno_cause(const char *reason) {
    char *buf = spmerrbuf_reason;
    if (reason != NULL) {
        sprintf(buf, " (%s)", reason);
    }
}

/**
 * Translate a spmerrno code to an error string
 * @param code
 * @return
 */
char *spm_strerror(int code) {
    char *buf = spmerrbuf;
    int is_spm_error = SPM_ERR_CONFIRM(code);

    memset(buf, '\0', sizeof(spmerrbuf));
    if (is_spm_error == 0) {
        strcpy(buf, strerror(code));
    } else {
        strcpy(buf, SPM_ERR_STRING[SPM_ERR_INDEX(code)]);
    }

    if (strlen(spmerrbuf_reason)) {
        strcat(buf, spmerrbuf_reason);
    }
    return buf;
}

void spm_perror(const char *msg) {
    fprintf(stderr, "%s: %s\n", msg ? msg : "", spm_strerror(spmerrno));
}

void spm_die(void) {
    spm_perror("FATAL");
    exit(1);
}