]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/subversion/subversion/include/svn_error.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / subversion / subversion / include / svn_error.h
1 /**
2  * @copyright
3  * ====================================================================
4  *    Licensed to the Apache Software Foundation (ASF) under one
5  *    or more contributor license agreements.  See the NOTICE file
6  *    distributed with this work for additional information
7  *    regarding copyright ownership.  The ASF licenses this file
8  *    to you under the Apache License, Version 2.0 (the
9  *    "License"); you may not use this file except in compliance
10  *    with the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *    Unless required by applicable law or agreed to in writing,
15  *    software distributed under the License is distributed on an
16  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  *    KIND, either express or implied.  See the License for the
18  *    specific language governing permissions and limitations
19  *    under the License.
20  * ====================================================================
21  * @endcopyright
22  *
23  * @file svn_error.h
24  * @brief Common exception handling for Subversion.
25  */
26
27 #ifndef SVN_ERROR_H
28 #define SVN_ERROR_H
29
30 #include <apr.h>        /* for apr_size_t */
31 #include <apr_errno.h>  /* APR's error system */
32 #include <apr_pools.h>  /* for apr_pool_t */
33
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 #define APR_WANT_STDIO
36 #endif
37 #include <apr_want.h>   /* for FILE* */
38
39 #include "svn_types.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif /* __cplusplus */
44
45
46 /* For the Subversion developers, this #define turns on extended "stack
47    traces" of any errors that get thrown. See the SVN_ERR() macro.  */
48 #ifdef SVN_DEBUG
49 #define SVN_ERR__TRACING
50 #endif
51
52
53 /** the best kind of (@c svn_error_t *) ! */
54 #define SVN_NO_ERROR   0
55
56 /* The actual error codes are kept in a separate file; see comments
57    there for the reasons why. */
58 #include "svn_error_codes.h"
59
60 /** Put an English description of @a statcode into @a buf and return @a buf,
61  * NULL-terminated. @a statcode is either an svn error or apr error.
62  */
63 char *
64 svn_strerror(apr_status_t statcode,
65              char *buf,
66              apr_size_t bufsize);
67
68
69 /**
70  * Return the symbolic name of an error code.  If the error code
71  * is in svn_error_codes.h, return the name of the macro as a string.
72  * If the error number is not recognised, return @c NULL.
73  *
74  * An error number may not be recognised because it was defined in a future
75  * version of Subversion (e.g., a 1.9.x server may transmit a defined-in-1.9.0
76  * error number to a 1.8.x client).
77  *
78  * An error number may be recognised @em incorrectly if the @c apr_status_t
79  * value originates in another library (such as libserf) which also uses APR.
80  * (This is a theoretical concern only: the @c apr_err member of #svn_error_t
81  * should never contain a "foreign" @c apr_status_t value, and
82  * in any case Subversion and Serf use non-overlapping subsets of the
83  * @c APR_OS_START_USERERR range.)
84  *
85  * Support for error codes returned by APR itself (i.e., not in the
86  * @c APR_OS_START_USERERR range, as defined in apr_errno.h) may be implemented
87  * in the future.
88  *
89  * @note In rare cases, a single numeric code has more than one symbolic name.
90  * (For example, #SVN_ERR_WC_NOT_DIRECTORY and #SVN_ERR_WC_NOT_WORKING_COPY).
91  * In those cases, it is not guaranteed which symbolic name is returned.
92  *
93  * @since New in 1.8.
94  */
95 const char *
96 svn_error_symbolic_name(apr_status_t statcode);
97
98
99 /** If @a err has a custom error message, return that, otherwise
100  * store the generic error string associated with @a err->apr_err into
101  * @a buf (terminating with NULL) and return @a buf.
102  *
103  * @since New in 1.4.
104  *
105  * @note @a buf and @a bufsize are provided in the interface so that
106  * this function is thread-safe and yet does no allocation.
107  */
108 const char *svn_err_best_message(svn_error_t *err,
109                                  char *buf,
110                                  apr_size_t bufsize);
111
112
113 \f
114 /** SVN error creation and destruction.
115  *
116  * @defgroup svn_error_error_creation_destroy Error creation and destruction
117  * @{
118  */
119
120 /** Create a nested exception structure.
121  *
122  * Input:  an APR or SVN custom error code,
123  *         a "child" error to wrap,
124  *         a specific message
125  *
126  * Returns:  a new error structure (containing the old one).
127  *
128  * @note Errors are always allocated in a subpool of the global pool,
129  *        since an error's lifetime is generally not related to the
130  *        lifetime of any convenient pool.  Errors must be freed
131  *        with svn_error_clear().  The specific message should be @c NULL
132  *        if there is nothing to add to the general message associated
133  *        with the error code.
134  *
135  *        If creating the "bottommost" error in a chain, pass @c NULL for
136  *        the child argument.
137  */
138 svn_error_t *
139 svn_error_create(apr_status_t apr_err,
140                  svn_error_t *child,
141                  const char *message);
142
143 /** Create an error structure with the given @a apr_err and @a child,
144  * with a printf-style error message produced by passing @a fmt, using
145  * apr_psprintf().
146  */
147 svn_error_t *
148 svn_error_createf(apr_status_t apr_err,
149                   svn_error_t *child,
150                   const char *fmt,
151                   ...)
152   __attribute__ ((format(printf, 3, 4)));
153
154 /** Wrap a @a status from an APR function.  If @a fmt is NULL, this is
155  * equivalent to svn_error_create(status,NULL,NULL).  Otherwise,
156  * the error message is constructed by formatting @a fmt and the
157  * following arguments according to apr_psprintf(), and then
158  * appending ": " and the error message corresponding to @a status.
159  * (If UTF-8 translation of the APR error message fails, the ": " and
160  * APR error are not appended to the error message.)
161  */
162 svn_error_t *
163 svn_error_wrap_apr(apr_status_t status,
164                    const char *fmt,
165                    ...)
166        __attribute__((format(printf, 2, 3)));
167
168 /** A quick n' easy way to create a wrapped exception with your own
169  * message, before throwing it up the stack.  (It uses all of the
170  * @a child's fields.)
171  */
172 svn_error_t *
173 svn_error_quick_wrap(svn_error_t *child,
174                      const char *new_msg);
175
176 /** Compose two errors, returning the composition as a brand new error
177  * and consuming the original errors.  Either or both of @a err1 and
178  * @a err2 may be @c SVN_NO_ERROR.  If both are not @c SVN_NO_ERROR,
179  * @a err2 will follow @a err1 in the chain of the returned error.
180  *
181  * Either @a err1 or @a err2 can be functions that return svn_error_t*
182  * but if both are functions they can be evaluated in either order as
183  * per the C language rules.
184  *
185  * @since New in 1.6.
186  */
187 svn_error_t *
188 svn_error_compose_create(svn_error_t *err1,
189                          svn_error_t *err2);
190
191 /** Add @a new_err to the end of @a chain's chain of errors.  The @a new_err
192  * chain will be copied into @a chain's pool and destroyed, so @a new_err
193  * itself becomes invalid after this function.
194  *
195  * Either @a chain or @a new_err can be functions that return svn_error_t*
196  * but if both are functions they can be evaluated in either order as
197  * per the C language rules.
198  */
199 void
200 svn_error_compose(svn_error_t *chain,
201                   svn_error_t *new_err);
202
203 /** Return the root cause of @a err by finding the last error in its
204  * chain (e.g. it or its children).  @a err may be @c SVN_NO_ERROR, in
205  * which case @c SVN_NO_ERROR is returned.
206  *
207  * @since New in 1.5.
208  */
209 svn_error_t *
210 svn_error_root_cause(svn_error_t *err);
211
212 /** Return the first error in @a err's chain that has an error code @a
213  * apr_err or #SVN_NO_ERROR if there is no error with that code.  The
214  * returned error should @em not be cleared as it shares memory with @a err.
215  *
216  * If @a err is #SVN_NO_ERROR, return #SVN_NO_ERROR.
217  *
218  * @since New in 1.7.
219  */
220 svn_error_t *
221 svn_error_find_cause(svn_error_t *err, apr_status_t apr_err);
222
223 /** Create a new error that is a deep copy of @a err and return it.
224  *
225  * @since New in 1.2.
226  */
227 svn_error_t *
228 svn_error_dup(svn_error_t *err);
229
230 /** Free the memory used by @a error, as well as all ancestors and
231  * descendants of @a error.
232  *
233  * Unlike other Subversion objects, errors are managed explicitly; you
234  * MUST clear an error if you are ignoring it, or you are leaking memory.
235  * For convenience, @a error may be @c NULL, in which case this function does
236  * nothing; thus, svn_error_clear(svn_foo(...)) works as an idiom to
237  * ignore errors.
238  */
239 void
240 svn_error_clear(svn_error_t *error);
241
242
243 #if defined(SVN_ERR__TRACING)
244 /** Set the error location for debug mode. */
245 void
246 svn_error__locate(const char *file,
247                   long line);
248
249 /* Wrapper macros to collect file and line information */
250 #define svn_error_create \
251   (svn_error__locate(__FILE__,__LINE__), (svn_error_create))
252 #define svn_error_createf \
253   (svn_error__locate(__FILE__,__LINE__), (svn_error_createf))
254 #define svn_error_wrap_apr \
255   (svn_error__locate(__FILE__,__LINE__), (svn_error_wrap_apr))
256 #define svn_error_quick_wrap \
257   (svn_error__locate(__FILE__,__LINE__), (svn_error_quick_wrap))
258 #endif
259
260
261 /**
262  * Very basic default error handler: print out error stack @a error to the
263  * stdio stream @a stream, with each error prefixed by @a prefix; quit and
264  * clear @a error iff the @a fatal flag is set.  Allocations are performed
265  * in the @a error's pool.
266  *
267  * If you're not sure what prefix to pass, just pass "svn: ".  That's
268  * what code that used to call svn_handle_error() and now calls
269  * svn_handle_error2() does.
270  *
271  * @since New in 1.2.
272  */
273 void
274 svn_handle_error2(svn_error_t *error,
275                   FILE *stream,
276                   svn_boolean_t fatal,
277                   const char *prefix);
278
279 /** Like svn_handle_error2() but with @c prefix set to "svn: "
280  *
281  * @deprecated Provided for backward compatibility with the 1.1 API.
282  */
283 SVN_DEPRECATED
284 void
285 svn_handle_error(svn_error_t *error,
286                  FILE *stream,
287                  svn_boolean_t fatal);
288
289 /**
290  * Very basic default warning handler: print out the error @a error to the
291  * stdio stream @a stream, prefixed by @a prefix.  Allocations are
292  * performed in the error's pool.
293  *
294  * @a error may not be @c NULL.
295  *
296  * @since New in 1.2.
297  */
298 void
299 svn_handle_warning2(FILE *stream,
300                     svn_error_t *error,
301                     const char *prefix);
302
303 /** Like svn_handle_warning2() but with @c prefix set to "svn: "
304  *
305  * @deprecated Provided for backward compatibility with the 1.1 API.
306  */
307 SVN_DEPRECATED
308 void
309 svn_handle_warning(FILE *stream,
310                    svn_error_t *error);
311
312
313 /** A statement macro for checking error values.
314  *
315  * Evaluate @a expr.  If it yields an error, return that error from the
316  * current function.  Otherwise, continue.
317  *
318  * The <tt>do { ... } while (0)</tt> wrapper has no semantic effect,
319  * but it makes this macro syntactically equivalent to the expression
320  * statement it resembles.  Without it, statements like
321  *
322  * @code
323  *   if (a)
324  *     SVN_ERR(some operation);
325  *   else
326  *     foo;
327  * @endcode
328  *
329  * would not mean what they appear to.
330  */
331 #define SVN_ERR(expr)                           \
332   do {                                          \
333     svn_error_t *svn_err__temp = (expr);        \
334     if (svn_err__temp)                          \
335       return svn_error_trace(svn_err__temp);    \
336   } while (0)
337
338 /**
339  * A macro for wrapping an error in a source-location trace message.
340  *
341  * This macro can be used when directly returning an already created
342  * error (when not using SVN_ERR, svn_error_create(), etc.) to ensure
343  * that the call stack is recorded correctly.
344  *
345  * @since New in 1.7.
346  */
347 #ifdef SVN_ERR__TRACING
348 svn_error_t *
349 svn_error__trace(const char *file, long line, svn_error_t *err);
350
351 #define svn_error_trace(expr)  svn_error__trace(__FILE__, __LINE__, (expr))
352 #else
353 #define svn_error_trace(expr)  (expr)
354 #endif
355
356 /**
357  * Returns an error chain that is based on @a err's error chain but
358  * does not include any error tracing placeholders.  @a err is not
359  * modified, except for any allocations using its pool.
360  *
361  * The returned error chain is allocated from @a err's pool and shares
362  * its message and source filename character arrays.  The returned
363  * error chain should *not* be cleared because it is not a fully
364  * fledged error chain, only clearing @a err should be done to clear
365  * the returned error chain.  If @a err is cleared, then the returned
366  * error chain is unusable.
367  *
368  * @a err can be #SVN_NO_ERROR.  If @a err is not #SVN_NO_ERROR, then
369  * the last link in the error chain must be a non-tracing error, i.e,
370  * a real error.
371  *
372  * @since New in 1.7.
373  */
374 svn_error_t *svn_error_purge_tracing(svn_error_t *err);
375
376
377 /** A statement macro, very similar to @c SVN_ERR.
378  *
379  * This macro will wrap the error with the specified text before
380  * returning the error.
381  */
382 #define SVN_ERR_W(expr, wrap_msg)                           \
383   do {                                                      \
384     svn_error_t *svn_err__temp = (expr);                    \
385     if (svn_err__temp)                                      \
386       return svn_error_quick_wrap(svn_err__temp, wrap_msg); \
387   } while (0)
388
389
390 /** A statement macro, similar to @c SVN_ERR, but returns an integer.
391  *
392  * Evaluate @a expr. If it yields an error, handle that error and
393  * return @c EXIT_FAILURE.
394  */
395 #define SVN_INT_ERR(expr)                                        \
396   do {                                                           \
397     svn_error_t *svn_err__temp = (expr);                         \
398     if (svn_err__temp) {                                         \
399       svn_handle_error2(svn_err__temp, stderr, FALSE, "svn: ");  \
400       svn_error_clear(svn_err__temp);                            \
401       return EXIT_FAILURE; }                                     \
402   } while (0)
403
404 /** @} */
405
406 \f
407 /** Error groups
408  *
409  * @defgroup svn_error_error_groups Error groups
410  * @{
411  */
412
413 /**
414  * Return TRUE if @a err is an error specifically related to locking a
415  * path in the repository, FALSE otherwise.
416  *
417  * SVN_ERR_FS_OUT_OF_DATE and SVN_ERR_FS_NOT_FOUND are in here because it's a
418  * non-fatal error that can be thrown when attempting to lock an item.
419  *
420  * @since New in 1.2.
421  */
422 #define SVN_ERR_IS_LOCK_ERROR(err)                          \
423   (err->apr_err == SVN_ERR_FS_PATH_ALREADY_LOCKED ||        \
424    err->apr_err == SVN_ERR_FS_NOT_FOUND           ||        \
425    err->apr_err == SVN_ERR_FS_OUT_OF_DATE         ||        \
426    err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN)
427
428 /**
429  * Return TRUE if @a err is an error specifically related to unlocking
430  * a path in the repository, FALSE otherwise.
431  *
432  * @since New in 1.2.
433  */
434 #define SVN_ERR_IS_UNLOCK_ERROR(err)                        \
435   (err->apr_err == SVN_ERR_FS_PATH_NOT_LOCKED ||            \
436    err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN ||             \
437    err->apr_err == SVN_ERR_FS_LOCK_OWNER_MISMATCH ||        \
438    err->apr_err == SVN_ERR_FS_NO_SUCH_LOCK ||               \
439    err->apr_err == SVN_ERR_RA_NOT_LOCKED ||                 \
440    err->apr_err == SVN_ERR_FS_LOCK_EXPIRED)
441
442 /** Evaluates to @c TRUE iff @a apr_err (of type apr_status_t) is in the given
443  * @a category, which should be one of the @c SVN_ERR_*_CATEGORY_START
444  * constants.
445  *
446  * @since New in 1.7.
447  */
448 #define SVN_ERROR_IN_CATEGORY(apr_err, category)            \
449     ((category) == ((apr_err) / SVN_ERR_CATEGORY_SIZE) * SVN_ERR_CATEGORY_SIZE)
450
451
452 /** @} */
453
454 \f
455 /** Internal malfunctions and assertions
456  *
457  * @defgroup svn_error_malfunction_assertion Malfunctions and assertions
458  * @{
459  */
460
461 /** Report that an internal malfunction has occurred, and possibly terminate
462  * the program.
463  *
464  * Act as determined by the current "malfunction handler" which may have
465  * been specified by a call to svn_error_set_malfunction_handler() or else
466  * is the default handler as specified in that function's documentation. If
467  * the malfunction handler returns, then cause the function using this macro
468  * to return the error object that it generated.
469  *
470  * @note The intended use of this macro is where execution reaches a point
471  * that cannot possibly be reached unless there is a bug in the program.
472  *
473  * @since New in 1.6.
474  */
475 #define SVN_ERR_MALFUNCTION()                                      \
476   do {                                                             \
477     return svn_error_trace(svn_error__malfunction(                 \
478                                  TRUE, __FILE__, __LINE__, NULL)); \
479   } while (0)
480
481 /** Similar to SVN_ERR_MALFUNCTION(), but without the option of returning
482  * an error to the calling function.
483  *
484  * If possible you should use SVN_ERR_MALFUNCTION() instead.
485  *
486  * @since New in 1.6.
487  */
488 #define SVN_ERR_MALFUNCTION_NO_RETURN()                      \
489   do {                                                       \
490     svn_error__malfunction(FALSE, __FILE__, __LINE__, NULL); \
491     abort();                                                 \
492   } while (1)
493
494 /** Like SVN_ERR_ASSERT(), but append ERR to the returned error chain.
495  *
496  * If EXPR is false, return a malfunction error whose chain includes ERR.
497  * If EXPR is true, do nothing.  (In particular, this does not clear ERR.)
498  *
499  * Types: (svn_boolean_t expr, svn_error_t *err)
500  *
501  * @since New in 1.8.
502  */
503 #ifdef __clang_analyzer__
504 #include <assert.h>
505 /* Just ignore ERR.  If the assert triggers, it'll be our least concern. */
506 #define SVN_ERR_ASSERT_E(expr, err)       assert((expr))
507 #else
508 #define SVN_ERR_ASSERT_E(expr, err)                                      \
509   do {                                                                  \
510     if (!(expr)) {                                                      \
511       return svn_error_compose_create(                                  \
512                svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr), \
513                (err));                                                  \
514     }                                                                   \
515   } while (0)
516 #endif
517
518
519 /** Check that a condition is true: if not, report an error and possibly
520  * terminate the program.
521  *
522  * If the Boolean expression @a expr is true, do nothing. Otherwise,
523  * act as determined by the current "malfunction handler" which may have
524  * been specified by a call to svn_error_set_malfunction_handler() or else
525  * is the default handler as specified in that function's documentation. If
526  * the malfunction handler returns, then cause the function using this macro
527  * to return the error object that it generated.
528  *
529  * @note The intended use of this macro is to check a condition that cannot
530  * possibly be false unless there is a bug in the program.
531  *
532  * @note The condition to be checked should not be computationally expensive
533  * if it is reached often, as, unlike traditional "assert" statements, the
534  * evaluation of this expression is not compiled out in release-mode builds.
535  *
536  * @since New in 1.6.
537  *
538  * @see SVN_ERR_ASSERT_E()
539  */
540 #ifdef __clang_analyzer__
541 #include <assert.h>
542 #define SVN_ERR_ASSERT(expr)       assert((expr))
543 #else
544 #define SVN_ERR_ASSERT(expr)                                            \
545   do {                                                                  \
546     if (!(expr))                                                        \
547       SVN_ERR(svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr)); \
548   } while (0)
549 #endif
550
551 /** Similar to SVN_ERR_ASSERT(), but without the option of returning
552  * an error to the calling function.
553  *
554  * If possible you should use SVN_ERR_ASSERT() instead.
555  *
556  * @since New in 1.6.
557  */
558 #define SVN_ERR_ASSERT_NO_RETURN(expr)                          \
559   do {                                                          \
560     if (!(expr)) {                                              \
561       svn_error__malfunction(FALSE, __FILE__, __LINE__, #expr); \
562       abort();                                                  \
563     }                                                           \
564   } while (0)
565
566 /** Report a "Not implemented" malfunction.  Internal use only. */
567 #define SVN__NOT_IMPLEMENTED() \
568   return svn_error__malfunction(TRUE, __FILE__, __LINE__, "Not implemented.")
569
570 /** A helper function for the macros that report malfunctions. Handle a
571  * malfunction by calling the current "malfunction handler" which may have
572  * been specified by a call to svn_error_set_malfunction_handler() or else
573  * is the default handler as specified in that function's documentation.
574  *
575  * Pass all of the parameters to the handler. The error occurred in the
576  * source file @a file at line @a line, and was an assertion failure of the
577  * expression @a expr, or, if @a expr is null, an unconditional error.
578  *
579  * If @a can_return is true, the handler can return an error object
580  * that is returned by the caller. If @a can_return is false the
581  * method should never return. (The caller will call abort())
582  *
583  * @since New in 1.6.
584  */
585 svn_error_t *
586 svn_error__malfunction(svn_boolean_t can_return,
587                        const char *file,
588                        int line,
589                        const char *expr);
590
591 /** A type of function that handles an assertion failure or other internal
592  * malfunction detected within the Subversion libraries.
593  *
594  * The error occurred in the source file @a file at line @a line, and was an
595  * assertion failure of the expression @a expr, or, if @a expr is null, an
596  * unconditional error.
597  *
598  * If @a can_return is false a function of this type must never return.
599  *
600  * If @a can_return is true a function of this type must do one of:
601  *   - Return an error object describing the error, using an error code in
602  *     the category SVN_ERR_MALFUNC_CATEGORY_START.
603  *   - Never return.
604  *
605  * The function may alter its behaviour according to compile-time
606  * and run-time and even interactive conditions.
607  *
608  * @see SVN_ERROR_IN_CATEGORY()
609  *
610  * @since New in 1.6.
611  */
612 typedef svn_error_t *(*svn_error_malfunction_handler_t)
613   (svn_boolean_t can_return, const char *file, int line, const char *expr);
614
615 /** Cause subsequent malfunctions to be handled by @a func.
616  * Return the handler that was previously in effect.
617  *
618  * @a func may not be null.
619  *
620  * @note The default handler is svn_error_abort_on_malfunction().
621  *
622  * @note This function must be called in a single-threaded context.
623  *
624  * @since New in 1.6.
625  */
626 svn_error_malfunction_handler_t
627 svn_error_set_malfunction_handler(svn_error_malfunction_handler_t func);
628
629 /** Handle a malfunction by returning an error object that describes it.
630  *
631  * When @a can_return is false, abort()
632  *
633  * This function implements @c svn_error_malfunction_handler_t.
634  *
635  * @since New in 1.6.
636  */
637 svn_error_t *
638 svn_error_raise_on_malfunction(svn_boolean_t can_return,
639                                const char *file,
640                                int line,
641                                const char *expr);
642
643 /** Handle a malfunction by printing a message to stderr and aborting.
644  *
645  * This function implements @c svn_error_malfunction_handler_t.
646  *
647  * @since New in 1.6.
648  */
649 svn_error_t *
650 svn_error_abort_on_malfunction(svn_boolean_t can_return,
651                                const char *file,
652                                int line,
653                                const char *expr);
654
655 /** @} */
656
657
658 #ifdef __cplusplus
659 }
660 #endif /* __cplusplus */
661
662 #endif /* SVN_ERROR_H */