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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
20 * ====================================================================
24 * @brief Common exception handling for Subversion.
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 */
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 #define APR_WANT_STDIO
37 #include <apr_want.h> /* for FILE* */
39 #include "svn_types.h"
43 #endif /* __cplusplus */
46 /* For the Subversion developers, this #define turns on extended "stack
47 traces" of any errors that get thrown. See the SVN_ERR() macro. */
49 #define SVN_ERR__TRACING
53 /** the best kind of (@c svn_error_t *) ! */
54 #define SVN_NO_ERROR 0
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"
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.
64 svn_strerror(apr_status_t statcode,
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.
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).
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.)
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
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.
96 svn_error_symbolic_name(apr_status_t statcode);
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.
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.
108 const char *svn_err_best_message(const svn_error_t *err,
114 /** SVN error creation and destruction.
116 * @defgroup svn_error_error_creation_destroy Error creation and destruction
120 /** Create a nested exception structure.
122 * Input: an APR or SVN custom error code,
123 * a "child" error to wrap,
126 * Returns: a new error structure (containing the old one).
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.
135 * If creating the "bottommost" error in a chain, pass @c NULL for
136 * the child argument.
139 svn_error_create(apr_status_t apr_err,
141 const char *message);
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
148 svn_error_createf(apr_status_t apr_err,
152 __attribute__ ((format(printf, 3, 4)));
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.)
163 svn_error_wrap_apr(apr_status_t status,
166 __attribute__((format(printf, 2, 3)));
168 /** If @a child is SVN_NO_ERROR, return SVN_NO_ERROR.
169 * Else, prepend a new error to the error chain of @a child. The new error
170 * uses @a new_msg as error message but all other error attributes (such
171 * as the error code) are copied from @a child.
174 svn_error_quick_wrap(svn_error_t *child,
175 const char *new_msg);
177 /** Like svn_error_quick_wrap(), but with format string support.
182 svn_error_quick_wrapf(svn_error_t *child,
185 __attribute__((format(printf, 2, 3)));
187 /** Compose two errors, returning the composition as a brand new error
188 * and consuming the original errors. Either or both of @a err1 and
189 * @a err2 may be @c SVN_NO_ERROR. If both are not @c SVN_NO_ERROR,
190 * @a err2 will follow @a err1 in the chain of the returned error.
192 * Either @a err1 or @a err2 can be functions that return svn_error_t*
193 * but if both are functions they can be evaluated in either order as
194 * per the C language rules.
199 svn_error_compose_create(svn_error_t *err1,
202 /** Add @a new_err to the end of @a chain's chain of errors. The @a new_err
203 * chain will be copied into @a chain's pool and destroyed, so @a new_err
204 * itself becomes invalid after this function.
206 * Either @a chain or @a new_err can be functions that return svn_error_t*
207 * but if both are functions they can be evaluated in either order as
208 * per the C language rules.
211 svn_error_compose(svn_error_t *chain,
212 svn_error_t *new_err);
214 /** Return the root cause of @a err by finding the last error in its
215 * chain (e.g. it or its children). @a err may be @c SVN_NO_ERROR, in
216 * which case @c SVN_NO_ERROR is returned. The returned error should
217 * @em not be cleared as it shares memory with @a err.
222 svn_error_root_cause(svn_error_t *err);
224 /** Return the first error in @a err's chain that has an error code @a
225 * apr_err or #SVN_NO_ERROR if there is no error with that code. The
226 * returned error should @em not be cleared as it shares memory with @a err.
228 * If @a err is #SVN_NO_ERROR, return #SVN_NO_ERROR.
233 svn_error_find_cause(svn_error_t *err, apr_status_t apr_err);
235 /** Create a new error that is a deep copy of @a err and return it.
240 svn_error_dup(const svn_error_t *err);
242 /** Free the memory used by @a error, as well as all ancestors and
243 * descendants of @a error.
245 * Unlike other Subversion objects, errors are managed explicitly; you
246 * MUST clear an error if you are ignoring it, or you are leaking memory.
247 * For convenience, @a error may be @c NULL, in which case this function does
248 * nothing; thus, svn_error_clear(svn_foo(...)) works as an idiom to
252 svn_error_clear(svn_error_t *error);
255 #if defined(SVN_ERR__TRACING)
256 /** Set the error location for debug mode. */
258 svn_error__locate(const char *file,
261 /* Wrapper macros to collect file and line information */
262 #define svn_error_create \
263 (svn_error__locate(__FILE__,__LINE__), (svn_error_create))
264 #define svn_error_createf \
265 (svn_error__locate(__FILE__,__LINE__), (svn_error_createf))
266 #define svn_error_wrap_apr \
267 (svn_error__locate(__FILE__,__LINE__), (svn_error_wrap_apr))
268 #define svn_error_quick_wrap \
269 (svn_error__locate(__FILE__,__LINE__), (svn_error_quick_wrap))
270 #define svn_error_quick_wrapf \
271 (svn_error__locate(__FILE__,__LINE__), (svn_error_quick_wrapf))
276 * Very basic default error handler: print out error stack @a error to the
277 * stdio stream @a stream, with each error prefixed by @a prefix; quit and
278 * clear @a error iff the @a fatal flag is set. Allocations are performed
279 * in the @a error's pool.
281 * If you're not sure what prefix to pass, just pass "svn: ". That's
282 * what code that used to call svn_handle_error() and now calls
283 * svn_handle_error2() does.
285 * Note that this should only be used from commandline specific code, or
286 * code that knows that @a stream is really where the application wants
287 * to receive its errors on.
292 svn_handle_error2(svn_error_t *error,
297 /** Like svn_handle_error2() but with @c prefix set to "svn: "
299 * @deprecated Provided for backward compatibility with the 1.1 API.
303 svn_handle_error(svn_error_t *error,
305 svn_boolean_t fatal);
308 * Very basic default warning handler: print out the error @a error to the
309 * stdio stream @a stream, prefixed by @a prefix. Allocations are
310 * performed in the error's pool.
312 * @a error may not be @c NULL.
314 * @note This does not clear @a error.
319 svn_handle_warning2(FILE *stream,
320 const svn_error_t *error,
323 /** Like svn_handle_warning2() but with @c prefix set to "svn: "
325 * @deprecated Provided for backward compatibility with the 1.1 API.
329 svn_handle_warning(FILE *stream,
333 /** A statement macro for checking error values.
335 * Evaluate @a expr. If it yields an error, return that error from the
336 * current function. Otherwise, continue.
338 * The <tt>do { ... } while (0)</tt> wrapper has no semantic effect,
339 * but it makes this macro syntactically equivalent to the expression
340 * statement it resembles. Without it, statements like
344 * SVN_ERR(some operation);
349 * would not mean what they appear to.
351 #define SVN_ERR(expr) \
353 svn_error_t *svn_err__temp = (expr); \
355 return svn_error_trace(svn_err__temp); \
359 * A macro for wrapping an error in a source-location trace message.
361 * This macro can be used when directly returning an already created
362 * error (when not using SVN_ERR, svn_error_create(), etc.) to ensure
363 * that the call stack is recorded correctly.
367 #ifdef SVN_ERR__TRACING
369 svn_error__trace(const char *file, long line, svn_error_t *err);
371 #define svn_error_trace(expr) svn_error__trace(__FILE__, __LINE__, (expr))
373 #define svn_error_trace(expr) (expr)
377 * Returns an error chain that is based on @a err's error chain but
378 * does not include any error tracing placeholders. @a err is not
379 * modified, except for any allocations using its pool.
381 * The returned error chain is allocated from @a err's pool and shares
382 * its message and source filename character arrays. The returned
383 * error chain should *not* be cleared because it is not a fully
384 * fledged error chain, only clearing @a err should be done to clear
385 * the returned error chain. If @a err is cleared, then the returned
386 * error chain is unusable.
388 * @a err can be #SVN_NO_ERROR. If @a err is not #SVN_NO_ERROR, then
389 * the last link in the error chain must be a non-tracing error, i.e,
394 svn_error_t *svn_error_purge_tracing(svn_error_t *err);
397 /** A statement macro, very similar to @c SVN_ERR.
399 * This macro will wrap the error with the specified text before
400 * returning the error.
402 #define SVN_ERR_W(expr, wrap_msg) \
404 svn_error_t *svn_err__temp = (expr); \
406 return svn_error_quick_wrap(svn_err__temp, wrap_msg); \
410 /** A statement macro intended for the main() function of the 'svn' program.
412 * Evaluate @a expr. If it yields an error, display the error on stdout
413 * and return @c EXIT_FAILURE.
415 * @note Not for use in the library, as it prints to stderr. This macro
416 * no longer suits the needs of the 'svn' program, and is not generally
417 * suitable for third-party use as it assumes the program name is 'svn'.
419 * @deprecated Provided for backward compatibility with the 1.8 API. Consider
420 * using svn_handle_error2() or svn_cmdline_handle_exit_error() instead.
422 #define SVN_INT_ERR(expr) \
424 svn_error_t *svn_err__temp = (expr); \
425 if (svn_err__temp) { \
426 svn_handle_error2(svn_err__temp, stderr, FALSE, "svn: "); \
427 svn_error_clear(svn_err__temp); \
428 return EXIT_FAILURE; } \
436 * @defgroup svn_error_error_groups Error groups
441 * Return TRUE if @a err is an error specifically related to locking a
442 * path in the repository, FALSE otherwise.
444 * SVN_ERR_FS_OUT_OF_DATE and SVN_ERR_FS_NOT_FOUND are in here because it's a
445 * non-fatal error that can be thrown when attempting to lock an item.
447 * SVN_ERR_REPOS_HOOK_FAILURE refers to the pre-lock hook.
451 #define SVN_ERR_IS_LOCK_ERROR(err) \
452 (err->apr_err == SVN_ERR_FS_PATH_ALREADY_LOCKED || \
453 err->apr_err == SVN_ERR_FS_NOT_FOUND || \
454 err->apr_err == SVN_ERR_FS_OUT_OF_DATE || \
455 err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN || \
456 err->apr_err == SVN_ERR_REPOS_HOOK_FAILURE || \
457 err->apr_err == SVN_ERR_FS_NO_SUCH_REVISION || \
458 err->apr_err == SVN_ERR_FS_OUT_OF_DATE || \
459 err->apr_err == SVN_ERR_FS_NOT_FILE)
462 * Return TRUE if @a err is an error specifically related to unlocking
463 * a path in the repository, FALSE otherwise.
465 * SVN_ERR_REPOS_HOOK_FAILURE refers to the pre-unlock hook.
469 #define SVN_ERR_IS_UNLOCK_ERROR(err) \
470 (err->apr_err == SVN_ERR_FS_PATH_NOT_LOCKED || \
471 err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN || \
472 err->apr_err == SVN_ERR_FS_LOCK_OWNER_MISMATCH || \
473 err->apr_err == SVN_ERR_FS_NO_SUCH_LOCK || \
474 err->apr_err == SVN_ERR_RA_NOT_LOCKED || \
475 err->apr_err == SVN_ERR_FS_LOCK_EXPIRED || \
476 err->apr_err == SVN_ERR_REPOS_HOOK_FAILURE)
478 /** Evaluates to @c TRUE iff @a apr_err (of type apr_status_t) is in the given
479 * @a category, which should be one of the @c SVN_ERR_*_CATEGORY_START
484 #define SVN_ERROR_IN_CATEGORY(apr_err, category) \
485 ((category) == ((apr_err) / SVN_ERR_CATEGORY_SIZE) * SVN_ERR_CATEGORY_SIZE)
491 /** Internal malfunctions and assertions
493 * @defgroup svn_error_malfunction_assertion Malfunctions and assertions
497 /** Report that an internal malfunction has occurred, and possibly terminate
500 * Act as determined by the current "malfunction handler" which may have
501 * been specified by a call to svn_error_set_malfunction_handler() or else
502 * is the default handler as specified in that function's documentation. If
503 * the malfunction handler returns, then cause the function using this macro
504 * to return the error object that it generated.
506 * @note The intended use of this macro is where execution reaches a point
507 * that cannot possibly be reached unless there is a bug in the program.
511 #define SVN_ERR_MALFUNCTION() \
513 return svn_error_trace(svn_error__malfunction( \
514 TRUE, __FILE__, __LINE__, NULL)); \
517 /** Similar to SVN_ERR_MALFUNCTION(), but without the option of returning
518 * an error to the calling function.
520 * If possible you should use SVN_ERR_MALFUNCTION() instead.
524 #define SVN_ERR_MALFUNCTION_NO_RETURN() \
526 svn_error__malfunction(FALSE, __FILE__, __LINE__, NULL); \
530 /** Like SVN_ERR_ASSERT(), but append ERR to the returned error chain.
532 * If EXPR is false, return a malfunction error whose chain includes ERR.
533 * If EXPR is true, do nothing. (In particular, this does not clear ERR.)
535 * Types: (svn_boolean_t expr, svn_error_t *err)
539 #ifdef __clang_analyzer__
541 /* Just ignore ERR. If the assert triggers, it'll be our least concern. */
542 #define SVN_ERR_ASSERT_E(expr, err) assert((expr))
544 #define SVN_ERR_ASSERT_E(expr, err) \
547 return svn_error_compose_create( \
548 svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr), \
555 /** Check that a condition is true: if not, report an error and possibly
556 * terminate the program.
558 * If the Boolean expression @a expr is true, do nothing. Otherwise,
559 * act as determined by the current "malfunction handler" which may have
560 * been specified by a call to svn_error_set_malfunction_handler() or else
561 * is the default handler as specified in that function's documentation. If
562 * the malfunction handler returns, then cause the function using this macro
563 * to return the error object that it generated.
565 * @note The intended use of this macro is to check a condition that cannot
566 * possibly be false unless there is a bug in the program.
568 * @note The condition to be checked should not be computationally expensive
569 * if it is reached often, as, unlike traditional "assert" statements, the
570 * evaluation of this expression is not compiled out in release-mode builds.
574 * @see SVN_ERR_ASSERT_E()
576 #ifdef __clang_analyzer__
578 #define SVN_ERR_ASSERT(expr) assert((expr))
580 #define SVN_ERR_ASSERT(expr) \
583 SVN_ERR(svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr)); \
587 /** Similar to SVN_ERR_ASSERT(), but without the option of returning
588 * an error to the calling function.
590 * If possible you should use SVN_ERR_ASSERT() instead.
594 #define SVN_ERR_ASSERT_NO_RETURN(expr) \
597 svn_error__malfunction(FALSE, __FILE__, __LINE__, #expr); \
602 /** Report a "Not implemented" malfunction. Internal use only. */
603 #define SVN__NOT_IMPLEMENTED() \
604 return svn_error__malfunction(TRUE, __FILE__, __LINE__, "Not implemented.")
606 /** A helper function for the macros that report malfunctions. Handle a
607 * malfunction by calling the current "malfunction handler" which may have
608 * been specified by a call to svn_error_set_malfunction_handler() or else
609 * is the default handler as specified in that function's documentation.
611 * Pass all of the parameters to the handler. The error occurred in the
612 * source file @a file at line @a line, and was an assertion failure of the
613 * expression @a expr, or, if @a expr is null, an unconditional error.
615 * If @a can_return is true, the handler can return an error object
616 * that is returned by the caller. If @a can_return is false the
617 * method should never return. (The caller will call abort())
622 svn_error__malfunction(svn_boolean_t can_return,
627 /** A type of function that handles an assertion failure or other internal
628 * malfunction detected within the Subversion libraries.
630 * The error occurred in the source file @a file at line @a line, and was an
631 * assertion failure of the expression @a expr, or, if @a expr is null, an
632 * unconditional error.
634 * If @a can_return is false a function of this type must never return.
636 * If @a can_return is true a function of this type must do one of:
637 * - Return an error object describing the error, using an error code in
638 * the category SVN_ERR_MALFUNC_CATEGORY_START.
641 * The function may alter its behaviour according to compile-time
642 * and run-time and even interactive conditions.
644 * @see SVN_ERROR_IN_CATEGORY()
648 typedef svn_error_t *(*svn_error_malfunction_handler_t)
649 (svn_boolean_t can_return, const char *file, int line, const char *expr);
651 /** Cause subsequent malfunctions to be handled by @a func.
652 * Return the handler that was previously in effect.
654 * @a func may not be null.
656 * @note The default handler is svn_error_abort_on_malfunction().
658 * @note This function must be called in a single-threaded context.
662 svn_error_malfunction_handler_t
663 svn_error_set_malfunction_handler(svn_error_malfunction_handler_t func);
665 /** Return the malfunction handler that is currently in effect.
666 * @since New in 1.9. */
667 svn_error_malfunction_handler_t
668 svn_error_get_malfunction_handler(void);
670 /** Handle a malfunction by returning an error object that describes it.
672 * When @a can_return is false, abort()
674 * This function implements @c svn_error_malfunction_handler_t.
679 svn_error_raise_on_malfunction(svn_boolean_t can_return,
684 /** Handle a malfunction by printing a message to stderr and aborting.
686 * This function implements @c svn_error_malfunction_handler_t.
691 svn_error_abort_on_malfunction(svn_boolean_t can_return,
701 #endif /* __cplusplus */
703 #endif /* SVN_ERROR_H */