Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nIboe-0003zZ-Kg for psycopg@arkaria.postgresql.org; Fri, 11 Feb 2022 19:42:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nIboc-0005Af-IE for psycopg@arkaria.postgresql.org; Fri, 11 Feb 2022 19:42:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nIboc-0005AB-Ac for psycopg@lists.postgresql.org; Fri, 11 Feb 2022 19:42:46 +0000 Received: from dog.birch.relay.mailchannels.net ([23.83.209.48]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nIboX-0001lc-IV for psycopg@postgresql.org; Fri, 11 Feb 2022 19:42:45 +0000 X-Sender-Id: cp7oy65bvo|x-authuser|paolo@paolodestefani.it Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 1B52C20B29; Fri, 11 Feb 2022 19:42:38 +0000 (UTC) Received: from lu-shared04.cpanelplatform.com (unknown [127.0.0.6]) (Authenticated sender: cp7oy65bvo) by relay.mailchannels.net (Postfix) with ESMTPA id B9F7D20BF7; Fri, 11 Feb 2022 19:42:36 +0000 (UTC) X-Sender-Id: cp7oy65bvo|x-authuser|paolo@paolodestefani.it Received: from lu-shared04.cpanelplatform.com ([UNAVAILABLE]. [107.189.4.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.107.255.151 (trex/6.5.3); Fri, 11 Feb 2022 19:42:37 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: cp7oy65bvo|x-authuser|paolo@paolodestefani.it X-MailChannels-Auth-Id: cp7oy65bvo X-Cooing-Ski: 5005d0a45f8d9ee1_1644608557753_4261271194 X-MC-Loop-Signature: 1644608557753:3205139196 X-MC-Ingress-Time: 1644608557753 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=paolodestefani.it; s=default; h=Content-Transfer-Encoding:Content-Type: Message-ID:References:In-Reply-To:Subject:Cc:To:From:Date:MIME-Version:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=5/GWneXSJM9Xv3PYQ5Z6Zd0/qKNKMZSRRFzW1jKxfS4=; b=M+2RUuGJMyl8IEAGW0YCAa0SMi efSMxe1wIvITfdY2tYvIuKY2xZhCSWkz8ISECOBgFjgocIgvEK1G9xYfkUwwFSNJUr7+sTZvuIWq8 ISghc0UQGOAlY+J8ytzIOkGVqNuEzU5wSBwgzKxbIjXU1ORSPaYhjFEmLXrzYrJ0EddajpbEbQ8d6 Y5jG6+U8lCdBU1AM07PxIwABb+0a/n45fgfxqrqTFAb7cIErzmGa8LwCNoqdx2m8zXnHPKPFg26RA xVKzXyjFXIKXKdcZ1rjsu1leHyA8s3Dlz5ZHaBN2H5OFSEBxNeqENwCWcPjOzLzkIVFbBOXsfaM9m oDW0If8g==; Received: from [::1] (port=36640 helo=lu-shared04.cpanelplatform.com) by lu-shared04.cpanelplatform.com with esmtpa (Exim 4.95) (envelope-from ) id 1nIboQ-0000Bd-8f; Fri, 11 Feb 2022 20:42:34 +0100 MIME-Version: 1.0 Date: Fri, 11 Feb 2022 20:42:34 +0100 From: Paolo De Stefani To: Daniele Varrazzo Cc: Psycopg Subject: Re: exception psycopg.Error from psycopg2 to psycopg 3 In-Reply-To: References: <11dedb1fce0dbbd6f125736347685fd5@paolodestefani.it> User-Agent: Roundcube Webmail/1.4.12 Message-ID: <3298f9c14d067440f828ccf8735f6e24@paolodestefani.it> X-Sender: paolo@paolodestefani.it Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-AuthUser: paolo@paolodestefani.it List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Yes perfect solution for me Thank your the lightspeed response.. Il 11/02/2022 19:47 Daniele Varrazzo ha scritto: > On Fri, 11 Feb 2022 at 18:34, Paolo De Stefani > wrote: > > Hi Paolo, > > Yes, I can see some inconsistency there. At the moment I suggest you > use `e.diag.sqlstate`, which works as expected and is available in > psycopg2 too. > > In [8]: try: cnn.execute(""" > ...: do $$ > ...: begin > ...: RAISE EXCEPTION 'Error wrong database' USING HINT = 'You > need to use a different database', ERRCODE = 'PA002'; > ...: end$$ > ...: language plpgsql > ...: """) > ...: except Exception as e: ex = e > > In [10]: ex.diag.sqlstate > Out[10]: 'PA002' > > In [11]: ex.sqlstate > None > > What is happening is that, in psycopg 3, Error.sqlstate is a class > property and is only set for the known classes - the ones listed at > . > > The attribute `e.diag.sqlstate` instead comes from the error message > received from the server. When the error is received, the matching > class is looked up by sqlstate, with the basic dbapi exceptions as > fallback if the code is not known, and the server result is passed to > the exception state, so that `e.diag` is populated with all the > details (such as the message, the hint etc). > > The cases I had in mind where 1) known exceptions where Error.sqlstate > and e.diag.sqlstate would match, and 2) non-server exceptions (e.g. on > connection, or raised by Python code) where Error.sqlstate and > e.diag.sqlstate are both None. > > I didn't think about the case 3) where a sqlstate exists, but psycopg > doesn't know it. In this case, the result of the current > implementation is to raise an exception with the sqlstate left to None > on the class but available in diag. > > ISTM that setting e.sqlstate = e.diag.sqlstate would be an > improvement. The docs describe indeed that sqlstate is expected to be > None on the DBAPI classes > (https://www.psycopg.org/psycopg3/docs/api/errors.html#psycopg.Error.sqlstate) > but that wasn't written thinking about the inconsistency above. It > makes more sense that Error.sqlstate is whatever state received, if > any. > > Does it sound right? > > -- Daniele -- Paolo De Stefani