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