From nobody Tue Feb 10 09:24:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1646668836; cv=none; d=zohomail.com; s=zohoarc; b=TBUgpBWflqxHYZorYlBHHrKZOHd6vDpg7cY9fui+By44YKrrNIEAEjIcdlVvGrl9xyICIWqwgvDaI5LrxEy0bMZ52q0Gz6BMTpIlPm0d2+gU6fEvK2JYKflrZAK8QL/At9/U+hPIunITUoQ+YHrJJUrnc3YZSrdAsXhulJgZn8s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646668836; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/+/GrIHIqQcTxJmQvmFc9N415dF4IPRPqavyIU0U/tU=; b=I4G51hbxG8kgAjnsG8Y5cRLR5AtfJTTvTxTrBZc1yHxBohs+SS5vFzdlLqGmBO13bloKuI3YokOpLl7/EJ0j/Z+Z5p86fBMWVx95Y6DQuKm/2lFnEfKSo51zTDelXmGnUsWQ4VT9JnjI0N0+o5DJnEzAGWjWq1pRqfV/yM61s0c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1646668836944325.0210290460051; Mon, 7 Mar 2022 08:00:36 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-330-I96oVY6jPPOcfkLbdt3oHg-1; Mon, 07 Mar 2022 11:00:28 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BCD288FA3CE; Mon, 7 Mar 2022 16:00:08 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F8F7400C849; Mon, 7 Mar 2022 16:00:08 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 17397196BB81; Mon, 7 Mar 2022 16:00:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 614D41931BEA for ; Mon, 7 Mar 2022 16:00:06 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 325E27D3CE; Mon, 7 Mar 2022 16:00:06 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 542DC82764 for ; Mon, 7 Mar 2022 16:00:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646668835; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/+/GrIHIqQcTxJmQvmFc9N415dF4IPRPqavyIU0U/tU=; b=gy0KoJ+9lU5Mg4aGt8pVCYcDLvJcDcvwXjUVpGz+zujxGYpg4QNV6K+LSWO/BrPZuqDwUZ NDV/owCAY3dt/rS/N99YWPNKPf9+eG7NA+F6XIm/dKI2FSLAhb+lBiZ7wkfMwt8Pso4WLr zOiluk8SuyKW8+PAlnbC2bQormS3hCg= X-MC-Unique: I96oVY6jPPOcfkLbdt3oHg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 08/17] docs: Convert 'errors' page to rST Date: Mon, 7 Mar 2022 16:59:28 +0100 Message-Id: <3e7fa7a3f2cd79664d9978d139273d952f97e83e.1646668723.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1646668838114100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- docs/errors.html.in | 84 ---------------------------------- docs/errors.rst | 109 ++++++++++++++++++++++++++++++++++++++++++++ docs/meson.build | 2 +- 3 files changed, 110 insertions(+), 85 deletions(-) delete mode 100644 docs/errors.html.in create mode 100644 docs/errors.rst diff --git a/docs/errors.html.in b/docs/errors.html.in deleted file mode 100644 index ea4272822f..0000000000 --- a/docs/errors.html.in +++ /dev/null @@ -1,84 +0,0 @@ - - - - -

Handling of errors

-

The main goals of libvirt when it comes to error handling are:

-
    -
  • provide as much detail as possible
  • -
  • provide the information as soon as possible
  • -
  • dont force the library user into one style of error handling
  • -
-

As result the library provide both synchronous, callback based and -asynchronous error reporting. When an error happens in the library code the -error is logged, allowing to retrieve it later and if the user registered = an -error callback it will be called synchronously. Once the call to libvirt e= nds -the error can be detected by the return value and the full information for -the last logged error can be retrieved.

-

To avoid as much as possible troubles with a global variable in a -multithreaded environment, libvirt will associate when possible the errors= to -the current connection they are related to, that way the error is stored i= n a -dynamic structure which can be made thread specific. Error callback can be -set specifically to a connection with

-

So error handling in the code is the following:

-
    -
  1. if the error can be associated to a connection for example when = failing - to look up a domain -
    1. if there is a callback associated to the connection set with <= a href=3D"html/libvirt-virterror.html#virConnSetErrorFunc">virConnSetErrorF= unc, - call it with the error information
    2. otherwise if there is a= global callback set with virSetErrorFunc, - call it with the error information
    3. otherwise call virDefaultErrorFunc - which is the default error function of the library issuing the err= or - on stderr
    4. save the error in the connection for later retri= eval with virCo= nnGetLastError
  2. -
  3. otherwise like when failing to create a hypervisor connection: -
    1. if there is a global callback set with virSetErrorFunc, - call it with the error information
    2. otherwise call virDefaultErrorFunc - which is the default error function of the library issuing the err= or - on stderr
    3. save the error in the connection for later retri= eval with virGetLas= tError
  4. -
-

In all cases the error information is provided as a virErrorPtr pointer to -read-only structure virEr= ror containing the -following fields:

- -

and then extra raw information about the error which may be initial= ized -to 0 or NULL if unused

-
    -
  • str1, str2, str3: string information, usually str1 is the error - message format
  • -
  • int1, int2: integer information
  • -
