summaryrefslogtreecommitdiffstats
path: root/rdcp_error.c
blob: f688eaf41513583f3392c4bb45620c22e2fce496 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include "rdcp_util.h"
#include "rdcp_error.h"

GQuark
rdcp_error_quark (void)
{
    static volatile gsize quark_volatile = 0;

    if (quark_volatile == 0)
        quark_volatile = g_quark_from_static_string ("rdcp-error");

    return (GQuark)quark_volatile;
}

const char *
rdcp_error_code_to_string(rdcp_error_codes ec)
{
    static char buf[80];

    switch(ec) {
    case RDCP_ERROR_INTERNAL:            return "RDCP_ERROR_INTERNAL";
    case RDCP_ERROR_INVALID_ARG:         return "RDCP_ERROR_INVALID_ARG";
    case RDCP_ERROR_INVALID_INSTANCE_ID: return "RDCP_ERROR_INVALID_INSTANCE_ID";
    case RDCP_ERROR_DBUS:                return "RDCP_ERROR_DBUS";
    default:
        snprintf(buf, sizeof(buf), "unknown error code (%d)", ec);
        return buf;
    }
}

/*----------------------------------------------------------------------------*/

/**
 * handle_g_error:
 * @g_error pointer to non-NULL GError pointer describing problem
 * @mb CMPI message broker
 * @st CMPI status result
 * @rc CMPI return code
 * @format printf-style format string, may be #NULL if no additional
 * message is desired.
 *
 * Sets @st status to the @rc return code and an optional printf
 * styles formatted message which is prepended to the error message
 * contained in the g_error. It frees the g_error.
 *
 * Returns: the @st status passed in.
 */
CMPIStatus
handle_g_error(GError **g_error, const CMPIBroker* mb, CMPIStatus* st, CMPIrc rc,
               const gchar *format, ...)
{
    CMPIStatus failsafe_status;
    GString *message;
    va_list va;

    CMSetStatus(&failsafe_status, CMPI_RC_ERR_FAILED);
    g_return_val_if_fail (g_error != NULL && *g_error != NULL, failsafe_status);
    g_return_val_if_fail (st != NULL, failsafe_status);

    message = g_string_sized_new(DEFAULT_STATUS_MSG_SIZE);
    g_string_append_printf(message, "%s: ", ORGID);

    if (format) {
        va_start(va, format);
        g_string_append_vprintf(message, format, va);
        va_end(va);
        g_string_append(message, ": ");
    }

    g_string_append_printf(message, "(%s) ", rdcp_error_code_to_string((*g_error)->code));
    g_string_append(message, (*g_error)->message);
    g_error_free(*g_error);
    *g_error = NULL;

    CMSetStatusWithChars(mb, st, rc, message->str);
    g_string_free(message, TRUE);

    return *st;
}


/**
 * SetCMPIStatus:
 * @mb CMPI message broker
 * @st CMPI status result
 * @rc CMPI return code
 * @format printf-style format string, may be #NULL if no additional
 * message is desired.
 *
 * Sets @st status to the @rc return code and an optional printf
 * style formatted message.
 *
 * Returns: the @st status passed in.
 */
CMPIStatus
SetCMPIStatus(const CMPIBroker* mb, CMPIStatus* st, CMPIrc rc,
              const gchar *format, ...)
{
    CMPIStatus failsafe_status;
    GString *message = NULL;
    va_list va;

    CMSetStatus(&failsafe_status, CMPI_RC_ERR_FAILED);
    g_return_val_if_fail (st != NULL, failsafe_status);

    if (format) {
        message = g_string_sized_new(DEFAULT_STATUS_MSG_SIZE);
        g_string_append_printf(message, "%s: ", ORGID);

        va_start(va, format);
        g_string_append_vprintf(message, format, va);
        va_end(va);

        CMSetStatusWithChars(mb, st, rc, message->str);
        g_string_free(message, TRUE);
    } else {
        CMSetStatus(st, rc);
    }

    return *st;
}