gst/gsterror.*: Add GST_RESOURCE_ERROR_NO_SPACE_LEFT (for #333352;
[platform/upstream/gstreamer.git] / gst / gsterror.c
1 /* GStreamer
2  * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:gsterror
22  * @short_description: Categorized error messages
23  * @see_also: #GstMessage
24  *
25  * GStreamer elements can throw non-fatal warnings and fatal errors.
26  * Higher-level elements and applications can programatically filter
27  * the ones they are interested in or can recover from,
28  * and have a default handler handle the rest of them.
29  *
30  * The rest of this section will use the term <quote>error</quote>
31  * to mean both (non-fatal) warnings and (fatal) errors; they are treated
32  * similarly.
33  *
34  * Errors from elements are the combination of a #GError and a debug string.
35  * The #GError contains:
36  * - a domain type: CORE, LIBRARY, RESOURCE or STREAM
37  * - a code: an enum value specific to the domain
38  * - a translated, human-readable message
39  * - a non-translated additional debug string, which also contains
40  * - file and line information
41  *
42  * Elements do not have the context required to decide what to do with
43  * errors.  As such, they should only inform about errors, and stop their
44  * processing.  In short, an element doesn't know what it is being used for.
45  *
46  * It is the application or compound element using the given element that
47  * has more context about the use of the element.  The thrown errors should
48  * be inspected, and filtered if appropriate.
49  *
50  * An application is expected to, by default, present the user with a
51  * dialog box (or an equivalent) showing the error message.  The dialog
52  * should also allow a way to get at the additional debug information,
53  * so the user can provide bug reporting information.
54  *
55  * A compound element is expected to forward errors by default higher up
56  * the hierarchy; this is done by default in the same way as for other types
57  * of #GstMessage.
58  *
59  * When applications or compound elements trigger errors that they can
60  * recover from, they can filter out these errors and take appropriate action.
61  * For example, an application that gets an error from xvimagesink
62  * that indicates all XVideo ports are taken, the application can attempt
63  * to use another sink instead.
64  *
65  * Elements throw errors using the #GST_ELEMENT_ERROR convenience macro:
66  *
67  * <example>
68  * <title>Throwing an error</title>
69  *   <programlisting>
70  *     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
71  *       (_("No file name specified for reading.")), (NULL));
72  *   </programlisting>
73  * </example>
74  *
75  * Things to keep in mind:
76  * <itemizedlist>
77  *   <listitem><para>Don't go off inventing new error codes.  The ones
78  *     currently provided should be enough.  If you find your type of error
79  *     does not fit the current codes, you should use FAILED.</para></listitem>
80  *   <listitem><para>Don't provide a message if the default one suffices.
81  *     this keeps messages more uniform.  Use (NULL) - not forgetting the
82  *     parentheses.</para></listitem>
83  *   <listitem><para>If you do supply a custom message, it should be
84  *     marked for translation.  The message should start with a capital
85  *     and end with a period.</para></listitem>
86  *   <listitem><para>The debug string can be as you like.  Again, use (NULL)
87  *     if there's nothing to add - file and line number will still be
88  *     passed.  #GST_ERROR_SYSTEM can be used as a shortcut to give
89  *     debug information on a system call error.</para></listitem>
90  * </itemizedlist>
91  */
92
93 #ifdef HAVE_CONFIG_H
94 #include "config.h"
95 #endif
96
97 #include "gst_private.h"
98 #include <gst/gst.h>
99 #include "gst-i18n-lib.h"
100
101 #define TABLE(t, d, a, b) t[GST_ ## d ## _ERROR_ ## a] = g_strdup (b)
102 #define QUARK_FUNC(string)                                              \
103 GQuark gst_ ## string ## _error_quark (void) {                          \
104   static GQuark quark;                                                  \
105   if (!quark)                                                           \
106     quark = g_quark_from_static_string ("gst-" # string "-error-quark"); \
107   return quark; }
108
109 GType
110 gst_g_error_get_type (void)
111 {
112   static GType type = 0;
113
114   if (!type)
115     type = g_boxed_type_register_static ("GstGError",
116         (GBoxedCopyFunc) g_error_copy, (GBoxedFreeFunc) g_error_free);
117   return type;
118 }
119
120 #define FILE_A_BUG "  Please file a bug at "\
121     "http://bugzilla.gnome.org/enter_bug.cgi?product=GStreamer."
122
123 /* initialize the dynamic table of translated core errors */
124 static gchar **
125 _gst_core_errors_init (void)
126 {
127   gchar **t = NULL;
128
129   t = g_new0 (gchar *, GST_CORE_ERROR_NUM_ERRORS);
130
131   TABLE (t, CORE, FAILED,
132       N_("GStreamer encountered a general core library error."));
133   TABLE (t, CORE, TOO_LAZY,
134       N_("GStreamer developers were too lazy to assign an error code "
135           "to this error." FILE_A_BUG));
136   TABLE (t, CORE, NOT_IMPLEMENTED,
137       N_("Internal GStreamer error: code not implemented." FILE_A_BUG));
138   TABLE (t, CORE, STATE_CHANGE,
139       N_("Internal GStreamer error: state change failed." FILE_A_BUG));
140   TABLE (t, CORE, PAD, N_("Internal GStreamer error: pad problem." FILE_A_BUG));
141   TABLE (t, CORE, THREAD,
142       N_("Internal GStreamer error: thread problem." FILE_A_BUG));
143   TABLE (t, CORE, NEGOTIATION,
144       N_("Internal GStreamer error: negotiation problem." FILE_A_BUG));
145   TABLE (t, CORE, EVENT,
146       N_("Internal GStreamer error: event problem." FILE_A_BUG));
147   TABLE (t, CORE, SEEK,
148       N_("Internal GStreamer error: seek problem." FILE_A_BUG));
149   TABLE (t, CORE, CAPS,
150       N_("Internal GStreamer error: caps problem." FILE_A_BUG));
151   TABLE (t, CORE, TAG, N_("Internal GStreamer error: tag problem." FILE_A_BUG));
152   TABLE (t, CORE, MISSING_PLUGIN,
153       N_("Your GStreamer installation is missing a plug-in."));
154   TABLE (t, CORE, CLOCK,
155       N_("Internal GStreamer error: clock problem." FILE_A_BUG));
156
157   return t;
158 }
159
160 /* initialize the dynamic table of translated library errors */
161 static gchar **
162 _gst_library_errors_init (void)
163 {
164   gchar **t = NULL;
165
166   t = g_new0 (gchar *, GST_LIBRARY_ERROR_NUM_ERRORS);
167
168   TABLE (t, LIBRARY, FAILED,
169       N_("GStreamer encountered a general supporting library error."));
170   TABLE (t, LIBRARY, TOO_LAZY,
171       N_("GStreamer developers were too lazy to assign an error code "
172           "to this error." FILE_A_BUG));
173   TABLE (t, LIBRARY, INIT, N_("Could not initialize supporting library."));
174   TABLE (t, LIBRARY, SHUTDOWN, N_("Could not close supporting library."));
175   TABLE (t, LIBRARY, SETTINGS, N_("Could not close supporting library."));
176
177   return t;
178 }
179
180 /* initialize the dynamic table of translated resource errors */
181 static gchar **
182 _gst_resource_errors_init (void)
183 {
184   gchar **t = NULL;
185
186   t = g_new0 (gchar *, GST_RESOURCE_ERROR_NUM_ERRORS);
187
188   TABLE (t, RESOURCE, FAILED,
189       N_("GStreamer encountered a general resource error."));
190   TABLE (t, RESOURCE, TOO_LAZY,
191       N_("GStreamer developers were too lazy to assign an error code "
192           "to this error." FILE_A_BUG));
193   TABLE (t, RESOURCE, NOT_FOUND, N_("Resource not found."));
194   TABLE (t, RESOURCE, BUSY, N_("Resource busy or not available."));
195   TABLE (t, RESOURCE, OPEN_READ, N_("Could not open resource for reading."));
196   TABLE (t, RESOURCE, OPEN_WRITE, N_("Could not open resource for writing."));
197   TABLE (t, RESOURCE, OPEN_READ_WRITE,
198       N_("Could not open resource for reading and writing."));
199   TABLE (t, RESOURCE, CLOSE, N_("Could not close resource."));
200   TABLE (t, RESOURCE, READ, N_("Could not read from resource."));
201   TABLE (t, RESOURCE, WRITE, N_("Could not write to resource."));
202   TABLE (t, RESOURCE, SEEK, N_("Could not perform seek on resource."));
203   TABLE (t, RESOURCE, SYNC, N_("Could not synchronize on resource."));
204   TABLE (t, RESOURCE, SETTINGS,
205       N_("Could not get/set settings from/on resource."));
206   TABLE (t, RESOURCE, NO_SPACE_LEFT, N_("No space left on the resource."));
207
208   return t;
209 }
210
211 /* initialize the dynamic table of translated stream errors */
212 static gchar **
213 _gst_stream_errors_init (void)
214 {
215   gchar **t = NULL;
216
217   t = g_new0 (gchar *, GST_STREAM_ERROR_NUM_ERRORS);
218
219   TABLE (t, STREAM, FAILED,
220       N_("GStreamer encountered a general stream error."));
221   TABLE (t, STREAM, TOO_LAZY,
222       N_("GStreamer developers were too lazy to assign an error code "
223           "to this error." FILE_A_BUG));
224   TABLE (t, STREAM, NOT_IMPLEMENTED,
225       N_("Element doesn't implement handling of this stream. "
226           "Please file a bug."));
227   TABLE (t, STREAM, TYPE_NOT_FOUND, N_("Could not determine type of stream."));
228   TABLE (t, STREAM, WRONG_TYPE,
229       N_("The stream is of a different type than handled by this element."));
230   TABLE (t, STREAM, CODEC_NOT_FOUND,
231       N_("There is no codec present that can handle the stream's type."));
232   TABLE (t, STREAM, DECODE, N_("Could not decode stream."));
233   TABLE (t, STREAM, ENCODE, N_("Could not encode stream."));
234   TABLE (t, STREAM, DEMUX, N_("Could not demultiplex stream."));
235   TABLE (t, STREAM, MUX, N_("Could not multiplex stream."));
236
237   return t;
238 }
239
240 QUARK_FUNC (core);
241 QUARK_FUNC (library);
242 QUARK_FUNC (resource);
243 QUARK_FUNC (stream);
244
245 /**
246  * gst_error_get_message:
247  * @domain: the GStreamer error domain this error belongs to.
248  * @code: the error code belonging to the domain.
249  *
250  * Get a string describing the error message in the current locale.
251  *
252  * Returns: a newly allocated string describing the error message in the
253  * current locale.
254  */
255 gchar *
256 gst_error_get_message (GQuark domain, gint code)
257 {
258   static gchar **gst_core_errors = NULL;
259   static gchar **gst_library_errors = NULL;
260   static gchar **gst_resource_errors = NULL;
261   static gchar **gst_stream_errors = NULL;
262
263   gchar *message = NULL;
264
265   /* initialize error message tables if necessary */
266   if (gst_core_errors == NULL)
267     gst_core_errors = _gst_core_errors_init ();
268   if (gst_library_errors == NULL)
269     gst_library_errors = _gst_library_errors_init ();
270   if (gst_resource_errors == NULL)
271     gst_resource_errors = _gst_resource_errors_init ();
272   if (gst_stream_errors == NULL)
273     gst_stream_errors = _gst_stream_errors_init ();
274
275
276   if (domain == GST_CORE_ERROR)
277     message = gst_core_errors[code];
278   else if (domain == GST_LIBRARY_ERROR)
279     message = gst_library_errors[code];
280   else if (domain == GST_RESOURCE_ERROR)
281     message = gst_resource_errors[code];
282   else if (domain == GST_STREAM_ERROR)
283     message = gst_stream_errors[code];
284   else {
285     g_warning ("No error messages for domain %s", g_quark_to_string (domain));
286     return g_strdup_printf (_("No error message for domain %s."),
287         g_quark_to_string (domain));
288   }
289   if (message)
290     return g_strdup (_(message));
291   else
292     return
293         g_strdup_printf (_
294         ("No standard error message for domain %s and code %d."),
295         g_quark_to_string (domain), code);
296 }