-

So usually, setting up specific error handling with libvirt consist= of -registering a handler with virSetErrorFunc or -with virConnSe= tErrorFunc, -check the value of the code value, take appropriate action, if needed let -libvirt print the error on stderr by calling virDefaultErrorFunc. -For asynchronous error handing, set such a function doing nothing to avoid -the error being reported on stderr, and call virConnGetLastError or -virGetLastError when an API call returned an error value. It can be a good -idea to use virR= esetError or virConnResetLastError -once an error has been processed fully.

-

At the python level, there only a global reporting callback functio= n at -this point, see the error.py example about it:

-
def handler(ctxt, err):
-    global errno
-
-    #print "handler(%s, %s)" % (ctxt, err)
-    errno =3D err
-
-libvirt.registerErrorHandler(handler, 'context') 
-

the second argument to the registerErrorHandler function is passed = as the -first argument of the callback like in the C version. The error is a tuple -containing the same field as a virError in C, but cast to Python.

- - diff --git a/docs/errors.rst b/docs/errors.rst new file mode 100644 index 0000000000..07c0fb1fc0 --- /dev/null +++ b/docs/errors.rst @@ -0,0 +1,109 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Handling of errors +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The main goals of libvirt when it comes to error handling are: + +- provide as much detail as possible +- provide the information as soon as possible +- dont force the library user into one style of error handling + +As result the library provide both synchronous, callback based and asynchr= onous +error reporting. When an error happens in the library code the error is lo= gged, +allowing to retrieve it later and if the user registered an error callback= it +will be called synchronously. Once the call to libvirt ends the error can = be +detected by the return value and the full information for the last logged = error +can be retrieved. + +To avoid as much as possible troubles with a global variable in a multithr= eaded +environment, libvirt will associate when possible the errors to the current +connection they are related to, that way the error is stored in a dynamic +structure which can be made thread specific. Error callback can be set +specifically to a connection with + +So error handling in the code is the following: + +#. if the error can be associated to a connection for example when failing= to + look up a domain + + #. if there is a callback associated to the connection set with + `virConnSetErrorFunc `__, + call it with the error information + #. otherwise if there is a global callback set with + `virSetErrorFunc `__, c= all it + with the error information + #. otherwise call + `virDefaultErrorFunc `__ + which is the default error function of the library issuing the error= on + stderr + #. save the error in the connection for later retrieval with + `virConnGetLastError `__ + +#. otherwise like when failing to create a hypervisor connection: + + #. if there is a global callback set with + `virSetErrorFunc `__, c= all it + with the error information + #. otherwise call + `virDefaultErrorFunc `__ + which is the default error function of the library issuing the error= on + stderr + #. save the error in the connection for later retrieval with + `virGetLastError `__ + +In all cases the error information is provided as a +`virErrorPtr `__ pointer to read-= only +structure `virError `__ containing t= he +following fields: + +- code: an error number from the + `virErrorNumber `__ enum +- domain: an enum indicating which part of libvirt raised the error see + `virErrorDomain `__ +- level: the error level, usually VIR_ERR_ERROR, though there is room for + warnings like VIR_ERR_WARNING +- message: the full human-readable formatted string of the error +- conn: if available a pointer to the + `virConnectPtr `__ connec= tion + to the hypervisor where this happened +- dom: if available a pointer to the + `virDomainPtr `__ domain + targeted in the operation + +and then extra raw information about the error which may be initialized to= 0 or +NULL if unused + +- str1, str2, str3: string information, usually str1 is the error message + format +- int1, int2: integer information + +So usually, setting up specific error handling with libvirt consist of +registering a handler with +`virSetErrorFunc `__ or with +`virConnSetErrorFunc `__,= check +the value of the code value, take appropriate action, if needed let libvirt +print the error on stderr by calling +`virDefaultErrorFunc `__.= For +asynchronous error handing, set such a function doing nothing to avoid the= error +being reported on stderr, and call virConnGetLastError or virGetLastError = when +an API call returned an error value. It can be a good idea to use +`virResetError `__ or +`virConnResetLastError = `__ +once an error has been processed fully. + +At the python level, there only a global reporting callback function at th= is +point, see the error.py example about it: + +:: + + def handler(ctxt, err): + global errno + + #print "handler(%s, %s)" % (ctxt, err) + errno =3D err + + libvirt.registerErrorHandler(handler, 'context') + +the second argument to the registerErrorHandler function is passed as the = first +argument of the callback like in the C version. The error is a tuple conta= ining +the same field as a virError in C, but cast to Python. diff --git a/docs/meson.build b/docs/meson.build index e92ce6bd9e..d0c1217351 100644 --- a/docs/meson.build +++ b/docs/meson.build @@ -39,7 +39,6 @@ docs_html_in_files =3D [ 'drvvirtuozzo', 'drvvmware', 'drvxen', - 'errors', 'firewall', 'formatcaps', 'formatdomaincaps', @@ -93,6 +92,7 @@ docs_rst_files =3D [ 'developer-tooling', 'drvqemu', 'drvch', + 'errors', 'formatbackup', 'formatcheckpoint', 'formatdomain', --=20 2.35.1