From eb7d315f3ae6e4fc15ee97def4661e75746371ea Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Sun, 26 Jun 2022 21:06:59 +0200
Subject: [PATCH] Update error handling in `fmpi_ctx` module

---
 src/fmpi_ctx.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/fmpi_ctx.c b/src/fmpi_ctx.c
index e265efe..6d9a338 100644
--- a/src/fmpi_ctx.c
+++ b/src/fmpi_ctx.c
@@ -74,8 +74,10 @@ struct fmpi_ctx * fmpi_ctx_create(int * const argc, char ** argv[])
         .user_data = ctx
     });
     if(ctx->mpi == NULL) {
-        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI", "fmpi_mpi_init() failed!");
-        fmpi_ctx_destroy(&ctx);
+        FMPI_RAISE_ERROR(err_handler, "FMPI", "fmpi_mpi_init() failed!");
+        if(fmpi_ctx_destroy(&ctx) != FMPI_SUCCESS) {
+            FMPI_RAISE_ERROR(err_handler, "FMPI", "fmpi_ctx_destroy() failed!");
+        }
         return NULL;
     }
     // Futhark Initialization
@@ -84,8 +86,11 @@ struct fmpi_ctx * fmpi_ctx_create(int * const argc, char ** argv[])
         .user_data = ctx
     });
     if(ctx->fut == NULL) {
-        FMPI_RAISE_ERROR(ctx->err_handler, "FMPI", "fmpi_futhark_init() failed!");
-        fmpi_ctx_destroy(&ctx);
+        FMPI_RAISE_ERROR(err_handler, "FMPI", "fmpi_futhark_init() failed!");
+        if(fmpi_ctx_destroy(&ctx) != FMPI_SUCCESS) {
+            FMPI_RAISE_ERROR(err_handler, "FMPI", "fmpi_ctx_destroy() failed!");
+        }
+        return NULL;
     }
     return ctx;
 }
@@ -97,15 +102,17 @@ int fmpi_ctx_destroy(struct fmpi_ctx ** const ctx)
     assert(ctx != NULL);
     assert(*ctx != NULL);
 
-    int err_id = -1;
-    if((*ctx)->fut != NULL) {
-        err_id = fmpi_futhark_exit(&(*ctx)->fut);
+    int err = FMPI_SUCCESS;
+    if((*ctx)->fut != NULL && fmpi_futhark_exit(&(*ctx)->fut) != FMPI_SUCCESS) {
+        FMPI_RAISE_ERROR((*ctx)->err_handler, "FMPI", "fmpi_futhark_exit() failed!");
+        err = FMPI_ERROR;
     }
-    if((*ctx)->mpi != NULL) {
-         err_id = fmpi_mpi_exit(&(*ctx)->mpi);
+    if((*ctx)->mpi != NULL && fmpi_mpi_exit(&(*ctx)->mpi) != FMPI_SUCCESS) {
+        FMPI_RAISE_ERROR((*ctx)->err_handler, "FMPI", "fmpi_mpi_exit() failed!");
+        err = FMPI_ERROR;
     }
     free(*ctx); *ctx = NULL;
-    return err_id;
+    return err;
 }
 /*==============================================================================
     PRIVATE FUNCTION DEFINITION
-- 
GitLab