From: Michael Tremer Date: Fri, 27 Jun 2025 10:07:40 +0000 (+0000) Subject: CLI: Add a function to show the API errors X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=837681fdfe2c38ac97694d726aac3af6d8e614ab;p=pakfire.git CLI: Add a function to show the API errors Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index af3aa396..20bc82ec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -486,6 +486,8 @@ noinst_LTLIBRARIES += \ libcli.la libcli_la_SOURCES = \ + src/cli/lib/api.c \ + src/cli/lib/api.h \ src/cli/lib/assert.c \ src/cli/lib/assert.h \ src/cli/lib/auth.c \ diff --git a/src/cli/lib/api.c b/src/cli/lib/api.c new file mode 100644 index 00000000..d92cad75 --- /dev/null +++ b/src/cli/lib/api.c @@ -0,0 +1,85 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include + +#include + +#include "api.h" + +int cli_api_error(const struct pakfire_xfer_response* response, + const char* format, ...) { + char* prefix = NULL; + char* error = NULL; + va_list args; + int r; + + // Do nothing if there has not been an error + if (!response->error) + return 0; + + // Fetch the error + r = pakfire_xfer_response_get_error(response, &error); + if (r < 0) + goto ERROR; + + // Format the prefix + if (format) { + va_start(args, format); + r = vasprintf(&prefix, format, args); + va_end(args); + + // Handle any errors + if (r < 0) { + r = -errno; + goto ERROR; + } + } + + // Print prefix & error + if (prefix && error) + r = fprintf(stderr, "%s: %s\n", prefix, error); + + // If there is no error, we only print the prefix + else if (prefix) + r = fprintf(stderr, "%s\n", prefix); + + // If there is no prefix, we only print the error + else if (error) + r = fprintf(stderr, "%s\n", error); + + // Handle any errors + if (r < 0) { + r = -errno; + goto ERROR; + } + + // For now, assume all of these are I/O errors + r = -EIO; + +ERROR: + if (prefix) + free(prefix); + if (error) + free(error); + + return r; +} diff --git a/src/cli/lib/api.h b/src/cli/lib/api.h new file mode 100644 index 00000000..748157ca --- /dev/null +++ b/src/cli/lib/api.h @@ -0,0 +1,29 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_CLI_API_H +#define PAKFIRE_CLI_API_H + +#include + +int cli_api_error(const struct pakfire_xfer_response* response, const char* format, ...) + __attribute__((format(printf, 2, 3)));; + +#endif /* PAKFIRE_CLI_API_H */ diff --git a/src/cli/lib/client-build.c b/src/cli/lib/client-build.c index 37fc19df..a29390a1 100644 --- a/src/cli/lib/client-build.c +++ b/src/cli/lib/client-build.c @@ -24,6 +24,7 @@ #include #include +#include "api.h" #include "client-build.h" #include "command.h" #include "pakfire.h" @@ -101,33 +102,22 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { static int build_callback(struct pakfire_xfer* xfer, const pakfire_xfer_response* response, void* data) { const char* uuid = NULL; - char* error = NULL; int r; // Handle errors - if (response->error) { - r = pakfire_xfer_response_get_error(response, &error); - if (r < 0) - goto ERROR; - - fprintf(stderr, "Failed to create build: %s\n", error); - r = 1; - goto ERROR; - } + r = cli_api_error(response, "Failed to create build"); + if (r < 0) + return 0; // Fetch the UUID r = pakfire_json_get_string(response->data, "uuid", &uuid); if (r < 0) - goto ERROR; + return r; // Show status printf("Created build %s\n", uuid); -ERROR: - if (error) - free(error); - - return r; + return 0; } // Called when an upload was successful