3 * ====================================================================
\r
4 * Copyright (c) 2000-2004, 2008 CollabNet. All rights reserved.
\r
6 * This software is licensed as described in the file COPYING, which
\r
7 * you should have received as part of this distribution. The terms
\r
8 * are also available at http://subversion.tigris.org/license-1.html.
\r
9 * If newer versions of this license are posted there, you may use a
\r
10 * newer version instead, at your option.
\r
12 * This software consists of voluntary contributions made by many
\r
13 * individuals. For exact contribution history, see the revision
\r
14 * history and logs, available at http://subversion.tigris.org/.
\r
15 * ====================================================================
\r
19 * @brief Common exception handling for Subversion.
\r
28 #include <apr.h> /* for apr_size_t */
\r
29 #include <apr_errno.h> /* APR's error system */
\r
30 #include <apr_pools.h> /* for apr_pool_t */
\r
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
\r
33 #define APR_WANT_STDIO
\r
35 #include <apr_want.h> /* for FILE* */
\r
37 #include "svn_types.h"
\r
41 #endif /* __cplusplus */
\r
43 /** the best kind of (@c svn_error_t *) ! */
\r
44 #define SVN_NO_ERROR 0
\r
46 /* The actual error codes are kept in a separate file; see comments
\r
47 there for the reasons why. */
\r
48 #include "svn_error_codes.h"
\r
50 /** Set the error location for debug mode. */
\r
52 svn_error__locate(const char *file,
\r
56 /** Put an English description of @a statcode into @a buf and return @a buf,
\r
57 * NULL-terminated. @a statcode is either an svn error or apr error.
\r
60 svn_strerror(apr_status_t statcode,
\r
62 apr_size_t bufsize);
\r
65 /** If @a err has a custom error message, return that, otherwise
\r
66 * store the generic error string associated with @a err->apr_err into
\r
67 * @a buf (terminating with NULL) and return @a buf.
\r
69 * @since New in 1.4.
\r
71 * @note @a buf and @a bufsize are provided in the interface so that
\r
72 * this function is thread-safe and yet does no allocation.
\r
74 const char *svn_err_best_message(svn_error_t *err,
\r
76 apr_size_t bufsize);
\r
80 /** SVN error creation and destruction.
\r
82 * @defgroup svn_error_error_creation_destroy Error creation and destruction
\r
86 /** Create a nested exception structure.
\r
88 * Input: an APR or SVN custom error code,
\r
89 * a "child" error to wrap,
\r
90 * a specific message
\r
92 * Returns: a new error structure (containing the old one).
\r
94 * @note Errors are always allocated in a subpool of the global pool,
\r
95 * since an error's lifetime is generally not related to the
\r
96 * lifetime of any convenient pool. Errors must be freed
\r
97 * with svn_error_clear(). The specific message should be @c NULL
\r
98 * if there is nothing to add to the general message associated
\r
99 * with the error code.
\r
101 * If creating the "bottommost" error in a chain, pass @c NULL for
\r
102 * the child argument.
\r
105 svn_error_create(apr_status_t apr_err,
\r
106 svn_error_t *child,
\r
107 const char *message);
\r
109 /** Wrapper macro to collect file and line information */
\r
110 #define svn_error_create \
\r
111 (svn_error__locate(__FILE__,__LINE__), (svn_error_create))
\r
113 /** Create an error structure with the given @a apr_err and @a child,
\r
114 * with a printf-style error message produced by passing @a fmt, using
\r
118 svn_error_createf(apr_status_t apr_err,
\r
119 svn_error_t *child,
\r
122 __attribute__ ((format(printf, 3, 4)));
\r
124 /** Wrapper macro to collect file and line information */
\r
125 #define svn_error_createf \
\r
126 (svn_error__locate(__FILE__,__LINE__), (svn_error_createf))
\r
128 /** Wrap a @a status from an APR function. If @a fmt is NULL, this is
\r
129 * equivalent to svn_error_create(status,NULL,NULL). Otherwise,
\r
130 * the error message is constructed by formatting @a fmt and the
\r
131 * following arguments according to apr_psprintf(), and then
\r
132 * appending ": " and the error message corresponding to @a status.
\r
133 * (If UTF-8 translation of the APR error message fails, the ": " and
\r
134 * APR error are not appended to the error message.)
\r
137 svn_error_wrap_apr(apr_status_t status,
\r
140 __attribute__((format(printf, 2, 3)));
\r
142 /** Wrapper macro to collect file and line information */
\r
143 #define svn_error_wrap_apr \
\r
144 (svn_error__locate(__FILE__,__LINE__), (svn_error_wrap_apr))
\r
146 /** A quick n' easy way to create a wrapped exception with your own
\r
147 * message, before throwing it up the stack. (It uses all of the
\r
148 * @a child's fields.)
\r
151 svn_error_quick_wrap(svn_error_t *child,
\r
152 const char *new_msg);
\r
154 /** Wrapper macro to collect file and line information */
\r
155 #define svn_error_quick_wrap \
\r
156 (svn_error__locate(__FILE__,__LINE__), (svn_error_quick_wrap))
\r
158 /** Compose two errors, returning the composition as a brand new error
\r
159 * and consuming the original errors. Either or both of @a err1 and
\r
160 * @a err2 may be @c SVN_NO_ERROR. If both are not @c SVN_NO_ERROR,
\r
161 * @a err2 will follow @a err1 in the chain of the returned error.
\r
163 * @since New in 1.6.
\r
166 svn_error_compose_create(svn_error_t *err1,
\r
167 svn_error_t *err2);
\r
169 /** Add @a new_err to the end of @a chain's chain of errors. The @a new_err
\r
170 * chain will be copied into @a chain's pool and destroyed, so @a new_err
\r
171 * itself becomes invalid after this function.
\r
174 svn_error_compose(svn_error_t *chain,
\r
175 svn_error_t *new_err);
\r
177 /** Return the root cause of @a err by finding the last error in its
\r
178 * chain (e.g. it or its children). @a err may be @c SVN_NO_ERROR, in
\r
179 * which case @c SVN_NO_ERROR is returned.
\r
181 * @since New in 1.5.
\r
184 svn_error_root_cause(svn_error_t *err);
\r
186 /** Create a new error that is a deep copy of @a err and return it.
\r
188 * @since New in 1.2.
\r
191 svn_error_dup(svn_error_t *err);
\r
193 /** Free the memory used by @a error, as well as all ancestors and
\r
194 * descendants of @a error.
\r
196 * Unlike other Subversion objects, errors are managed explicitly; you
\r
197 * MUST clear an error if you are ignoring it, or you are leaking memory.
\r
198 * For convenience, @a error may be @c NULL, in which case this function does
\r
199 * nothing; thus, svn_error_clear(svn_foo(...)) works as an idiom to
\r
203 svn_error_clear(svn_error_t *error);
\r
207 * Very basic default error handler: print out error stack @a error to the
\r
208 * stdio stream @a stream, with each error prefixed by @a prefix; quit and
\r
209 * clear @a error iff the @a fatal flag is set. Allocations are performed
\r
210 * in the @a error's pool.
\r
212 * If you're not sure what prefix to pass, just pass "svn: ". That's
\r
213 * what code that used to call svn_handle_error() and now calls
\r
214 * svn_handle_error2() does.
\r
216 * @since New in 1.2.
\r
219 svn_handle_error2(svn_error_t *error,
\r
221 svn_boolean_t fatal,
\r
222 const char *prefix);
\r
224 /** Like svn_handle_error2() but with @c prefix set to "svn: "
\r
226 * @deprecated Provided for backward compatibility with the 1.1 API.
\r
230 svn_handle_error(svn_error_t *error,
\r
232 svn_boolean_t fatal);
\r
235 * Very basic default warning handler: print out the error @a error to the
\r
236 * stdio stream @a stream, prefixed by @a prefix. Allocations are
\r
237 * performed in the error's pool.
\r
239 * @since New in 1.2.
\r
242 svn_handle_warning2(FILE *stream,
\r
243 svn_error_t *error,
\r
244 const char *prefix);
\r
246 /** Like svn_handle_warning2() but with @c prefix set to "svn: "
\r
248 * @deprecated Provided for backward compatibility with the 1.1 API.
\r
252 svn_handle_warning(FILE *stream,
\r
253 svn_error_t *error);
\r
256 /** A statement macro for checking error values.
\r
258 * Evaluate @a expr. If it yields an error, return that error from the
\r
259 * current function. Otherwise, continue.
\r
261 * The <tt>do { ... } while (0)</tt> wrapper has no semantic effect,
\r
262 * but it makes this macro syntactically equivalent to the expression
\r
263 * statement it resembles. Without it, statements like
\r
267 * SVN_ERR (some operation);
\r
272 * would not mean what they appear to.
\r
274 #define SVN_ERR(expr) \
\r
276 svn_error_t *svn_err__temp = (expr); \
\r
277 if (svn_err__temp) \
\r
278 return svn_err__temp; \
\r
282 /** A statement macro, very similar to @c SVN_ERR.
\r
284 * This macro will wrap the error with the specified text before
\r
285 * returning the error.
\r
287 #define SVN_ERR_W(expr, wrap_msg) \
\r
289 svn_error_t *svn_err__temp = (expr); \
\r
290 if (svn_err__temp) \
\r
291 return svn_error_quick_wrap(svn_err__temp, wrap_msg); \
\r
295 /** A statement macro, similar to @c SVN_ERR, but returns an integer.
\r
297 * Evaluate @a expr. If it yields an error, handle that error and
\r
298 * return @c EXIT_FAILURE.
\r
300 #define SVN_INT_ERR(expr) \
\r
302 svn_error_t *svn_err__temp = (expr); \
\r
303 if (svn_err__temp) { \
\r
304 svn_handle_error2(svn_err__temp, stderr, FALSE, "svn: "); \
\r
305 svn_error_clear(svn_err__temp); \
\r
306 return EXIT_FAILURE; } \
\r
312 * Return TRUE if @a err is an error specifically related to locking a
\r
313 * path in the repository, FALSE otherwise.
\r
315 * SVN_ERR_FS_OUT_OF_DATE is in here because it's a non-fatal error
\r
316 * that can be thrown when attempting to lock an item.
\r
318 * @since New in 1.2.
\r
320 #define SVN_ERR_IS_LOCK_ERROR(err) \
\r
321 (err->apr_err == SVN_ERR_FS_PATH_ALREADY_LOCKED || \
\r
322 err->apr_err == SVN_ERR_FS_OUT_OF_DATE) \
\r
325 * Return TRUE if @a err is an error specifically related to unlocking
\r
326 * a path in the repository, FALSE otherwise.
\r
328 * @since New in 1.2.
\r
330 #define SVN_ERR_IS_UNLOCK_ERROR(err) \
\r
331 (err->apr_err == SVN_ERR_FS_PATH_NOT_LOCKED || \
\r
332 err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN || \
\r
333 err->apr_err == SVN_ERR_FS_LOCK_OWNER_MISMATCH || \
\r
334 err->apr_err == SVN_ERR_FS_NO_SUCH_LOCK || \
\r
335 err->apr_err == SVN_ERR_RA_NOT_LOCKED || \
\r
336 err->apr_err == SVN_ERR_FS_LOCK_EXPIRED)
\r
338 /** Report that an internal malfunction has occurred, and possibly terminate
\r
341 * Act as determined by the current "malfunction handler" which may have
\r
342 * been specified by a call to svn_error_set_malfunction_handler() or else
\r
343 * is the default handler as specified in that function's documentation. If
\r
344 * the malfunction handler returns, then cause the function using this macro
\r
345 * to return the error object that it generated.
\r
347 * @note The intended use of this macro is where execution reaches a point
\r
348 * that cannot possibly be reached unless there is a bug in the program.
\r
350 * @since New in 1.6.
\r
352 #define SVN_ERR_MALFUNCTION() \
\r
354 return svn_error__malfunction(TRUE, __FILE__, __LINE__, NULL); \
\r
357 /** Similar to SVN_ERR_MALFUNCTION(), but without the option of returning
\r
358 * an error to the calling function.
\r
360 * If possible you should use SVN_ERR_MALFUNCTION() instead.
\r
362 * @since New in 1.6.
\r
364 #define SVN_ERR_MALFUNCTION_NO_RETURN() \
\r
366 svn_error__malfunction(FALSE, __FILE__, __LINE__, NULL); \
\r
370 /** Check that a condition is true: if not, report an error and possibly
\r
371 * terminate the program.
\r
373 * If the Boolean expression @a expr is true, do nothing. Otherwise,
\r
374 * act as determined by the current "malfunction handler" which may have
\r
375 * been specified by a call to svn_error_set_malfunction_handler() or else
\r
376 * is the default handler as specified in that function's documentation. If
\r
377 * the malfunction handler returns, then cause the function using this macro
\r
378 * to return the error object that it generated.
\r
380 * @note The intended use of this macro is to check a condition that cannot
\r
381 * possibly be false unless there is a bug in the program.
\r
383 * @note The condition to be checked should not be computationally expensive
\r
384 * if it is reached often, as, unlike traditional "assert" statements, the
\r
385 * evaluation of this expression is not compiled out in release-mode builds.
\r
387 * @since New in 1.6.
\r
389 #define SVN_ERR_ASSERT(expr) \
\r
392 SVN_ERR(svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr)); \
\r
395 /** Similar to SVN_ERR_ASSERT(), but without the option of returning
\r
396 * an error to the calling function.
\r
398 * If possible you should use SVN_ERR_ASSERT() instead.
\r
400 * @since New in 1.6.
\r
402 #define SVN_ERR_ASSERT_NO_RETURN(expr) \
\r
405 svn_error__malfunction(FALSE, __FILE__, __LINE__, #expr); \
\r
411 /** A helper function for the macros that report malfunctions. Handle a
\r
412 * malfunction by calling the current "malfunction handler" which may have
\r
413 * been specified by a call to svn_error_set_malfunction_handler() or else
\r
414 * is the default handler as specified in that function's documentation.
\r
416 * Pass all of the parameters to the handler. The error occurred in the
\r
417 * source file @a file at line @a line, and was an assertion failure of the
\r
418 * expression @a expr, or, if @a expr is null, an unconditional error.
\r
420 * If @a can_return is true, the handler can return an error object
\r
421 * that is returned by the caller. If @a can_return is false the
\r
422 * method should never return. (The caller will call abort())
\r
424 * @since New in 1.6.
\r
427 svn_error__malfunction(svn_boolean_t can_return,
\r
432 /** A type of function that handles an assertion failure or other internal
\r
433 * malfunction detected within the Subversion libraries.
\r
435 * The error occurred in the source file @a file at line @a line, and was an
\r
436 * assertion failure of the expression @a expr, or, if @a expr is null, an
\r
437 * unconditional error.
\r
439 * If @a can_return is false a function of this type must never return.
\r
441 * If @a can_return is true a function of this type must do one of:
\r
442 * - Return an error object describing the error, using an error code in
\r
443 * the category SVN_ERR_MALFUNC_CATEGORY_START.
\r
446 * The function may alter its behaviour according to compile-time
\r
447 * and run-time and even interactive conditions.
\r
449 * @since New in 1.6.
\r
451 typedef svn_error_t *(*svn_error_malfunction_handler_t)
\r
452 (svn_boolean_t can_return, const char *file, int line, const char *expr);
\r
454 /** Cause subsequent malfunctions to be handled by @a func.
\r
455 * Return the handler that was previously in effect.
\r
457 * @a func may not be null.
\r
459 * @note The default handler is svn_error_abort_on_malfunction().
\r
461 * @note This function must be called in a single-threaded context.
\r
463 * @since New in 1.6.
\r
465 svn_error_malfunction_handler_t
\r
466 svn_error_set_malfunction_handler(svn_error_malfunction_handler_t func);
\r
468 /** Handle a malfunction by returning an error object that describes it.
\r
470 * When @a can_return is false, abort()
\r
472 * This function implements @c svn_error_malfunction_handler_t.
\r
474 * @since New in 1.6.
\r
477 svn_error_raise_on_malfunction(svn_boolean_t can_return,
\r
482 /** Handle a malfunction by printing a message to stderr and aborting.
\r
484 * This function implements @c svn_error_malfunction_handler_t.
\r
486 * @since New in 1.6.
\r
489 svn_error_abort_on_malfunction(svn_boolean_t can_return,
\r
497 #endif /* __cplusplus */
\r
499 #endif /* SVN_ERROR_H */
\r