PageRenderTime 271ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/3rd-party/glib-2.16.6/docs/reference/glib/xml/error_reporting.xml

https://bitbucket.org/super119/plu2youku
XML | 688 lines | 614 code | 73 blank | 1 comment | 0 complexity | cabef492a488dc37213f6bfd9da72a4f MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-3.0
  1. <refentry id="glib-Error-Reporting">
  2. <refmeta>
  3. <refentrytitle role="top_of_page" id="glib-Error-Reporting.top_of_page">Error Reporting</refentrytitle>
  4. <manvolnum>3</manvolnum>
  5. <refmiscinfo>GLIB Library</refmiscinfo>
  6. </refmeta>
  7. <refnamediv>
  8. <refname>Error Reporting</refname>
  9. <refpurpose>a system for reporting errors</refpurpose>
  10. <!--[<xref linkend="desc" endterm="desc.title"/>]-->
  11. </refnamediv>
  12. <refsynopsisdiv id="glib-Error-Reporting.synopsis" role="synopsis">
  13. <title role="synopsis.title">Synopsis</title>
  14. <synopsis>
  15. #include &lt;glib.h&gt;
  16. <link linkend="GError">GError</link>;
  17. <link linkend="GError">GError</link>* <link linkend="g-error-new">g_error_new</link> (<link linkend="GQuark">GQuark</link> domain,
  18. <link linkend="gint">gint</link> code,
  19. const <link linkend="gchar">gchar</link> *format,
  20. ...);
  21. <link linkend="GError">GError</link>* <link linkend="g-error-new-literal">g_error_new_literal</link> (<link linkend="GQuark">GQuark</link> domain,
  22. <link linkend="gint">gint</link> code,
  23. const <link linkend="gchar">gchar</link> *message);
  24. <link linkend="void">void</link> <link linkend="g-error-free">g_error_free</link> (<link linkend="GError">GError</link> *error);
  25. <link linkend="GError">GError</link>* <link linkend="g-error-copy">g_error_copy</link> (const <link linkend="GError">GError</link> *error);
  26. <link linkend="gboolean">gboolean</link> <link linkend="g-error-matches">g_error_matches</link> (const <link linkend="GError">GError</link> *error,
  27. <link linkend="GQuark">GQuark</link> domain,
  28. <link linkend="gint">gint</link> code);
  29. <link linkend="void">void</link> <link linkend="g-set-error">g_set_error</link> (<link linkend="GError">GError</link> **err,
  30. <link linkend="GQuark">GQuark</link> domain,
  31. <link linkend="gint">gint</link> code,
  32. const <link linkend="gchar">gchar</link> *format,
  33. ...);
  34. <link linkend="void">void</link> <link linkend="g-propagate-error">g_propagate_error</link> (<link linkend="GError">GError</link> **dest,
  35. <link linkend="GError">GError</link> *src);
  36. <link linkend="void">void</link> <link linkend="g-clear-error">g_clear_error</link> (<link linkend="GError">GError</link> **err);
  37. <link linkend="void">void</link> <link linkend="g-prefix-error">g_prefix_error</link> (<link linkend="GError">GError</link> **err,
  38. const <link linkend="gchar">gchar</link> *format,
  39. ...);
  40. <link linkend="void">void</link> <link linkend="g-propagate-prefixed-error">g_propagate_prefixed_error</link> (<link linkend="GError">GError</link> **dest,
  41. <link linkend="GError">GError</link> *src,
  42. const <link linkend="gchar">gchar</link> *format,
  43. ...);
  44. </synopsis>
  45. </refsynopsisdiv>
  46. <refsect1 id="glib-Error-Reporting.description" role="desc">
  47. <title role="desc.title">Description</title>
  48. <para>
  49. GLib provides a standard method of reporting errors from a called function to
  50. the calling code. (This is the same problem solved by exceptions in other
  51. languages.) It's important to understand that this method is both a
  52. <emphasis>data type</emphasis> (the <link linkend="GError"><type>GError</type></link> object) and a <emphasis>set of
  53. rules.</emphasis> If you use <link linkend="GError"><type>GError</type></link> incorrectly, then your code will not
  54. properly interoperate with other code that uses <link linkend="GError"><type>GError</type></link>, and users of your API
  55. will probably get confused.
  56. </para>
  57. <para>
  58. First and foremost: <emphasis><link linkend="GError"><type>GError</type></link> should only be used to report
  59. recoverable runtime errors, never to report programming errors.</emphasis> If
  60. the programmer has screwed up, then you should use <link linkend="g-warning"><function>g_warning()</function></link>,
  61. <link linkend="g-return-if-fail"><function>g_return_if_fail()</function></link>, <link linkend="g-assert"><function>g_assert()</function></link>, <link linkend="g-error"><function>g_error()</function></link>, or some similar facility.
  62. (Incidentally, remember that the <link linkend="g-error"><function>g_error()</function></link> function should
  63. <emphasis>only</emphasis> be used for programming errors, it should not be used
  64. to print any error reportable via <link linkend="GError"><type>GError</type></link>.)
  65. </para>
  66. <para>
  67. Examples of recoverable runtime errors are "file not found" or "failed to parse
  68. input." Examples of programming errors are "NULL passed to <link linkend="strcmp"><function>strcmp()</function></link>" or
  69. "attempted to free the same pointer twice." These two kinds of errors are
  70. fundamentally different: runtime errors should be handled or reported to the
  71. user, programming errors should be eliminated by fixing the bug in the program.
  72. This is why most functions in GLib and GTK+ do not use the <link linkend="GError"><type>GError</type></link> facility.
  73. </para>
  74. <para>
  75. Functions that can fail take a return location for a <link linkend="GError"><type>GError</type></link> as their last argument.
  76. For example:
  77. <informalexample><programlisting>
  78. gboolean g_file_get_contents (const gchar *filename,
  79. gchar **contents,
  80. gsize *length,
  81. GError **error);
  82. </programlisting></informalexample>
  83. If you pass a non-<link linkend="NULL:CAPS"><literal>NULL</literal></link> value for the <literal>error</literal> argument, it should
  84. point to a location where an error can be placed. For example:
  85. <informalexample><programlisting>
  86. gchar *contents;
  87. GError *err = NULL;
  88. g_file_get_contents ("foo.txt", &amp;contents, NULL, &amp;err);
  89. g_assert ((contents == NULL &amp;&amp; err != NULL) || (contents != NULL &amp;&amp; err == NULL));
  90. if (err != NULL)
  91. {
  92. /* Report error to user, and free error */
  93. g_assert (contents == NULL);
  94. fprintf (stderr, "Unable to read file: &percnt;s\n", err->message);
  95. g_error_free (err);
  96. }
  97. else
  98. {
  99. /* Use file contents */
  100. g_assert (contents != NULL);
  101. }
  102. </programlisting></informalexample>
  103. Note that <literal>err != NULL</literal> in this example is a
  104. <emphasis>reliable</emphasis> indicator of whether
  105. <link linkend="g-file-get-contents"><function>g_file_get_contents()</function></link> failed. Additionally, <link linkend="g-file-get-contents"><function>g_file_get_contents()</function></link> returns
  106. a boolean which indicates whether it was successful.
  107. </para>
  108. <para>
  109. Because <link linkend="g-file-get-contents"><function>g_file_get_contents()</function></link> returns <link linkend="FALSE:CAPS"><literal>FALSE</literal></link> on failure, if you are only
  110. interested in whether it failed and don't need to display an error message, you
  111. can pass <link linkend="NULL:CAPS"><literal>NULL</literal></link> for the <literal>error</literal> argument:
  112. <informalexample><programlisting>
  113. if (g_file_get_contents ("foo.txt", &amp;contents, NULL, NULL)) /* ignore errors */
  114. /* no error occurred */ ;
  115. else
  116. /* error */ ;
  117. </programlisting></informalexample>
  118. </para>
  119. <para>
  120. The <link linkend="GError"><type>GError</type></link> object contains three fields: <literal>domain</literal> indicates
  121. the module the error-reporting function is located in, <literal>code</literal>
  122. indicates the specific error that occurred, and <literal>message</literal> is a
  123. user-readable error message with as many details as possible. Several functions
  124. are provided to deal with an error received from a called function:
  125. <link linkend="g-error-matches"><function>g_error_matches()</function></link> returns <link linkend="TRUE:CAPS"><literal>TRUE</literal></link> if the error matches a given domain and code,
  126. <link linkend="g-propagate-error"><function>g_propagate_error()</function></link> copies an error into an error location (so the calling
  127. function will receive it), and <link linkend="g-clear-error"><function>g_clear_error()</function></link> clears an error location by
  128. freeing the error and resetting the location to <link linkend="NULL:CAPS"><literal>NULL</literal></link>. To display an error to the
  129. user, simply display <literal>error-&gt;message</literal>, perhaps along with
  130. additional context known only to the calling function (the file being opened, or
  131. whatever -- though in the <link linkend="g-file-get-contents"><function>g_file_get_contents()</function></link> case,
  132. <literal>error-&gt;message</literal> already contains a filename).
  133. </para>
  134. <para>
  135. When implementing a function that can report errors, the basic tool is
  136. <link linkend="g-set-error"><function>g_set_error()</function></link>. Typically, if a fatal error occurs you want to <link linkend="g-set-error"><function>g_set_error()</function></link>,
  137. then return immediately. <link linkend="g-set-error"><function>g_set_error()</function></link> does nothing if the error location passed
  138. to it is <link linkend="NULL:CAPS"><literal>NULL</literal></link>. Here's an example:
  139. <informalexample><programlisting>
  140. gint
  141. foo_open_file (GError **error)
  142. {
  143. gint fd;
  144. fd = open ("file.txt", O_RDONLY);
  145. if (fd &lt; 0)
  146. {
  147. g_set_error (error,
  148. FOO_ERROR, /* error domain */
  149. FOO_ERROR_BLAH, /* error code */
  150. "Failed to open file: &percnt;s", /* error message format string */
  151. g_strerror (errno));
  152. return -1;
  153. }
  154. else
  155. return fd;
  156. }
  157. </programlisting></informalexample>
  158. </para>
  159. <para>
  160. Things are somewhat more complicated if you yourself call another function that
  161. can report a <link linkend="GError"><type>GError</type></link>. If the sub-function indicates fatal errors in some way
  162. other than reporting a <link linkend="GError"><type>GError</type></link>, such as by returning <link linkend="TRUE:CAPS"><literal>TRUE</literal></link> on success, you can
  163. simply do the following:
  164. <informalexample><programlisting>
  165. gboolean
  166. my_function_that_can_fail (GError **err)
  167. {
  168. g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
  169. if (!sub_function_that_can_fail (err))
  170. {
  171. /* assert that error was set by the sub-function */
  172. g_assert (err == NULL || *err != NULL);
  173. return FALSE;
  174. }
  175. /* otherwise continue, no error occurred */
  176. g_assert (err == NULL || *err == NULL);
  177. }
  178. </programlisting></informalexample>
  179. </para>
  180. <para>
  181. If the sub-function does not indicate errors other than by reporting a <link linkend="GError"><type>GError</type></link>,
  182. you need to create a temporary <link linkend="GError"><type>GError</type></link> since the passed-in one may be <link linkend="NULL:CAPS"><literal>NULL</literal></link>.
  183. <link linkend="g-propagate-error"><function>g_propagate_error()</function></link> is intended for use in this case.
  184. <informalexample><programlisting>
  185. gboolean
  186. my_function_that_can_fail (GError **err)
  187. {
  188. GError *tmp_error;
  189. g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
  190. tmp_error = NULL;
  191. sub_function_that_can_fail (&amp;tmp_error);
  192. if (tmp_error != NULL)
  193. {
  194. /* store tmp_error in err, if err != NULL,
  195. * otherwise call g_error_free(<!-- -->) on tmp_error
  196. */
  197. g_propagate_error (err, tmp_error);
  198. return FALSE;
  199. }
  200. /* otherwise continue, no error occurred */
  201. }
  202. </programlisting></informalexample>
  203. </para>
  204. <para>
  205. Error pileups are always a bug. For example, this code is incorrect:
  206. <informalexample><programlisting>
  207. gboolean
  208. my_function_that_can_fail (GError **err)
  209. {
  210. GError *tmp_error;
  211. g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
  212. tmp_error = NULL;
  213. sub_function_that_can_fail (&amp;tmp_error);
  214. other_function_that_can_fail (&amp;tmp_error);
  215. if (tmp_error != NULL)
  216. {
  217. g_propagate_error (err, tmp_error);
  218. return FALSE;
  219. }
  220. }
  221. </programlisting></informalexample>
  222. <literal>tmp_error</literal> should be checked immediately after
  223. <function><link linkend="sub-function-that-can-fail"><function>sub_function_that_can_fail()</function></link></function>, and either cleared or propagated upward. The rule
  224. is: <emphasis>after each error, you must either handle the error, or return it to the
  225. calling function</emphasis>. Note that passing <link linkend="NULL:CAPS"><literal>NULL</literal></link> for the error location is the
  226. equivalent of handling an error by always doing nothing about it. So the
  227. following code is fine, assuming errors in <function><link linkend="sub-function-that-can-fail"><function>sub_function_that_can_fail()</function></link></function> are not
  228. fatal to <function><link linkend="my-function-that-can-fail"><function>my_function_that_can_fail()</function></link></function>:
  229. <informalexample><programlisting>
  230. gboolean
  231. my_function_that_can_fail (GError **err)
  232. {
  233. GError *tmp_error;
  234. g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
  235. sub_function_that_can_fail (NULL); /* ignore errors */
  236. tmp_error = NULL;
  237. other_function_that_can_fail (&amp;tmp_error);
  238. if (tmp_error != NULL)
  239. {
  240. g_propagate_error (err, tmp_error);
  241. return FALSE;
  242. }
  243. }
  244. </programlisting></informalexample>
  245. </para>
  246. <para>
  247. Note that passing <link linkend="NULL:CAPS"><literal>NULL</literal></link> for the error location <emphasis>ignores</emphasis>
  248. errors; it's equivalent to <literal>try { <link linkend="sub-function-that-can-fail"><function>sub_function_that_can_fail()</function></link>; } catch
  249. (...) {}</literal> in C++. It does <emphasis>not</emphasis> mean to leave errors
  250. unhandled; it means to handle them by doing nothing.
  251. </para>
  252. <para>
  253. Error domains and codes are conventionally named as follows:
  254. <itemizedlist>
  255. <listitem>
  256. <para>
  257. The error domain is called
  258. <literal>&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR</literal>, for example
  259. <link linkend="G-SPAWN-ERROR:CAPS"><literal>G_SPAWN_ERROR</literal></link> or <link linkend="G-THREAD-ERROR:CAPS"><literal>G_THREAD_ERROR</literal></link>:
  260. <informalexample><programlisting>
  261. #define G_SPAWN_ERROR g_spawn_error_quark (<!-- -->)
  262. GQuark
  263. g_spawn_error_quark (void)
  264. {
  265. return g_quark_from_static_string ("g-spawn-error-quark");
  266. }
  267. </programlisting></informalexample>
  268. </para>
  269. </listitem>
  270. <listitem>
  271. <para>
  272. The error codes are in an enumeration called
  273. <literal>&lt;Namespace&gt;&lt;Module&gt;Error</literal>; for example,
  274. <link linkend="GThreadError"><type>GThreadError</type></link> or <link linkend="GSpawnError"><type>GSpawnError</type></link>.
  275. </para>
  276. </listitem>
  277. <listitem>
  278. <para>
  279. Members of the error code enumeration are called <literal>&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_&lt;CODE&gt;</literal>, for example <link linkend="G-SPAWN-ERROR-FORK:CAPS"><literal>G_SPAWN_ERROR_FORK</literal></link> or <link linkend="G-THREAD-ERROR-AGAIN:CAPS"><literal>G_THREAD_ERROR_AGAIN</literal></link>.
  280. </para>
  281. </listitem>
  282. <listitem>
  283. <para>
  284. If there's a "generic" or "unknown" error code for unrecoverable errors it
  285. doesn't make sense to distinguish with specific codes, it should be called
  286. <literal>&lt;NAMESPACE&gt;_&lt;MODULE&gt;_ERROR_FAILED</literal>, for
  287. example <link linkend="G-SPAWN-ERROR-FAILED:CAPS"><literal>G_SPAWN_ERROR_FAILED</literal></link> or <link linkend="G-THREAD-ERROR-FAILED:CAPS"><literal>G_THREAD_ERROR_FAILED</literal></link>.
  288. </para>
  289. </listitem>
  290. </itemizedlist>
  291. </para>
  292. <para>
  293. Summary of rules for use of <link linkend="GError--"><type>""</type></link>
  294. <itemizedlist>
  295. <listitem>
  296. <para>
  297. Do not report programming errors via <link linkend="GError"><type>GError</type></link>.
  298. </para>
  299. </listitem>
  300. <listitem>
  301. <para>
  302. The last argument of a function that returns an error should be a
  303. location where a <link linkend="GError"><type>GError</type></link> can be placed (i.e. "<link linkend="GError"><type>GError</type></link>** error"). If
  304. <link linkend="GError"><type>GError</type></link> is used with varargs, the <link linkend="GError"><type>GError</type></link>** should be the last
  305. argument before the "...".
  306. </para>
  307. </listitem>
  308. <listitem>
  309. <para>
  310. The caller may pass <link linkend="NULL:CAPS"><literal>NULL</literal></link> for the <link linkend="GError"><type>GError</type></link>** if they are not interested
  311. in details of the exact error that occurred.
  312. </para>
  313. </listitem>
  314. <listitem>
  315. <para>
  316. If <link linkend="NULL:CAPS"><literal>NULL</literal></link> is passed for the <link linkend="GError"><type>GError</type></link>** argument, then errors should
  317. not be returned to the caller, but your function should still
  318. abort and return if an error occurs. That is, control flow should
  319. not be affected by whether the caller wants to get a <link linkend="GError"><type>GError</type></link>.
  320. </para>
  321. </listitem>
  322. <listitem>
  323. <para>
  324. If a <link linkend="GError"><type>GError</type></link> is reported, then your function by definition
  325. <emphasis>had a fatal failure and did not complete whatever it was supposed
  326. to do</emphasis>. If the failure was not fatal, then you handled it
  327. and you should not report it. If it was fatal, then you must report it
  328. and discontinue whatever you were doing immediately.
  329. </para>
  330. </listitem>
  331. <listitem>
  332. <para>
  333. A <link linkend="GError"><type>GError</type></link>* must be initialized to <link linkend="NULL:CAPS"><literal>NULL</literal></link> before passing its address to
  334. a function that can report errors.
  335. </para>
  336. </listitem>
  337. <listitem>
  338. <para>
  339. "Piling up" errors is always a bug. That is, if you assign a new
  340. <link linkend="GError"><type>GError</type></link> to a <link linkend="GError"><type>GError</type></link>* that is non-<link linkend="NULL:CAPS"><literal>NULL</literal></link>, thus overwriting the previous
  341. error, it indicates that you should have aborted the operation instead
  342. of continuing. If you were able to continue, you should have cleared
  343. the previous error with <link linkend="g-clear-error"><function>g_clear_error()</function></link>. <link linkend="g-set-error"><function>g_set_error()</function></link> will complain
  344. if you pile up errors.
  345. </para>
  346. </listitem>
  347. <listitem>
  348. <para>
  349. By convention, if you return a boolean value indicating success
  350. then <link linkend="TRUE:CAPS"><literal>TRUE</literal></link> means success and <link linkend="FALSE:CAPS"><literal>FALSE</literal></link> means failure. If <link linkend="FALSE:CAPS"><literal>FALSE</literal></link> is returned,
  351. the error <emphasis>must</emphasis> be set to a non-<link linkend="NULL:CAPS"><literal>NULL</literal></link> value.
  352. </para>
  353. </listitem>
  354. <listitem>
  355. <para>
  356. A <link linkend="NULL:CAPS"><literal>NULL</literal></link> return value is also frequently used to mean that an error
  357. occurred. You should make clear in your documentation whether <link linkend="NULL:CAPS"><literal>NULL</literal></link> is
  358. a valid return value in non-error cases; if <link linkend="NULL:CAPS"><literal>NULL</literal></link> is a valid value,
  359. then users must check whether an error was returned to see if the
  360. function succeeded.
  361. </para>
  362. </listitem>
  363. <listitem>
  364. <para>
  365. When implementing a function that can report errors, you may want to
  366. add a check at the top of your function that the error return location
  367. is either <link linkend="NULL:CAPS"><literal>NULL</literal></link> or contains a <link linkend="NULL:CAPS"><literal>NULL</literal></link> error
  368. (e.g. <literal>g_return_if_fail (error == NULL || *error ==
  369. NULL);</literal>).
  370. </para>
  371. </listitem>
  372. </itemizedlist>
  373. </para>
  374. </refsect1>
  375. <refsect1 id="glib-Error-Reporting.details" role="details">
  376. <title role="details.title">Details</title>
  377. <refsect2 id="GError" role="struct">
  378. <title>GError</title>
  379. <indexterm zone="GError"><primary>GError</primary></indexterm><programlisting>typedef struct {
  380. GQuark domain;
  381. gint code;
  382. gchar *message;
  383. } GError;
  384. </programlisting>
  385. <para>
  386. The <structname>GError</structname> structure contains
  387. information about an error that has occurred.
  388. </para><variablelist role="struct">
  389. <varlistentry>
  390. <term><link linkend="GQuark">GQuark</link>&nbsp;<structfield>domain</structfield>;</term>
  391. <listitem><simpara>error domain, e.g. <link linkend="G-FILE-ERROR:CAPS"><type>G_FILE_ERROR</type></link>.
  392. </simpara></listitem>
  393. </varlistentry>
  394. <varlistentry>
  395. <term><link linkend="gint">gint</link>&nbsp;<structfield>code</structfield>;</term>
  396. <listitem><simpara>error code, e.g. <link linkend="G-FILE-ERROR-NOENT:CAPS"><literal>G_FILE_ERROR_NOENT</literal></link>.
  397. </simpara></listitem>
  398. </varlistentry>
  399. <varlistentry>
  400. <term><link linkend="gchar">gchar</link>&nbsp;*<structfield>message</structfield>;</term>
  401. <listitem><simpara>human-readable informative error message.
  402. </simpara></listitem>
  403. </varlistentry>
  404. </variablelist></refsect2>
  405. <refsect2 id="g-error-new" role="function">
  406. <title>g_error_new ()</title>
  407. <indexterm zone="g-error-new"><primary>g_error_new</primary></indexterm><programlisting><link linkend="GError">GError</link>* g_error_new (<link linkend="GQuark">GQuark</link> domain,
  408. <link linkend="gint">gint</link> code,
  409. const <link linkend="gchar">gchar</link> *format,
  410. ...);</programlisting>
  411. <para>
  412. Creates a new <link linkend="GError"><type>GError</type></link> with the given <parameter>domain</parameter> and <parameter>code</parameter>,
  413. and a message formatted with <parameter>format</parameter>.</para>
  414. <para>
  415. </para><variablelist role="params">
  416. <varlistentry><term><parameter>domain</parameter>&nbsp;:</term>
  417. <listitem><simpara> error domain
  418. </simpara></listitem></varlistentry>
  419. <varlistentry><term><parameter>code</parameter>&nbsp;:</term>
  420. <listitem><simpara> error code
  421. </simpara></listitem></varlistentry>
  422. <varlistentry><term><parameter>format</parameter>&nbsp;:</term>
  423. <listitem><simpara> <link linkend="printf"><function>printf()</function></link>-style format for error message
  424. </simpara></listitem></varlistentry>
  425. <varlistentry><term><parameter>...</parameter>&nbsp;:</term>
  426. <listitem><simpara> parameters for message format
  427. </simpara></listitem></varlistentry>
  428. <varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> a new <link linkend="GError"><type>GError</type></link>
  429. </simpara></listitem></varlistentry>
  430. </variablelist></refsect2>
  431. <refsect2 id="g-error-new-literal" role="function">
  432. <title>g_error_new_literal ()</title>
  433. <indexterm zone="g-error-new-literal"><primary>g_error_new_literal</primary></indexterm><programlisting><link linkend="GError">GError</link>* g_error_new_literal (<link linkend="GQuark">GQuark</link> domain,
  434. <link linkend="gint">gint</link> code,
  435. const <link linkend="gchar">gchar</link> *message);</programlisting>
  436. <para>
  437. Creates a new <link linkend="GError"><type>GError</type></link>; unlike <link linkend="g-error-new"><function>g_error_new()</function></link>, <parameter>message</parameter> is not
  438. a <link linkend="printf"><function>printf()</function></link>-style format string. Use this
  439. function if <parameter>message</parameter> contains text you don't have control over,
  440. that could include <link linkend="printf"><function>printf()</function></link> escape sequences.</para>
  441. <para>
  442. </para><variablelist role="params">
  443. <varlistentry><term><parameter>domain</parameter>&nbsp;:</term>
  444. <listitem><simpara> error domain
  445. </simpara></listitem></varlistentry>
  446. <varlistentry><term><parameter>code</parameter>&nbsp;:</term>
  447. <listitem><simpara> error code
  448. </simpara></listitem></varlistentry>
  449. <varlistentry><term><parameter>message</parameter>&nbsp;:</term>
  450. <listitem><simpara> error message
  451. </simpara></listitem></varlistentry>
  452. <varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> a new <link linkend="GError"><type>GError</type></link>
  453. </simpara></listitem></varlistentry>
  454. </variablelist></refsect2>
  455. <refsect2 id="g-error-free" role="function">
  456. <title>g_error_free ()</title>
  457. <indexterm zone="g-error-free"><primary>g_error_free</primary></indexterm><programlisting><link linkend="void">void</link> g_error_free (<link linkend="GError">GError</link> *error);</programlisting>
  458. <para>
  459. Frees a <link linkend="GError"><type>GError</type></link> and associated resources.</para>
  460. <para>
  461. </para><variablelist role="params">
  462. <varlistentry><term><parameter>error</parameter>&nbsp;:</term>
  463. <listitem><simpara> a <link linkend="GError"><type>GError</type></link>
  464. </simpara></listitem></varlistentry>
  465. </variablelist></refsect2>
  466. <refsect2 id="g-error-copy" role="function">
  467. <title>g_error_copy ()</title>
  468. <indexterm zone="g-error-copy"><primary>g_error_copy</primary></indexterm><programlisting><link linkend="GError">GError</link>* g_error_copy (const <link linkend="GError">GError</link> *error);</programlisting>
  469. <para>
  470. Makes a copy of <parameter>error</parameter>.</para>
  471. <para>
  472. </para><variablelist role="params">
  473. <varlistentry><term><parameter>error</parameter>&nbsp;:</term>
  474. <listitem><simpara> a <link linkend="GError"><type>GError</type></link>
  475. </simpara></listitem></varlistentry>
  476. <varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> a new <link linkend="GError"><type>GError</type></link>
  477. </simpara></listitem></varlistentry>
  478. </variablelist></refsect2>
  479. <refsect2 id="g-error-matches" role="function">
  480. <title>g_error_matches ()</title>
  481. <indexterm zone="g-error-matches"><primary>g_error_matches</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link> g_error_matches (const <link linkend="GError">GError</link> *error,
  482. <link linkend="GQuark">GQuark</link> domain,
  483. <link linkend="gint">gint</link> code);</programlisting>
  484. <para>
  485. Returns <link linkend="TRUE:CAPS"><literal>TRUE</literal></link> if <parameter>error</parameter> matches <parameter>domain</parameter> and <parameter>code</parameter>, <link linkend="FALSE:CAPS"><literal>FALSE</literal></link>
  486. otherwise.</para>
  487. <para>
  488. </para><variablelist role="params">
  489. <varlistentry><term><parameter>error</parameter>&nbsp;:</term>
  490. <listitem><simpara> a <link linkend="GError"><type>GError</type></link>
  491. </simpara></listitem></varlistentry>
  492. <varlistentry><term><parameter>domain</parameter>&nbsp;:</term>
  493. <listitem><simpara> an error domain
  494. </simpara></listitem></varlistentry>
  495. <varlistentry><term><parameter>code</parameter>&nbsp;:</term>
  496. <listitem><simpara> an error code
  497. </simpara></listitem></varlistentry>
  498. <varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> whether <parameter>error</parameter> has <parameter>domain</parameter> and <parameter>code</parameter>
  499. </simpara></listitem></varlistentry>
  500. </variablelist></refsect2>
  501. <refsect2 id="g-set-error" role="function">
  502. <title>g_set_error ()</title>
  503. <indexterm zone="g-set-error"><primary>g_set_error</primary></indexterm><programlisting><link linkend="void">void</link> g_set_error (<link linkend="GError">GError</link> **err,
  504. <link linkend="GQuark">GQuark</link> domain,
  505. <link linkend="gint">gint</link> code,
  506. const <link linkend="gchar">gchar</link> *format,
  507. ...);</programlisting>
  508. <para>
  509. Does nothing if <parameter>err</parameter> is <link linkend="NULL:CAPS"><literal>NULL</literal></link>; if <parameter>err</parameter> is non-<link linkend="NULL:CAPS"><literal>NULL</literal></link>, then *<parameter>err</parameter> must
  510. be <link linkend="NULL:CAPS"><literal>NULL</literal></link>. A new <link linkend="GError"><type>GError</type></link> is created and assigned to *<parameter>err</parameter>.</para>
  511. <para>
  512. </para><variablelist role="params">
  513. <varlistentry><term><parameter>err</parameter>&nbsp;:</term>
  514. <listitem><simpara> a return location for a <link linkend="GError"><type>GError</type></link>, or <link linkend="NULL:CAPS"><literal>NULL</literal></link>
  515. </simpara></listitem></varlistentry>
  516. <varlistentry><term><parameter>domain</parameter>&nbsp;:</term>
  517. <listitem><simpara> error domain
  518. </simpara></listitem></varlistentry>
  519. <varlistentry><term><parameter>code</parameter>&nbsp;:</term>
  520. <listitem><simpara> error code
  521. </simpara></listitem></varlistentry>
  522. <varlistentry><term><parameter>format</parameter>&nbsp;:</term>
  523. <listitem><simpara> <link linkend="printf"><function>printf()</function></link>-style format
  524. </simpara></listitem></varlistentry>
  525. <varlistentry><term><parameter>...</parameter>&nbsp;:</term>
  526. <listitem><simpara> args for <parameter>format</parameter>
  527. </simpara></listitem></varlistentry>
  528. </variablelist></refsect2>
  529. <refsect2 id="g-propagate-error" role="function">
  530. <title>g_propagate_error ()</title>
  531. <indexterm zone="g-propagate-error"><primary>g_propagate_error</primary></indexterm><programlisting><link linkend="void">void</link> g_propagate_error (<link linkend="GError">GError</link> **dest,
  532. <link linkend="GError">GError</link> *src);</programlisting>
  533. <para>
  534. If <parameter>dest</parameter> is <link linkend="NULL:CAPS"><literal>NULL</literal></link>, free <parameter>src</parameter>; otherwise, moves <parameter>src</parameter> into *<parameter>dest</parameter>.
  535. The error variable <parameter>dest</parameter> points to must be <link linkend="NULL:CAPS"><literal>NULL</literal></link>.</para>
  536. <para>
  537. </para><variablelist role="params">
  538. <varlistentry><term><parameter>dest</parameter>&nbsp;:</term>
  539. <listitem><simpara> error return location
  540. </simpara></listitem></varlistentry>
  541. <varlistentry><term><parameter>src</parameter>&nbsp;:</term>
  542. <listitem><simpara> error to move into the return location
  543. </simpara></listitem></varlistentry>
  544. </variablelist></refsect2>
  545. <refsect2 id="g-clear-error" role="function">
  546. <title>g_clear_error ()</title>
  547. <indexterm zone="g-clear-error"><primary>g_clear_error</primary></indexterm><programlisting><link linkend="void">void</link> g_clear_error (<link linkend="GError">GError</link> **err);</programlisting>
  548. <para>
  549. If <parameter>err</parameter> is <link linkend="NULL:CAPS"><literal>NULL</literal></link>, does nothing. If <parameter>err</parameter> is non-<link linkend="NULL:CAPS"><literal>NULL</literal></link>,
  550. calls <link linkend="g-error-free"><function>g_error_free()</function></link> on *<parameter>err</parameter> and sets *<parameter>err</parameter> to <link linkend="NULL:CAPS"><literal>NULL</literal></link>.</para>
  551. <para>
  552. </para><variablelist role="params">
  553. <varlistentry><term><parameter>err</parameter>&nbsp;:</term>
  554. <listitem><simpara> a <link linkend="GError"><type>GError</type></link> return location
  555. </simpara></listitem></varlistentry>
  556. </variablelist></refsect2>
  557. <refsect2 id="g-prefix-error" role="function" condition="since:2.16">
  558. <title>g_prefix_error ()</title>
  559. <indexterm zone="g-prefix-error" role="2.16"><primary>g_prefix_error</primary></indexterm><programlisting><link linkend="void">void</link> g_prefix_error (<link linkend="GError">GError</link> **err,
  560. const <link linkend="gchar">gchar</link> *format,
  561. ...);</programlisting>
  562. <para>
  563. Formats a string according to <parameter>format</parameter> and
  564. prefix it to an existing error message. If
  565. <parameter>err</parameter> is <link linkend="NULL:CAPS"><literal>NULL</literal></link> (ie: no error variable) then do
  566. nothing.
  567. </para>
  568. <para>
  569. If *<parameter>err</parameter> is <link linkend="NULL:CAPS"><literal>NULL</literal></link> (ie: an error variable is
  570. present but there is no error condition) then
  571. also do nothing. Whether or not it makes
  572. sense to take advantage of this feature is up
  573. to you.</para>
  574. <para>
  575. </para><variablelist role="params">
  576. <varlistentry><term><parameter>err</parameter>&nbsp;:</term>
  577. <listitem><simpara> a return location for a <link linkend="GError"><type>GError</type></link>, or <link linkend="NULL:CAPS"><literal>NULL</literal></link>
  578. </simpara></listitem></varlistentry>
  579. <varlistentry><term><parameter>format</parameter>&nbsp;:</term>
  580. <listitem><simpara> <link linkend="printf"><function>printf()</function></link>-style format string
  581. </simpara></listitem></varlistentry>
  582. <varlistentry><term><parameter>...</parameter>&nbsp;:</term>
  583. <listitem><simpara> arguments to <parameter>format</parameter>
  584. </simpara></listitem></varlistentry>
  585. </variablelist><para role="since">Since 2.16
  586. </para></refsect2>
  587. <refsect2 id="g-propagate-prefixed-error" role="function" condition="since:2.16">
  588. <title>g_propagate_prefixed_error ()</title>
  589. <indexterm zone="g-propagate-prefixed-error" role="2.16"><primary>g_propagate_prefixed_error</primary></indexterm><programlisting><link linkend="void">void</link> g_propagate_prefixed_error (<link linkend="GError">GError</link> **dest,
  590. <link linkend="GError">GError</link> *src,
  591. const <link linkend="gchar">gchar</link> *format,
  592. ...);</programlisting>
  593. <para>
  594. If <parameter>dest</parameter> is <link linkend="NULL:CAPS"><literal>NULL</literal></link>, free <parameter>src</parameter>; otherwise,
  595. moves <parameter>src</parameter> into *<parameter>dest</parameter>. *<parameter>dest</parameter> must be <link linkend="NULL:CAPS"><literal>NULL</literal></link>.
  596. After the move, add a prefix as with
  597. <link linkend="g-prefix-error"><function>g_prefix_error()</function></link>.</para>
  598. <para>
  599. </para><variablelist role="params">
  600. <varlistentry><term><parameter>dest</parameter>&nbsp;:</term>
  601. <listitem><simpara> error return location
  602. </simpara></listitem></varlistentry>
  603. <varlistentry><term><parameter>src</parameter>&nbsp;:</term>
  604. <listitem><simpara> error to move into the return location
  605. </simpara></listitem></varlistentry>
  606. <varlistentry><term><parameter>format</parameter>&nbsp;:</term>
  607. <listitem><simpara> <link linkend="printf"><function>printf()</function></link>-style format string
  608. </simpara></listitem></varlistentry>
  609. <varlistentry><term><parameter>...</parameter>&nbsp;:</term>
  610. <listitem><simpara> arguments to <parameter>format</parameter>
  611. </simpara></listitem></varlistentry>
  612. </variablelist><para role="since">Since 2.16
  613. </para></refsect2>
  614. </refsect1>
  615. </refentry>