error, qerror: add ErrorClass argument to error functions
[qemu.git] / qerror.c
blob0bf8aec947ca629e85c507c6c351433b674befe0
1 /*
2 * QError Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
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.
13 #include "monitor.h"
14 #include "qjson.h"
15 #include "qerror.h"
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject *obj);
20 static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
25 /**
26 * The 'desc' parameter is a printf-like string, the format of the format
27 * string is:
29 * %(KEY)
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
33 * Example:
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
38 * for example:
40 * "running out of foo: %(foo)%%"
42 * Please keep the entries in alphabetical order.
43 * Use scripts/check-qerror.sh to check.
45 static const QErrorStringTable qerror_table[] = {
47 QERR_ADD_CLIENT_FAILED,
48 "Could not add client",
51 QERR_AMBIGUOUS_PATH,
52 "Path '%(path)' does not uniquely identify an object"
55 QERR_BAD_BUS_FOR_DEVICE,
56 "Device '%(device)' can't go on a %(bad_bus_type) bus",
59 QERR_BASE_NOT_FOUND,
60 "Base '%(base)' not found",
63 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
64 "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
67 QERR_BUS_NO_HOTPLUG,
68 "Bus '%(bus)' does not support hotplugging",
71 QERR_BUS_NOT_FOUND,
72 "Bus '%(bus)' not found",
75 QERR_COMMAND_DISABLED,
76 "The command %(name) has been disabled for this instance",
79 QERR_COMMAND_NOT_FOUND,
80 "The command %(name) has not been found",
83 QERR_DEVICE_ENCRYPTED,
84 "'%(device)' (%(filename)) is encrypted",
87 QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
88 "Migration is disabled when using feature '%(feature)' in device '%(device)'",
91 QERR_DEVICE_HAS_NO_MEDIUM,
92 "Device '%(device)' has no medium",
95 QERR_DEVICE_INIT_FAILED,
96 "Device '%(device)' could not be initialized",
99 QERR_DEVICE_IN_USE,
100 "Device '%(device)' is in use",
103 QERR_DEVICE_IS_READ_ONLY,
104 "Device '%(device)' is read only",
107 QERR_DEVICE_LOCKED,
108 "Device '%(device)' is locked",
111 QERR_DEVICE_MULTIPLE_BUSSES,
112 "Device '%(device)' has multiple child busses",
115 QERR_DEVICE_NO_BUS,
116 "Device '%(device)' has no child bus",
119 QERR_DEVICE_NO_HOTPLUG,
120 "Device '%(device)' does not support hotplugging",
123 QERR_DEVICE_NOT_ACTIVE,
124 "Device '%(device)' has not been activated",
127 QERR_DEVICE_NOT_ENCRYPTED,
128 "Device '%(device)' is not encrypted",
131 QERR_DEVICE_NOT_FOUND,
132 "Device '%(device)' not found",
135 QERR_DEVICE_NOT_REMOVABLE,
136 "Device '%(device)' is not removable",
139 QERR_DUPLICATE_ID,
140 "Duplicate ID '%(id)' for %(object)",
143 QERR_FD_NOT_FOUND,
144 "File descriptor named '%(name)' not found",
147 QERR_FD_NOT_SUPPLIED,
148 "No file descriptor supplied via SCM_RIGHTS",
151 QERR_FEATURE_DISABLED,
152 "The feature '%(name)' is not enabled",
155 QERR_INVALID_BLOCK_FORMAT,
156 "Invalid block format '%(name)'",
159 QERR_INVALID_OPTION_GROUP,
160 "There is no option group '%(group)'",
163 QERR_INVALID_PARAMETER,
164 "Invalid parameter '%(name)'",
167 QERR_INVALID_PARAMETER_COMBINATION,
168 "Invalid parameter combination",
171 QERR_INVALID_PARAMETER_TYPE,
172 "Invalid parameter type for '%(name)', expected: %(expected)",
175 QERR_INVALID_PARAMETER_VALUE,
176 "Parameter '%(name)' expects %(expected)",
179 QERR_INVALID_PASSWORD,
180 "Password incorrect",
183 QERR_IO_ERROR,
184 "An IO error has occurred",
187 QERR_JSON_PARSE_ERROR,
188 "JSON parse error, %(message)",
192 QERR_JSON_PARSING,
193 "Invalid JSON syntax",
196 QERR_KVM_MISSING_CAP,
197 "Using KVM without %(capability), %(feature) unavailable",
200 QERR_MIGRATION_ACTIVE,
201 "There's a migration process in progress",
204 QERR_MIGRATION_NOT_SUPPORTED,
205 "State blocked by non-migratable device '%(device)'",
208 QERR_MIGRATION_EXPECTED,
209 "An incoming migration is expected before this command can be executed",
212 QERR_MISSING_PARAMETER,
213 "Parameter '%(name)' is missing",
216 QERR_NO_BUS_FOR_DEVICE,
217 "No '%(bus)' bus found for device '%(device)'",
220 QERR_NOT_SUPPORTED,
221 "Not supported",
224 QERR_OPEN_FILE_FAILED,
225 "Could not open '%(filename)'",
228 QERR_PERMISSION_DENIED,
229 "Insufficient permission to perform this operation",
232 QERR_PROPERTY_NOT_FOUND,
233 "Property '%(device).%(property)' not found",
236 QERR_PROPERTY_VALUE_BAD,
237 "Property '%(device).%(property)' doesn't take value '%(value)'",
240 QERR_PROPERTY_VALUE_IN_USE,
241 "Property '%(device).%(property)' can't take value '%(value)', it's in use",
244 QERR_PROPERTY_VALUE_NOT_FOUND,
245 "Property '%(device).%(property)' can't find value '%(value)'",
248 QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
249 "Property '%(device).%(property)' doesn't take "
250 "value '%(value)', it's not a power of 2",
253 QERR_PROPERTY_VALUE_OUT_OF_RANGE,
254 "Property '%(device).%(property)' doesn't take "
255 "value %(value) (minimum: %(min), maximum: %(max))",
258 QERR_QGA_COMMAND_FAILED,
259 "Guest agent command failed, error was '%(message)'",
262 QERR_QGA_LOGGING_FAILED,
263 "Guest agent failed to log non-optional log statement",
266 QERR_QMP_BAD_INPUT_OBJECT,
267 "Expected '%(expected)' in QMP input",
270 QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
271 "QMP input object member '%(member)' expects '%(expected)'",
274 QERR_QMP_EXTRA_MEMBER,
275 "QMP input object member '%(member)' is unexpected",
278 QERR_RESET_REQUIRED,
279 "Resetting the Virtual Machine is required",
282 QERR_SET_PASSWD_FAILED,
283 "Could not set password",
286 QERR_TOO_MANY_FILES,
287 "Too many open files",
290 QERR_UNDEFINED_ERROR,
291 "An undefined error has occurred",
294 QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
295 "'%(device)' uses a %(format) feature which is not "
296 "supported by this qemu version: %(feature)",
299 QERR_UNSUPPORTED,
300 "this feature or command is not currently supported",
303 QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
304 "Migration is disabled when VirtFS export path '%(path)' "
305 "is mounted in the guest using mount_tag '%(tag)'",
308 QERR_VNC_SERVER_FAILED,
309 "Could not start VNC server on %(target)",
312 QERR_SOCKET_CONNECT_FAILED,
313 "Failed to connect to socket",
316 QERR_SOCKET_LISTEN_FAILED,
317 "Failed to set socket to listening mode",
320 QERR_SOCKET_BIND_FAILED,
321 "Failed to bind socket",
324 QERR_SOCKET_CREATE_FAILED,
325 "Failed to create socket",
331 * qerror_new(): Create a new QError
333 * Return strong reference.
335 static QError *qerror_new(void)
337 QError *qerr;
339 qerr = g_malloc0(sizeof(*qerr));
340 QOBJECT_INIT(qerr, &qerror_type);
342 return qerr;
345 static QDict *error_obj_from_fmt_no_fail(const char *fmt, va_list *va)
347 QObject *obj;
348 QDict *ret;
350 obj = qobject_from_jsonv(fmt, va);
351 if (!obj) {
352 fprintf(stderr, "invalid json in error dict '%s'\n", fmt);
353 abort();
355 if (qobject_type(obj) != QTYPE_QDICT) {
356 fprintf(stderr, "error is not a dict '%s'\n", fmt);
357 abort();
360 ret = qobject_to_qdict(obj);
361 obj = qdict_get(ret, "class");
362 if (!obj) {
363 fprintf(stderr, "missing 'class' key in '%s'\n", fmt);
364 abort();
366 if (qobject_type(obj) != QTYPE_QSTRING) {
367 fprintf(stderr, "'class' key value should be a string in '%s'\n", fmt);
368 abort();
371 obj = qdict_get(ret, "data");
372 if (!obj) {
373 fprintf(stderr, "missing 'data' key in '%s'\n", fmt);
374 abort();
376 if (qobject_type(obj) != QTYPE_QDICT) {
377 fprintf(stderr, "'data' key value should be a dict in '%s'\n", fmt);
378 abort();
381 return ret;
385 * qerror_from_info(): Create a new QError from error information
387 * Return strong reference.
389 static QError *qerror_from_info(ErrorClass err_class, const char *fmt,
390 va_list *va)
392 QError *qerr;
394 qerr = qerror_new();
395 loc_save(&qerr->loc);
397 qerr->err_class = err_class;
398 qerr->error = error_obj_from_fmt_no_fail(fmt, va);
399 qerr->err_msg = qerror_format(fmt, qerr->error);
401 return qerr;
404 static void parse_error(const QErrorStringTable *entry, int c)
406 fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
407 abort();
410 static const char *append_field(QDict *error, QString *outstr,
411 const QErrorStringTable *entry,
412 const char *start)
414 QObject *obj;
415 QDict *qdict;
416 QString *key_qs;
417 const char *end, *key;
419 if (*start != '%')
420 parse_error(entry, '%');
421 start++;
422 if (*start != '(')
423 parse_error(entry, '(');
424 start++;
426 end = strchr(start, ')');
427 if (!end)
428 parse_error(entry, ')');
430 key_qs = qstring_from_substr(start, 0, end - start - 1);
431 key = qstring_get_str(key_qs);
433 qdict = qobject_to_qdict(qdict_get(error, "data"));
434 obj = qdict_get(qdict, key);
435 if (!obj) {
436 abort();
439 switch (qobject_type(obj)) {
440 case QTYPE_QSTRING:
441 qstring_append(outstr, qdict_get_str(qdict, key));
442 break;
443 case QTYPE_QINT:
444 qstring_append_int(outstr, qdict_get_int(qdict, key));
445 break;
446 default:
447 abort();
450 QDECREF(key_qs);
451 return ++end;
454 static QString *qerror_format_desc(QDict *error,
455 const QErrorStringTable *entry)
457 QString *qstring;
458 const char *p;
460 assert(entry != NULL);
462 qstring = qstring_new();
464 for (p = entry->desc; *p != '\0';) {
465 if (*p != '%') {
466 qstring_append_chr(qstring, *p++);
467 } else if (*(p + 1) == '%') {
468 qstring_append_chr(qstring, '%');
469 p += 2;
470 } else {
471 p = append_field(error, qstring, entry, p);
475 return qstring;
478 char *qerror_format(const char *fmt, QDict *error)
480 const QErrorStringTable *entry = NULL;
481 QString *qstr;
482 char *ret;
483 int i;
485 for (i = 0; qerror_table[i].error_fmt; i++) {
486 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
487 entry = &qerror_table[i];
488 break;
492 qstr = qerror_format_desc(error, entry);
493 ret = g_strdup(qstring_get_str(qstr));
494 QDECREF(qstr);
496 return ret;
500 * qerror_human(): Format QError data into human-readable string.
502 QString *qerror_human(const QError *qerror)
504 return qstring_from_str(qerror->err_msg);
508 * qerror_print(): Print QError data
510 * This function will print the member 'desc' of the specified QError object,
511 * it uses error_report() for this, so that the output is routed to the right
512 * place (ie. stderr or Monitor's device).
514 static void qerror_print(QError *qerror)
516 QString *qstring = qerror_human(qerror);
517 loc_push_restore(&qerror->loc);
518 error_report("%s", qstring_get_str(qstring));
519 loc_pop(&qerror->loc);
520 QDECREF(qstring);
523 void qerror_report(ErrorClass eclass, const char *fmt, ...)
525 va_list va;
526 QError *qerror;
528 va_start(va, fmt);
529 qerror = qerror_from_info(eclass, fmt, &va);
530 va_end(va);
532 if (monitor_cur_is_qmp()) {
533 monitor_set_error(cur_mon, qerror);
534 } else {
535 qerror_print(qerror);
536 QDECREF(qerror);
540 /* Evil... */
541 struct Error
543 QDict *obj;
544 char *msg;
545 ErrorClass err_class;
548 void qerror_report_err(Error *err)
550 QError *qerr;
552 qerr = qerror_new();
553 loc_save(&qerr->loc);
554 QINCREF(err->obj);
555 qerr->error = err->obj;
556 qerr->err_msg = g_strdup(err->msg);
557 qerr->err_class = err->err_class;
559 if (monitor_cur_is_qmp()) {
560 monitor_set_error(cur_mon, qerr);
561 } else {
562 qerror_print(qerr);
563 QDECREF(qerr);
567 void assert_no_error(Error *err)
569 if (err) {
570 qerror_report_err(err);
571 abort();
576 * qobject_to_qerror(): Convert a QObject into a QError
578 static QError *qobject_to_qerror(const QObject *obj)
580 if (qobject_type(obj) != QTYPE_QERROR) {
581 return NULL;
584 return container_of(obj, QError, base);
588 * qerror_destroy_obj(): Free all memory allocated by a QError
590 static void qerror_destroy_obj(QObject *obj)
592 QError *qerr;
594 assert(obj != NULL);
595 qerr = qobject_to_qerror(obj);
597 QDECREF(qerr->error);
598 g_free(qerr->err_msg);
599 g_free(qerr);