Merge branch 'master' into 0.11
[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 GType
127 gst_g_error_get_type (void)
128 {
129 #if GLIB_CHECK_VERSION(2,25,2)
130   return g_error_get_type ();
131 #else
132   static GType type = 0;
133
134   if (G_UNLIKELY (type == 0))
135     type = g_boxed_type_register_static ("GstGError",
136         (GBoxedCopyFunc) g_error_copy, (GBoxedFreeFunc) g_error_free);
137   return type;
138 #endif
139 }
140
141 #define FILE_A_BUG "  Please file a bug at " PACKAGE_BUGREPORT "."
142
143 static const gchar *
144 gst_error_get_core_error (GstCoreError code)
145 {
146   switch (code) {
147     case GST_CORE_ERROR_FAILED:
148       return _("GStreamer encountered a general core library error.");
149     case GST_CORE_ERROR_TOO_LAZY:
150       return _("GStreamer developers were too lazy to assign an error code "
151           "to this error." FILE_A_BUG);
152     case GST_CORE_ERROR_NOT_IMPLEMENTED:
153       return _("Internal GStreamer error: code not implemented." FILE_A_BUG);
154     case GST_CORE_ERROR_STATE_CHANGE:
155       return _("GStreamer error: state change failed and some element failed "
156           "to post a proper error message with the reason for the failure.");
157     case GST_CORE_ERROR_PAD:
158       return _("Internal GStreamer error: pad problem." FILE_A_BUG);
159     case GST_CORE_ERROR_THREAD:
160       return _("Internal GStreamer error: thread problem." FILE_A_BUG);
161     case GST_CORE_ERROR_NEGOTIATION:
162       return _("Internal GStreamer error: negotiation problem." FILE_A_BUG);
163     case GST_CORE_ERROR_EVENT:
164       return _("Internal GStreamer error: event problem." FILE_A_BUG);
165     case GST_CORE_ERROR_SEEK:
166       return _("Internal GStreamer error: seek problem." FILE_A_BUG);
167     case GST_CORE_ERROR_CAPS:
168       return _("Internal GStreamer error: caps problem." FILE_A_BUG);
169     case GST_CORE_ERROR_TAG:
170       return _("Internal GStreamer error: tag problem." FILE_A_BUG);
171     case GST_CORE_ERROR_MISSING_PLUGIN:
172       return _("Your GStreamer installation is missing a plug-in.");
173     case GST_CORE_ERROR_CLOCK:
174       return _("Internal GStreamer error: clock problem." FILE_A_BUG);
175     case GST_CORE_ERROR_DISABLED:
176       return _("This application is trying to use GStreamer functionality "
177           "that has been disabled.");
178     case GST_CORE_ERROR_NUM_ERRORS:
179     default:
180       break;
181   }
182   return NULL;
183 }
184
185 static const gchar *
186 gst_error_get_library_error (GstLibraryError code)
187 {
188   switch (code) {
189     case GST_LIBRARY_ERROR_FAILED:
190       return _("GStreamer encountered a general supporting library error.");
191     case GST_LIBRARY_ERROR_TOO_LAZY:
192       return _("GStreamer developers were too lazy to assign an error code "
193           "to this error." FILE_A_BUG);
194     case GST_LIBRARY_ERROR_INIT:
195       return _("Could not initialize supporting library.");
196     case GST_LIBRARY_ERROR_SHUTDOWN:
197       return _("Could not close supporting library.");
198     case GST_LIBRARY_ERROR_SETTINGS:
199       return _("Could not configure supporting library.");
200     case GST_LIBRARY_ERROR_ENCODE:
201       return _("Encoding error.");
202     case GST_LIBRARY_ERROR_NUM_ERRORS:
203     default:
204       break;
205   }
206   return NULL;
207 }
208
209 static const gchar *
210 gst_error_get_resource_error (GstResourceError code)
211 {
212   switch (code) {
213     case GST_RESOURCE_ERROR_FAILED:
214       return _("GStreamer encountered a general resource error.");
215     case GST_RESOURCE_ERROR_TOO_LAZY:
216       return _("GStreamer developers were too lazy to assign an error code "
217           "to this error." FILE_A_BUG);
218     case GST_RESOURCE_ERROR_NOT_FOUND:
219       return _("Resource not found.");
220     case GST_RESOURCE_ERROR_BUSY:
221       return _("Resource busy or not available.");
222     case GST_RESOURCE_ERROR_OPEN_READ:
223       return _("Could not open resource for reading.");
224     case GST_RESOURCE_ERROR_OPEN_WRITE:
225       return _("Could not open resource for writing.");
226     case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
227       return _("Could not open resource for reading and writing.");
228     case GST_RESOURCE_ERROR_CLOSE:
229       return _("Could not close resource.");
230     case GST_RESOURCE_ERROR_READ:
231       return _("Could not read from resource.");
232     case GST_RESOURCE_ERROR_WRITE:
233       return _("Could not write to resource.");
234     case GST_RESOURCE_ERROR_SEEK:
235       return _("Could not perform seek on resource.");
236     case GST_RESOURCE_ERROR_SYNC:
237       return _("Could not synchronize on resource.");
238     case GST_RESOURCE_ERROR_SETTINGS:
239       return _("Could not get/set settings from/on resource.");
240     case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
241       return _("No space left on the resource.");
242     case GST_RESOURCE_ERROR_NUM_ERRORS:
243     default:
244       break;
245   }
246   return NULL;
247 }
248
249 static const gchar *
250 gst_error_get_stream_error (GstStreamError code)
251 {
252   switch (code) {
253     case GST_STREAM_ERROR_FAILED:
254       return _("GStreamer encountered a general stream error.");
255     case GST_STREAM_ERROR_TOO_LAZY:
256       return _("GStreamer developers were too lazy to assign an error code "
257           "to this error." FILE_A_BUG);
258     case GST_STREAM_ERROR_NOT_IMPLEMENTED:
259       return _("Element doesn't implement handling of this stream. "
260           "Please file a bug.");
261     case GST_STREAM_ERROR_TYPE_NOT_FOUND:
262       return _("Could not determine type of stream.");
263     case GST_STREAM_ERROR_WRONG_TYPE:
264       return _("The stream is of a different type than handled by this "
265           "element.");
266     case GST_STREAM_ERROR_CODEC_NOT_FOUND:
267       return _("There is no codec present that can handle the stream's type.");
268     case GST_STREAM_ERROR_DECODE:
269       return _("Could not decode stream.");
270     case GST_STREAM_ERROR_ENCODE:
271       return _("Could not encode stream.");
272     case GST_STREAM_ERROR_DEMUX:
273       return _("Could not demultiplex stream.");
274     case GST_STREAM_ERROR_MUX:
275       return _("Could not multiplex stream.");
276     case GST_STREAM_ERROR_FORMAT:
277       return _("The stream is in the wrong format.");
278     case GST_STREAM_ERROR_DECRYPT:
279       return _("The stream is encrypted and decryption is not supported.");
280     case GST_STREAM_ERROR_DECRYPT_NOKEY:
281       return _("The stream is encrypted and can't be decrypted because no "
282           "suitable key has been supplied.");
283     case GST_STREAM_ERROR_NUM_ERRORS:
284     default:
285       break;
286   }
287
288   return NULL;
289 }
290
291 QUARK_FUNC (core);
292 QUARK_FUNC (library);
293 QUARK_FUNC (resource);
294 QUARK_FUNC (stream);
295
296 /**
297  * gst_error_get_message:
298  * @domain: the GStreamer error domain this error belongs to.
299  * @code: the error code belonging to the domain.
300  *
301  * Get a string describing the error message in the current locale.
302  *
303  * Returns: (transfer full): a newly allocated string describing
304  *     the error message (in UTF-8 encoding)
305  */
306 gchar *
307 gst_error_get_message (GQuark domain, gint code)
308 {
309   const gchar *message = NULL;
310
311   if (domain == GST_CORE_ERROR)
312     message = gst_error_get_core_error ((GstCoreError) code);
313   else if (domain == GST_LIBRARY_ERROR)
314     message = gst_error_get_library_error ((GstLibraryError) code);
315   else if (domain == GST_RESOURCE_ERROR)
316     message = gst_error_get_resource_error ((GstResourceError) code);
317   else if (domain == GST_STREAM_ERROR)
318     message = gst_error_get_stream_error ((GstStreamError) code);
319   else {
320     g_warning ("No error messages for domain %s", g_quark_to_string (domain));
321     return g_strdup_printf (_("No error message for domain %s."),
322         g_quark_to_string (domain));
323   }
324   if (message)
325     return g_strdup (message);
326   else
327     return
328         g_strdup_printf (_
329         ("No standard error message for domain %s and code %d."),
330         g_quark_to_string (domain), code);
331 }