~pbatch/patchwerk

patchwerk/error.w -rw-r--r-- 2.2 KiB
9c265356 — paul plan9 additions from Sigrid 3 months ago
                                                                                
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@** Error handling.
\mkref{error}
When something goes wrong, it is helpful to know why.
This implements a reasonably minimal error handling interface.

@ The error codes are supplied in an enum. This is to be included in the header.
@<Header@>+=
enum {
    @<Error Codes@>
};

@ The error codes are as follows:

|PW_OK| indicates that things completed successfully without error.

|PW_NOT_OK| is a general error code.

|PW_INVALID_CABLE| indicates a nonexistent or invalid cable.

|PW_INVALID_NODE| indicates a nonexistent or invalid node.

|PW_INVALID_BUFFER| indicates a nonexistent or invalid buffer.

|PW_ALREADY_ALLOCATED| when a patch is attempted to be reallocated.

|PW_CONNECTION_MISMATCH| happens when a cable attempts to connect to a cable
belonging to a lower ranking node.

|PW_NOT_ENOUGH_NODES| happens in |pw_patch_new_node| when the nodes in the
patch are filled up and there are no nodes left.

|PW_NULL_VALUE| happens when a value attempted to be read is NULL.

@<Error Codes@>=
PW_OK,@/
PW_NOT_OK,@/
PW_INVALID_CABLE,@/
PW_INVALID_NODE,@/
PW_INVALID_BUFFER,@/
PW_ALREADY_ALLOCATED,@/
PW_CONNECTION_MISMATCH,@/
PW_INVALID_ENTRY,@/
PW_NOT_ENOUGH_NODES,@/
PW_NULL_VALUE,@/
PW_I_DONT_KNOW,@/
PW_POOL_FULL,@/
PW_STACK_OVERFLOW@/

@ Each error code corresponds to a entry inside of an array of constant
string literals.
@<Top@> +=
static const char *errmsg[] = {
"Everything is okay!",
"Oops! Something went wrong.",
"Invalid cable.",
"Invalid node.",
"Invalid buffer.",
"This thing has already been allocated.",
"Cables can only connect to nodes with a higher id.",
"Position out of range in ugen list.",
"Patch is out of nodes. Consider allocating more nodes.",
"Attempted to read a NULL value.",
"I'm not actually sure what happened.",
"Buffer Pool is Full.",
"Stack Overflow.",
};

@ The function |pw_error| returns the string at a particular index position.

@<Header@>+=
const char * pw_error(int rc);

@ @<Top@> +=
const char * pw_error(int rc)
{
    if(rc >= (sizeof(errmsg)/sizeof(*errmsg)) || rc < 0) {
        return errmsg[PW_I_DONT_KNOW];
    } else {
        return errmsg[rc];
    }
}

@ Error handling implements a macro which does an early return on bad return
codes.

@<Header@>+=
#define PW_ERROR_CHECK(rc) if(rc != PW_OK) return rc