2 * QError: QEMU Error data-type.
4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject
*obj
);
20 static const QType qerror_type
= {
22 .destroy
= qerror_destroy_obj
,
26 * The 'desc' parameter is a printf-like string, the format of the format
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
40 * "running out of foo: %(foo)%%"
42 static const QErrorStringTable qerror_table
[] = {
44 .error_fmt
= QERR_COMMAND_NOT_FOUND
,
45 .desc
= "The command %(name) has not been found",
48 .error_fmt
= QERR_DEVICE_ENCRYPTED
,
49 .desc
= "The %(device) is encrypted",
52 .error_fmt
= QERR_DEVICE_LOCKED
,
53 .desc
= "Device %(device) is locked",
56 .error_fmt
= QERR_DEVICE_NOT_ACTIVE
,
57 .desc
= "The %(device) device has not been activated by the guest",
60 .error_fmt
= QERR_DEVICE_NOT_FOUND
,
61 .desc
= "The %(device) device has not been found",
64 .error_fmt
= QERR_DEVICE_NOT_REMOVABLE
,
65 .desc
= "Device %(device) is not removable",
68 .error_fmt
= QERR_INVALID_BLOCK_FORMAT
,
69 .desc
= "Invalid block format %(name)",
72 .error_fmt
= QERR_INVALID_PARAMETER_TYPE
,
73 .desc
= "Invalid parameter type, expected: %(expected)",
76 .error_fmt
= QERR_INVALID_PASSWORD
,
77 .desc
= "The entered password is invalid",
80 .error_fmt
= QERR_JSON_PARSING
,
81 .desc
= "Invalid JSON syntax",
84 .error_fmt
= QERR_KVM_MISSING_CAP
,
85 .desc
= "Using KVM without %(capability), %(feature) unavailable",
88 .error_fmt
= QERR_MISSING_PARAMETER
,
89 .desc
= "Parameter %(name) is missing",
92 .error_fmt
= QERR_QMP_BAD_INPUT_OBJECT
,
93 .desc
= "Bad QMP input object",
96 .error_fmt
= QERR_UNDEFINED_ERROR
,
97 .desc
= "An undefined error has ocurred",
103 * qerror_new(): Create a new QError
105 * Return strong reference.
107 QError
*qerror_new(void)
111 qerr
= qemu_mallocz(sizeof(*qerr
));
112 QOBJECT_INIT(qerr
, &qerror_type
);
117 static void qerror_abort(const QError
*qerr
, const char *fmt
, ...)
121 fprintf(stderr
, "qerror: bad call in function '%s':\n", qerr
->func
);
122 fprintf(stderr
, "qerror: -> ");
125 vfprintf(stderr
, fmt
, ap
);
128 fprintf(stderr
, "\nqerror: call at %s:%d\n", qerr
->file
, qerr
->linenr
);
132 static void qerror_set_data(QError
*qerr
, const char *fmt
, va_list *va
)
136 obj
= qobject_from_jsonv(fmt
, va
);
138 qerror_abort(qerr
, "invalid format '%s'", fmt
);
140 if (qobject_type(obj
) != QTYPE_QDICT
) {
141 qerror_abort(qerr
, "error format is not a QDict '%s'", fmt
);
144 qerr
->error
= qobject_to_qdict(obj
);
146 obj
= qdict_get(qerr
->error
, "class");
148 qerror_abort(qerr
, "missing 'class' key in '%s'", fmt
);
150 if (qobject_type(obj
) != QTYPE_QSTRING
) {
151 qerror_abort(qerr
, "'class' key value should be a QString");
154 obj
= qdict_get(qerr
->error
, "data");
156 qerror_abort(qerr
, "missing 'data' key in '%s'", fmt
);
158 if (qobject_type(obj
) != QTYPE_QDICT
) {
159 qerror_abort(qerr
, "'data' key value should be a QDICT");
163 static void qerror_set_desc(QError
*qerr
, const char *fmt
)
167 // FIXME: inefficient loop
169 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
170 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
171 qerr
->entry
= &qerror_table
[i
];
176 qerror_abort(qerr
, "error format '%s' not found", fmt
);
180 * qerror_from_info(): Create a new QError from error information
182 * The information consists of:
184 * - file the file name of where the error occurred
185 * - linenr the line number of where the error occurred
186 * - func the function name of where the error occurred
187 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
189 * - va va_list of all arguments specified by fmt
191 * Return strong reference.
193 QError
*qerror_from_info(const char *file
, int linenr
, const char *func
,
194 const char *fmt
, va_list *va
)
199 qerr
->linenr
= linenr
;
204 qerror_abort(qerr
, "QDict not specified");
207 qerror_set_data(qerr
, fmt
, va
);
208 qerror_set_desc(qerr
, fmt
);
213 static void parse_error(const QError
*qerror
, int c
)
215 qerror_abort(qerror
, "expected '%c' in '%s'", c
, qerror
->entry
->desc
);
218 static const char *append_field(QString
*outstr
, const QError
*qerror
,
224 const char *end
, *key
;
227 parse_error(qerror
, '%');
230 parse_error(qerror
, '(');
233 end
= strchr(start
, ')');
235 parse_error(qerror
, ')');
237 key_qs
= qstring_from_substr(start
, 0, end
- start
- 1);
238 key
= qstring_get_str(key_qs
);
240 qdict
= qobject_to_qdict(qdict_get(qerror
->error
, "data"));
241 obj
= qdict_get(qdict
, key
);
243 qerror_abort(qerror
, "key '%s' not found in QDict", key
);
246 switch (qobject_type(obj
)) {
248 qstring_append(outstr
, qdict_get_str(qdict
, key
));
251 qstring_append_int(outstr
, qdict_get_int(qdict
, key
));
254 qerror_abort(qerror
, "invalid type '%c'", qobject_type(obj
));
262 * qerror_print(): Print QError data
264 * This function will print the member 'desc' of the specified QError object,
265 * it uses qemu_error() for this, so that the output is routed to the right
266 * place (ie. stderr or Monitor's device).
268 void qerror_print(const QError
*qerror
)
273 assert(qerror
->entry
!= NULL
);
275 qstring
= qstring_new();
277 for (p
= qerror
->entry
->desc
; *p
!= '\0';) {
279 qstring_append_chr(qstring
, *p
++);
280 } else if (*(p
+ 1) == '%') {
281 qstring_append_chr(qstring
, '%');
284 p
= append_field(qstring
, qerror
, p
);
288 qemu_error("%s\n", qstring_get_str(qstring
));
293 * qobject_to_qerror(): Convert a QObject into a QError
295 QError
*qobject_to_qerror(const QObject
*obj
)
297 if (qobject_type(obj
) != QTYPE_QERROR
) {
301 return container_of(obj
, QError
, base
);
305 * qerror_destroy_obj(): Free all memory allocated by a QError
307 static void qerror_destroy_obj(QObject
*obj
)
312 qerr
= qobject_to_qerror(obj
);
314 QDECREF(qerr
->error
);