From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) by gnuweeb.org (Postfix) with ESMTPSA id 80F3D804FD for ; Fri, 28 Oct 2022 18:26:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1666981580; bh=ZwAZb9TCMx62ZGIaKQbtXysXdLV2pZATgaGTYNp8MXE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=aRoTuNWlEqwC03dM8hTRAPl4FooU0dRK7FZs6lZNEqVR1F8ihGqn/yaqsUTFM2yK2 TBADFLpUVv/sdqwSTJeVEE1cH6cudiZGIf7IzKbfv3gDiGzi6ypAkl7xcg3THdX2EJ ytcjgvdx7cj0kyn+HG7y3J60AnBvkyPkducooD2L9bIzBFMOVA4LpVYvvtriRs2kFw YoygrsIfbF18B7kSAncdMkvRmFtae4B/bmvFsvhMFAdmbkUwhiOlctfzZBFMdx51Jc 79gZ1PA3/Xb5SKNJqYFSYPp/kRuNDh3kN2o151yyO885QTcNTJ2G0BYU92VTpkTTPs FNRmy/axCdrVQ== Received: by mail-lj1-f173.google.com with SMTP id b8so9436658ljf.0 for ; Fri, 28 Oct 2022 11:26:20 -0700 (PDT) X-Gm-Message-State: ACrzQf3AoG2hm3Mkj8C7nW5dQeBZJRESdfdwK9eopru4tMM6vYpXLYWz EU8NO/r1Q/p5xGlL+CBTtoNQEfb+zAV+OaTmtjM= X-Google-Smtp-Source: AMsMyM5FTXKDcrJ1/28CRxyyXs0SrgDFivIrOAo4vd828nHQnQmzkdYS2AWHE5hm5XjvUIEhZJUj2hVkqR6Dw30+aZI= X-Received: by 2002:a2e:a239:0:b0:277:1d99:ab0c with SMTP id i25-20020a2ea239000000b002771d99ab0cmr338900ljm.111.1666981578696; Fri, 28 Oct 2022 11:26:18 -0700 (PDT) MIME-Version: 1.0 References: <20221027150823.601914-1-ammarfaizi2@gnuweeb.org> <20221027150823.601914-3-ammarfaizi2@gnuweeb.org> <1d500d37-b11b-75fd-38e5-d7f8e0a9b1d4@gnuweeb.org> <3a79a587-ddee-9e25-2ac5-b573938b44a9@gnuweeb.org> <043f55c3-67d8-9130-aca4-73c59926d2af@gnuweeb.org> <63aa1b63-f7e2-8da3-b16d-0c7e1045d697@gnuweeb.org> In-Reply-To: <63aa1b63-f7e2-8da3-b16d-0c7e1045d697@gnuweeb.org> From: Alviro Iskandar Setiawan Date: Sat, 29 Oct 2022 01:26:07 +0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 2/2] daemon: telegram: Handle MySQL error To: Muhammad Rizki Cc: Ammar Faizi , "GNU/Weeb Mailing List" Content-Type: text/plain; charset="UTF-8" List-Id: On Sat, Oct 29, 2022 at 1:10 AM Muhammad Rizki wrote: > On 28/10/22 23.46, Alviro Iskandar Setiawan wrote: >> Even this part is also problematic, if you hit an error before >> utils.remove_patch(), the patch file won't get removed because >> exception will stop the current execution and throw the exception to >> the previous call stack. > > You are right. So we just check every value from the DB method and if > it's None just return like goto? You don't use goto in Python. In an OOP style, the cleanup usually happens in the destructor, or in a "try with finally" statement. >> >> I hate try and catch style, because it's very easy to miss an error >> like this. I prefer the way C handles error manually via return value >> like: >> >> ret = func_call(); >> if (ret < 0) >> goto clean_up; >> > > I've improved the remove_patch() earlier using the glob UNIX style path > to check if all temp dirs is exists then remove them all. What do you think? It doesn't address the issue. You still don't understand the underlying issue behind your remove_patch() placement. You have this: for d, f in files: await m.reply_document(f"{d}/{f}", file_name=f) await asyncio.sleep(1) utils.remove_patch(files) What happens if you follow that for loop, then m.reply_document() throws an exception? The answer is: utils.remove_patch(files) will *not* be executed because it will throw the exception to the previous call stack, and if the previous call stack doesn't have a "try and except" statement, it will throw the exception to the previous call stack again, until at some point it is handled by and "try and except". If it never hits a "try and except" statement, the application will terminate. You don't do the cleanup if: - An error happens *after you create the patch file*. - But before the remove_patch() is executed. There are 2 possible solutions, either using a "try with finally" statement, or using a destructor wrapped in a class.