Add python3-setuptools dependency for python 3.12
[platform/core/system/tlm.git] / src / common / tlm-error.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of tlm (Tiny Login Manager)
5  *
6  * Copyright (C) 2014 Intel Corporation.
7  *
8  * Contact: Imran Zaman <imran.zaman@intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include <string.h>
27 #include <gio/gio.h>
28
29 #include "tlm-error.h"
30
31 /**
32  * SECTION:tlm-error
33  * @short_description: error definitions and utilities
34  * @title: Errors
35  *
36  * This file provides Tlm error definitions and utilities.
37  * When creating an error, use #TLM_ERROR for the error domain and errors
38  * from #TlmError for the error code.
39  *
40  * |[
41  *     GError* err = g_error_new(TLM_ERROR, TLM_ERROR_INVALID_INPUT,
42  *     "Invalid input");
43  * ]|
44  */
45
46 /**
47  * TLM_ERROR:
48  *
49  * This macro should be used when creating a #GError (for example with
50  * g_error_new()).
51  */
52
53 /**
54  * TlmError:
55  * @TLM_ERROR_NONE: No error
56  * @TLM_ERROR_UNKNOWN: Catch-all for errors not distinguished by another error
57  * code
58  * @TLM_ERROR_INTERNAL_SERVER: Server internal error
59  * @TLM_ERROR_PERMISSION_DENIED: Permission denied
60  * @TLM_ERROR_INVALID_INPUT: Invalid input
61  * @TLM_ERROR_SEAT_NOT_FOUND: Seat not found
62  * @TLM_ERROR_SESSION_CREATION_FAILURE: Session creation failed
63  * @TLM_ERROR_SESSION_ALREADY_EXISTS: Session already exists
64  * @TLM_ERROR_SESSION_NOT_VALID: session is not valid anymore
65  * @TLM_ERROR_SESSION_TERMINATION_FAILURE: Session termination failed
66  * @TLM_ERROR_DBUS_SERVER_START_FAILURE: dbus-server startup failed
67  * @TLM_ERROR_PAM_AUTH_FAILURE: PAM authentication failed
68  * @TLM_ERROR_DBUS_REQ_ABORTED: Dbus request aborted
69  * @TLM_ERROR_DBUS_REQ_NOT_SUPPORTED: Dbus request not supported
70  * @TLM_ERROR_DBUS_REQ_UNKNOWN: Dbus request failed with unknown error
71  * @TLM_ERROR_LAST_ERR: Placeholder to rearrange enumeration
72  *
73  * This enumeration provides a list of errors
74  */
75
76 /**
77  * TLM_ERROR_DOMAIN:
78  *
79  * This macro defines the error domain for tlm.
80  */
81 #define TLM_ERROR_DOMAIN "tlm"
82
83 /**
84  * TLM_GET_ERROR_FOR_ID:
85  * @code: A #TlmError specifying the error
86  * @message: Format string for the error message
87  * @...: parameters for the error string
88  *
89  * A helper macro that creates a #GError with the proper tlm domain
90  */
91
92 #define _ERROR_PREFIX "org.O1.Tlm.Error"
93
94 GDBusErrorEntry _tlm_errors[] =
95 {
96     {TLM_ERROR_UNKNOWN, _ERROR_PREFIX".Unknown"},
97     {TLM_ERROR_INTERNAL_SERVER, _ERROR_PREFIX".InternalServerError"},
98     {TLM_ERROR_PERMISSION_DENIED, _ERROR_PREFIX".PermissionDenied"},
99
100     {TLM_ERROR_INVALID_INPUT, _ERROR_PREFIX".InvalidInput"},
101     {TLM_ERROR_SEAT_NOT_FOUND, _ERROR_PREFIX".SeatNotFound"},
102     {TLM_ERROR_SESSION_CREATION_FAILURE,
103             _ERROR_PREFIX".SessionCreationFailure"},
104             {TLM_ERROR_SESSION_TERMINATION_FAILURE,
105                     _ERROR_PREFIX".SessionTerminationFailure"},
106     {TLM_ERROR_SESSION_ALREADY_EXISTS, _ERROR_PREFIX".SessionAlreadyExists"},
107     {TLM_ERROR_SESSION_NOT_VALID, _ERROR_PREFIX".SessionNotValid"},
108     {TLM_ERROR_DBUS_SERVER_START_FAILURE,
109             _ERROR_PREFIX".DBusServerStartFailure"},
110     {TLM_ERROR_PAM_AUTH_FAILURE,
111             _ERROR_PREFIX".PamAuthFailure"},
112     {TLM_ERROR_DBUS_REQ_ABORTED, _ERROR_PREFIX".DBusRequestAborted"},
113     {TLM_ERROR_DBUS_REQ_NOT_SUPPORTED, _ERROR_PREFIX".DBusRequestNotSupported"},
114     {TLM_ERROR_DBUS_REQ_UNKNOWN, _ERROR_PREFIX".DBusRequestUknown"},
115 } ;
116
117  /**
118   * tlm_error_quark:
119   *
120   * Creates and returns a domain for Tlm errors.
121   *
122   * Returns: #GQuark for Tlm errors
123   */
124 GQuark
125 tlm_error_quark (void)
126 {
127     static volatile gsize quark_volatile = 0;
128
129     g_dbus_error_register_error_domain (TLM_ERROR_DOMAIN,
130                                         &quark_volatile,
131                                         _tlm_errors,
132                                         G_N_ELEMENTS (_tlm_errors));
133
134     return (GQuark) quark_volatile;
135 }
136
137 /**
138  * tlm_error_new_from_variant:
139  * @var: (transfer none): instance of #GVariant
140  *
141  * Converts the GVariant to GError.
142  *
143  * Returns: (transfer full): #GError object if successful, NULL otherwise.
144  */
145 GError *
146 tlm_error_new_from_variant (
147         GVariant *var)
148 {
149     GError *error = NULL;
150     gchar *message;
151     GQuark domain;
152     gint code;
153
154     if (!var) {
155         return NULL;
156     }
157
158     g_variant_get (var, "(uis)", &domain, &code, &message);
159     error = g_error_new_literal (domain, code, message);
160     g_free (message);
161     return error;
162 }
163
164 /**
165  * tlm_error_to_variant:
166  * @error: (transfer none): instance of #GError
167  *
168  * Converts the GError to GVariant.
169  *
170  * Returns: (transfer full) #GVariant object if successful, NULL otherwise.
171  */
172 GVariant *
173 tlm_error_to_variant (
174         GError *error)
175 {
176     if (!error) {
177         return NULL;
178     }
179
180     return g_variant_new ("(uis)", error->domain, error->code, error->message);
181 }