From 4fd676e6f549151160005304ad825f48ebeb22fb Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 22 Jan 2024 14:35:48 +0000 Subject: [PATCH] [Minor] Add some convenience methods --- src/libutil/cxx/error.hxx | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/libutil/cxx/error.hxx b/src/libutil/cxx/error.hxx index 91f9d0cf4..4689d4276 100644 --- a/src/libutil/cxx/error.hxx +++ b/src/libutil/cxx/error.hxx @@ -1,11 +1,11 @@ -/*- - * Copyright 2022 Vsevolod Stakhov +/* + * Copyright 2024 Vsevolod Stakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -126,6 +126,27 @@ public: } } + /** + * Convert into GError + * @return + */ + auto into_g_error(GQuark quark) const -> GError * + { + return g_error_new(quark, error_code, "%s", + error_message.data()); + } + + /** + * Convenience alias for the `into_g_error` + * @param err + */ + auto into_g_error_set(GQuark quark, GError **err) const -> void + { + if (err && *err == nullptr) { + *err = into_g_error(quark); + } + } + public: std::string_view error_message; int error_code; -- 2.39.5