Use recent GLib API unconditionally now that we depend on the latest GLib
[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. Errors can be received by
48  * listening to the #GstBus of the element/pipeline for #GstMessage objects with
49  * the type %GST_MESSAGE_ERROR or %GST_MESSAGE_WARNING. The thrown errors should
50  * be inspected, and filtered if appropriate.
51  *
52  * An application is expected to, by default, present the user with a
53  * dialog box (or an equivalent) showing the error message.  The dialog
54  * should also allow a way to get at the additional debug information,
55  * so the user can provide bug reporting information.
56  *
57  * A compound element is expected to forward errors by default higher up
58  * the hierarchy; this is done by default in the same way as for other types
59  * of #GstMessage.
60  *
61  * When applications or compound elements trigger errors that they can
62  * recover from, they can filter out these errors and take appropriate action.
63  * For example, an application that gets an error from xvimagesink
64  * that indicates all XVideo ports are taken, the application can attempt
65  * to use another sink instead.
66  *
67  * Elements throw errors using the #GST_ELEMENT_ERROR convenience macro:
68  *
69  * <example>
70  * <title>Throwing an error</title>
71  *   <programlisting>
72  *     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
73  *       (_("No file name specified for reading.")), (NULL));
74  *   </programlisting>
75  * </example>
76  *
77  * Things to keep in mind:
78  * <itemizedlist>
79  *   <listitem><para>Don't go off inventing new error codes.  The ones
80  *     currently provided should be enough.  If you find your type of error
81  *     does not fit the current codes, you should use FAILED.</para></listitem>
82  *   <listitem><para>Don't provide a message if the default one suffices.
83  *     this keeps messages more uniform.  Use (NULL) - not forgetting the
84  *     parentheses.</para></listitem>
85  *   <listitem><para>If you do supply a custom message, it should be
86  *     marked for translation.  The message should start with a capital
87  *     and end with a period.  The message should describe the error in short,
88  *     in a human-readable form, and without any complex technical terms.
89  *     A user interface will present this message as the first thing a user
90  *     sees.  Details, technical info, ... should go in the debug string.
91  *   </para></listitem>
92  *   <listitem><para>The debug string can be as you like.  Again, use (NULL)
93  *     if there's nothing to add - file and line number will still be
94  *     passed.  #GST_ERROR_SYSTEM can be used as a shortcut to give
95  *     debug information on a system call error.</para></listitem>
96  * </itemizedlist>
97  *
98  * Last reviewed on 2006-09-15 (0.10.10)
99  */
100
101 /* FIXME 0.11: the entire error system needs an overhaul - it's not very
102  * useful the way it is. Also, we need to be able to specify additional
103  * 'details' for errors (e.g. disk/file/resource error -> out-of-space; or
104  * put the url/filename/device name that caused the error somewhere)
105  * without having to add enums for every little thing.
106  *
107  * FIXME 0.11: get rid of GST_{CORE,LIBRARY,RESOURCE,STREAM}_ERROR_NUM_ERRORS.
108  * Maybe also replace _quark() functions with g_quark_from_static_string()?
109  */
110 #ifdef HAVE_CONFIG_H
111 #include "config.h"
112 #endif
113
114 #include "gst_private.h"
115 #include <gst/gst.h>
116 #include "gst-i18n-lib.h"
117
118 #define QUARK_FUNC(string)                                              \
119 GQuark gst_ ## string ## _error_quark (void) {                          \
120   static GQuark quark;                                                  \
121   if (!quark)                                                           \
122     quark = g_quark_from_static_string ("gst-" # string "-error-quark"); \
123   return quark; }
124
125 /* FIXME: Deprecate when we depend on GLib 2.26 */
126 /* FIXME 0.11: remove gst_g_error_get_type() */
127 GType
128 gst_g_error_get_type (void)
129 {
130   return g_error_get_type ();
131 }
132
133 #define FILE_A_BUG "  Please file a bug at " PACKAGE_BUGREPORT "."
134
135 static const gchar *
136 gst_error_get_core_error (GstCoreError code)
137 {
138   switch (code) {
139     case GST_CORE_ERROR_FAILED:
140       return _("GStreamer encountered a general core library error.");
141     case GST_CORE_ERROR_TOO_LAZY:
142       return _("GStreamer developers were too lazy to assign an error code "
143           "to this error." FILE_A_BUG);
144     case GST_CORE_ERROR_NOT_IMPLEMENTED:
145       return _("Internal GStreamer error: code not implemented." FILE_A_BUG);
146     case GST_CORE_ERROR_STATE_CHANGE:
147       return _("GStreamer error: state change failed and some element failed "
148           "to post a proper error message with the reason for the failure.");
149     case GST_CORE_ERROR_PAD:
150       return _("Internal GStreamer error: pad problem." FILE_A_BUG);
151     case GST_CORE_ERROR_THREAD:
152       return _("Internal GStreamer error: thread problem." FILE_A_BUG);
153     case GST_CORE_ERROR_NEGOTIATION:
154       return _("Internal GStreamer error: negotiation problem." FILE_A_BUG);
155     case GST_CORE_ERROR_EVENT:
156       return _("Internal GStreamer error: event problem." FILE_A_BUG);
157     case GST_CORE_ERROR_SEEK:
158       return _("Internal GStreamer error: seek problem." FILE_A_BUG);
159     case GST_CORE_ERROR_CAPS:
160       return _("Internal GStreamer error: caps problem." FILE_A_BUG);
161     case GST_CORE_ERROR_TAG:
162       return _("Internal GStreamer error: tag problem." FILE_A_BUG);
163     case GST_CORE_ERROR_MISSING_PLUGIN:
164       return _("Your GStreamer installation is missing a plug-in.");
165     case GST_CORE_ERROR_CLOCK:
166       return _("Internal GStreamer error: clock problem." FILE_A_BUG);
167     case GST_CORE_ERROR_DISABLED:
168       return _("This application is trying to use GStreamer functionality "
169           "that has been disabled.");
170     case GST_CORE_ERROR_NUM_ERRORS:
171     default:
172       break;
173   }
174   return NULL;
175 }
176
177 static const gchar *
178 gst_error_get_library_error (GstLibraryError code)
179 {
180   switch (code) {
181     case GST_LIBRARY_ERROR_FAILED:
182       return _("GStreamer encountered a general supporting library error.");
183     case GST_LIBRARY_ERROR_TOO_LAZY:
184       return _("GStreamer developers were too lazy to assign an error code "
185           "to this error." FILE_A_BUG);
186     case GST_LIBRARY_ERROR_INIT:
187       return _("Could not initialize supporting library.");
188     case GST_LIBRARY_ERROR_SHUTDOWN:
189       return _("Could not close supporting library.");
190     case GST_LIBRARY_ERROR_SETTINGS:
191       return _("Could not configure supporting library.");
192     case GST_LIBRARY_ERROR_ENCODE:
193       return _("Encoding error.");
194     case GST_LIBRARY_ERROR_NUM_ERRORS:
195     default:
196       break;
197   }
198   return NULL;
199 }
200
201 static const gchar *
202 gst_error_get_resource_error (GstResourceError code)
203 {
204   switch (code) {
205     case GST_RESOURCE_ERROR_FAILED:
206       return _("GStreamer encountered a general resource error.");
207     case GST_RESOURCE_ERROR_TOO_LAZY:
208       return _("GStreamer developers were too lazy to assign an error code "
209           "to this error." FILE_A_BUG);
210     case GST_RESOURCE_ERROR_NOT_FOUND:
211       return _("Resource not found.");
212     case GST_RESOURCE_ERROR_BUSY:
213       return _("Resource busy or not available.");
214     case GST_RESOURCE_ERROR_OPEN_READ:
215       return _("Could not open resource for reading.");
216     case GST_RESOURCE_ERROR_OPEN_WRITE:
217       return _("Could not open resource for writing.");
218     case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
219       return _("Could not open resource for reading and writing.");
220     case GST_RESOURCE_ERROR_CLOSE:
221       return _("Could not close resource.");
222     case GST_RESOURCE_ERROR_READ:
223       return _("Could not read from resource.");
224     case GST_RESOURCE_ERROR_WRITE:
225       return _("Could not write to resource.");
226     case GST_RESOURCE_ERROR_SEEK:
227       return _("Could not perform seek on resource.");
228     case GST_RESOURCE_ERROR_SYNC:
229       return _("Could not synchronize on resource.");
230     case GST_RESOURCE_ERROR_SETTINGS:
231       return _("Could not get/set settings from/on resource.");
232     case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
233       return _("No space left on the resource.");
234     case GST_RESOURCE_ERROR_NUM_ERRORS:
235     default:
236       break;
237   }
238   return NULL;
239 }
240
241 static const gchar *
242 gst_error_get_stream_error (GstStreamError code)
243 {
244   switch (code) {
245     case GST_STREAM_ERROR_FAILED:
246       return _("GStreamer encountered a general stream error.");
247     case GST_STREAM_ERROR_TOO_LAZY:
248       return _("GStreamer developers were too lazy to assign an error code "
249           "to this error." FILE_A_BUG);
250     case GST_STREAM_ERROR_NOT_IMPLEMENTED:
251       return _("Element doesn't implement handling of this stream. "
252           "Please file a bug.");
253     case GST_STREAM_ERROR_TYPE_NOT_FOUND:
254       return _("Could not determine type of stream.");
255     case GST_STREAM_ERROR_WRONG_TYPE:
256       return _("The stream is of a different type than handled by this "
257           "element.");
258     case GST_STREAM_ERROR_CODEC_NOT_FOUND:
259       return _("There is no codec present that can handle the stream's type.");
260     case GST_STREAM_ERROR_DECODE:
261       return _("Could not decode stream.");
262     case GST_STREAM_ERROR_ENCODE:
263       return _("Could not encode stream.");
264     case GST_STREAM_ERROR_DEMUX:
265       return _("Could not demultiplex stream.");
266     case GST_STREAM_ERROR_MUX:
267       return _("Could not multiplex stream.");
268     case GST_STREAM_ERROR_FORMAT:
269       return _("The stream is in the wrong format.");
270     case GST_STREAM_ERROR_DECRYPT:
271       return _("The stream is encrypted and decryption is not supported.");
272     case GST_STREAM_ERROR_DECRYPT_NOKEY:
273       return _("The stream is encrypted and can't be decrypted because no "
274           "suitable key has been supplied.");
275     case GST_STREAM_ERROR_NUM_ERRORS:
276     default:
277       break;
278   }
279
280   return NULL;
281 }
282
283 QUARK_FUNC (core);
284 QUARK_FUNC (library);
285 QUARK_FUNC (resource);
286 QUARK_FUNC (stream);
287
288 /**
289  * gst_error_get_message:
290  * @domain: the GStreamer error domain this error belongs to.
291  * @code: the error code belonging to the domain.
292  *
293  * Get a string describing the error message in the current locale.
294  *
295  * Returns: (transfer full): a newly allocated string describing
296  *     the error message (in UTF-8 encoding)
297  */
298 gchar *
299 gst_error_get_message (GQuark domain, gint code)
300 {
301   const gchar *message = NULL;
302
303   if (domain == GST_CORE_ERROR)
304     message = gst_error_get_core_error ((GstCoreError) code);
305   else if (domain == GST_LIBRARY_ERROR)
306     message = gst_error_get_library_error ((GstLibraryError) code);
307   else if (domain == GST_RESOURCE_ERROR)
308     message = gst_error_get_resource_error ((GstResourceError) code);
309   else if (domain == GST_STREAM_ERROR)
310     message = gst_error_get_stream_error ((GstStreamError) code);
311   else {
312     g_warning ("No error messages for domain %s", g_quark_to_string (domain));
313     return g_strdup_printf (_("No error message for domain %s."),
314         g_quark_to_string (domain));
315   }
316   if (message)
317     return g_strdup (message);
318   else
319     return
320         g_strdup_printf (_
321         ("No standard error message for domain %s and code %d."),
322         g_quark_to_string (domain), code);
323 }