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 /** 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.)
173 svn_error_quick_wrap(svn_error_t *child,
174 const char *new_msg);
176 /** A quick n' easy way to create a wrapped exception with your own
177 * printf-style error message produced by passing @a fmt, using
178 * apr_psprintf(), before throwing it up the stack. (It uses all of the
179 * @a child's fields.)
184 svn_error_quick_wrapf(svn_error_t *child,
187 __attribute__((format(printf, 2, 3)));
189 /** Compose two errors, returning the composition as a brand new error
190 * and consuming the original errors. Either or both of @a err1 and
191 * @a err2 may be @c SVN_NO_ERROR. If both are not @c SVN_NO_ERROR,
192 * @a err2 will follow @a err1 in the chain of the returned error.
194 * Either @a err1 or @a err2 can be functions that return svn_error_t*
195 * but if both are functions they can be evaluated in either order as
196 * per the C language rules.
201 svn_error_compose_create(svn_error_t *err1,
204 /** Add @a new_err to the end of @a chain's chain of errors. The @a new_err
205 * chain will be copied into @a chain's pool and destroyed, so @a new_err
206 * itself becomes invalid after this function.
208 * Either @a chain or @a new_err can be functions that return svn_error_t*
209 * but if both are functions they can be evaluated in either order as
210 * per the C language rules.
213 svn_error_compose(svn_error_t *chain,
214 svn_error_t *new_err);
216 /** Return the root cause of @a err by finding the last error in its
217 * chain (e.g. it or its children). @a err may be @c SVN_NO_ERROR, in
218 * which case @c SVN_NO_ERROR is returned. The returned error should
219 * @em not be cleared as it shares memory with @a err.
224 svn_error_root_cause(svn_error_t *err);
226 /** Return the first error in @a err's chain that has an error code @a
227 * apr_err or #SVN_NO_ERROR if there is no error with that code. The
228 * returned error should @em not be cleared as it shares memory with @a err.
230 * If @a err is #SVN_NO_ERROR, return #SVN_NO_ERROR.
235 svn_error_find_cause(svn_error_t *err, apr_status_t apr_err);
237 /** Create a new error that is a deep copy of @a err and return it.
242 svn_error_dup(const svn_error_t *err);
244 /** Free the memory used by @a error, as well as all ancestors and
245 * descendants of @a error.
247 * Unlike other Subversion objects, errors are managed explicitly; you
248 * MUST clear an error if you are ignoring it, or you are leaking memory.
249 * For convenience, @a error may be @c NULL, in which case this function does
250 * nothing; thus, svn_error_clear(svn_foo(...)) works as an idiom to
254 svn_error_clear(svn_error_t *error);
257 #if defined(SVN_ERR__TRACING)
258 /** Set the error location for debug mode. */
260 svn_error__locate(const char *file,
263 /* Wrapper macros to collect file and line information */
264 #define svn_error_create \
265 (svn_error__locate(__FILE__,__LINE__), (svn_error_create))
266 #define svn_error_createf \
267 (svn_error__locate(__FILE__,__LINE__), (svn_error_createf))
268 #define svn_error_wrap_apr \
269 (svn_error__locate(__FILE__,__LINE__), (svn_error_wrap_apr))
270 #define svn_error_quick_wrap \
271 (svn_error__locate(__FILE__,__LINE__), (svn_error_quick_wrap))
272 #define svn_error_quick_wrapf \
273 (svn_error__locate(__FILE__,__LINE__), (svn_error_quick_wrapf))
278 * Very basic default error handler: print out error stack @a error to the
279 * stdio stream @a stream, with each error prefixed by @a prefix; quit and
280 * clear @a error iff the @a fatal flag is set. Allocations are performed
281 * in the @a error's pool.
283 * If you're not sure what prefix to pass, just pass "svn: ". That's
284 * what code that used to call svn_handle_error() and now calls
285 * svn_handle_error2() does.
287 * Note that this should only be used from commandline specific code, or
288 * code that knows that @a stream is really where the application wants
289 * to receive its errors on.
294 svn_handle_error2(svn_error_t *error,
299 /** Like svn_handle_error2() but with @c prefix set to "svn: "
301 * @deprecated Provided for backward compatibility with the 1.1 API.
305 svn_handle_error(svn_error_t *error,
307 svn_boolean_t fatal);
310 * Very basic default warning handler: print out the error @a error to the
311 * stdio stream @a stream, prefixed by @a prefix. Allocations are
312 * performed in the error's pool.
314 * @a error may not be @c NULL.
316 * @note This does not clear @a error.
321 svn_handle_warning2(FILE *stream,
322 const svn_error_t *error,
325 /** Like svn_handle_warning2() but with @c prefix set to "svn: "
327 * @deprecated Provided for backward compatibility with the 1.1 API.
331 svn_handle_warning(FILE *stream,
335 /** A statement macro for checking error values.
337 * Evaluate @a expr. If it yields an error, return that error from the
338 * current function. Otherwise, continue.
340 * The <tt>do { ... } while (0)</tt> wrapper has no semantic effect,
341 * but it makes this macro syntactically equivalent to the expression
342 * statement it resembles. Without it, statements like
346 * SVN_ERR(some operation);
351 * would not mean what they appear to.
353 #define SVN_ERR(expr) \
355 svn_error_t *svn_err__temp = (expr); \
357 return svn_error_trace(svn_err__temp); \
361 * A macro for wrapping an error in a source-location trace message.
363 * This macro can be used when directly returning an already created
364 * error (when not using SVN_ERR, svn_error_create(), etc.) to ensure
365 * that the call stack is recorded correctly.
369 #ifdef SVN_ERR__TRACING
371 svn_error__trace(const char *file, long line, svn_error_t *err);
373 #define svn_error_trace(expr) svn_error__trace(__FILE__, __LINE__, (expr))
375 #define svn_error_trace(expr) (expr)
379 * Returns an error chain that is based on @a err's error chain but
380 * does not include any error tracing placeholders. @a err is not
381 * modified, except for any allocations using its pool.
383 * The returned error chain is allocated from @a err's pool and shares
384 * its message and source filename character arrays. The returned
385 * error chain should *not* be cleared because it is not a fully
386 * fledged error chain, only clearing @a err should be done to clear
387 * the returned error chain. If @a err is cleared, then the returned
388 * error chain is unusable.
390 * @a err can be #SVN_NO_ERROR. If @a err is not #SVN_NO_ERROR, then
391 * the last link in the error chain must be a non-tracing error, i.e,
396 svn_error_t *svn_error_purge_tracing(svn_error_t *err);
399 /** A statement macro, very similar to @c SVN_ERR.
401 * This macro will wrap the error with the specified text before
402 * returning the error.
404 #define SVN_ERR_W(expr, wrap_msg) \
406 svn_error_t *svn_err__temp = (expr); \
408 return svn_error_quick_wrap(svn_err__temp, wrap_msg); \
412 /** A statement macro intended for the main() function of the 'svn' program.
414 * Evaluate @a expr. If it yields an error, display the error on stdout
415 * and return @c EXIT_FAILURE.
417 * @note Not for use in the library, as it prints to stderr. This macro
418 * no longer suits the needs of the 'svn' program, and is not generally
419 * suitable for third-party use as it assumes the program name is 'svn'.
421 * @deprecated Provided for backward compatibility with the 1.8 API. Consider
422 * using svn_handle_error2() or svn_cmdline_handle_exit_error() instead.
424 #define SVN_INT_ERR(expr) \
426 svn_error_t *svn_err__temp = (expr); \
427 if (svn_err__temp) { \
428 svn_handle_error2(svn_err__temp, stderr, FALSE, "svn: "); \
429 svn_error_clear(svn_err__temp); \
430 return EXIT_FAILURE; } \
438 * @defgroup svn_error_error_groups Error groups
443 * Return TRUE if @a err is an error specifically related to locking a
444 * path in the repository, FALSE otherwise.
446 * SVN_ERR_FS_OUT_OF_DATE and SVN_ERR_FS_NOT_FOUND are in here because it's a
447 * non-fatal error that can be thrown when attempting to lock an item.
449 * SVN_ERR_REPOS_HOOK_FAILURE refers to the pre-lock hook.
453 #define SVN_ERR_IS_LOCK_ERROR(err) \
454 (err->apr_err == SVN_ERR_FS_PATH_ALREADY_LOCKED || \
455 err->apr_err == SVN_ERR_FS_NOT_FOUND || \
456 err->apr_err == SVN_ERR_FS_OUT_OF_DATE || \
457 err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN || \
458 err->apr_err == SVN_ERR_REPOS_HOOK_FAILURE || \
459 err->apr_err == SVN_ERR_FS_NO_SUCH_REVISION || \
460 err->apr_err == SVN_ERR_FS_OUT_OF_DATE || \
461 err->apr_err == SVN_ERR_FS_NOT_FILE)
464 * Return TRUE if @a err is an error specifically related to unlocking
465 * a path in the repository, FALSE otherwise.
467 * SVN_ERR_REPOS_HOOK_FAILURE refers to the pre-unlock hook.
471 #define SVN_ERR_IS_UNLOCK_ERROR(err) \
472 (err->apr_err == SVN_ERR_FS_PATH_NOT_LOCKED || \
473 err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN || \
474 err->apr_err == SVN_ERR_FS_LOCK_OWNER_MISMATCH || \
475 err->apr_err == SVN_ERR_FS_NO_SUCH_LOCK || \
476 err->apr_err == SVN_ERR_RA_NOT_LOCKED || \
477 err->apr_err == SVN_ERR_FS_LOCK_EXPIRED || \
478 err->apr_err == SVN_ERR_REPOS_HOOK_FAILURE)
480 /** Evaluates to @c TRUE iff @a apr_err (of type apr_status_t) is in the given
481 * @a category, which should be one of the @c SVN_ERR_*_CATEGORY_START
486 #define SVN_ERROR_IN_CATEGORY(apr_err, category) \
487 ((category) == ((apr_err) / SVN_ERR_CATEGORY_SIZE) * SVN_ERR_CATEGORY_SIZE)
493 /** Internal malfunctions and assertions
495 * @defgroup svn_error_malfunction_assertion Malfunctions and assertions
499 /** Report that an internal malfunction has occurred, and possibly terminate
502 * Act as determined by the current "malfunction handler" which may have
503 * been specified by a call to svn_error_set_malfunction_handler() or else
504 * is the default handler as specified in that function's documentation. If
505 * the malfunction handler returns, then cause the function using this macro
506 * to return the error object that it generated.
508 * @note The intended use of this macro is where execution reaches a point
509 * that cannot possibly be reached unless there is a bug in the program.
513 #define SVN_ERR_MALFUNCTION() \
515 return svn_error_trace(svn_error__malfunction( \
516 TRUE, __FILE__, __LINE__, NULL)); \
519 /** Similar to SVN_ERR_MALFUNCTION(), but without the option of returning
520 * an error to the calling function.
522 * If possible you should use SVN_ERR_MALFUNCTION() instead.
526 #define SVN_ERR_MALFUNCTION_NO_RETURN() \
528 svn_error__malfunction(FALSE, __FILE__, __LINE__, NULL); \
532 /** Like SVN_ERR_ASSERT(), but append ERR to the returned error chain.
534 * If EXPR is false, return a malfunction error whose chain includes ERR.
535 * If EXPR is true, do nothing. (In particular, this does not clear ERR.)
537 * Types: (svn_boolean_t expr, svn_error_t *err)
541 #ifdef __clang_analyzer__
543 /* Just ignore ERR. If the assert triggers, it'll be our least concern. */
544 #define SVN_ERR_ASSERT_E(expr, err) assert((expr))
546 #define SVN_ERR_ASSERT_E(expr, err) \
549 return svn_error_compose_create( \
550 svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr), \
557 /** Check that a condition is true: if not, report an error and possibly
558 * terminate the program.
560 * If the Boolean expression @a expr is true, do nothing. Otherwise,
561 * act as determined by the current "malfunction handler" which may have
562 * been specified by a call to svn_error_set_malfunction_handler() or else
563 * is the default handler as specified in that function's documentation. If
564 * the malfunction handler returns, then cause the function using this macro
565 * to return the error object that it generated.
567 * @note The intended use of this macro is to check a condition that cannot
568 * possibly be false unless there is a bug in the program.
570 * @note The condition to be checked should not be computationally expensive
571 * if it is reached often, as, unlike traditional "assert" statements, the
572 * evaluation of this expression is not compiled out in release-mode builds.
576 * @see SVN_ERR_ASSERT_E()
578 #ifdef __clang_analyzer__
580 #define SVN_ERR_ASSERT(expr) assert((expr))
582 #define SVN_ERR_ASSERT(expr) \
585 SVN_ERR(svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr)); \
589 /** Similar to SVN_ERR_ASSERT(), but without the option of returning
590 * an error to the calling function.
592 * If possible you should use SVN_ERR_ASSERT() instead.
596 #define SVN_ERR_ASSERT_NO_RETURN(expr) \
599 svn_error__malfunction(FALSE, __FILE__, __LINE__, #expr); \
604 /** Report a "Not implemented" malfunction. Internal use only. */
605 #define SVN__NOT_IMPLEMENTED() \
606 return svn_error__malfunction(TRUE, __FILE__, __LINE__, "Not implemented.")
608 /** A helper function for the macros that report malfunctions. Handle a
609 * malfunction by calling the current "malfunction handler" which may have
610 * been specified by a call to svn_error_set_malfunction_handler() or else
611 * is the default handler as specified in that function's documentation.
613 * Pass all of the parameters to the handler. The error occurred in the
614 * source file @a file at line @a line, and was an assertion failure of the
615 * expression @a expr, or, if @a expr is null, an unconditional error.
617 * If @a can_return is true, the handler can return an error object
618 * that is returned by the caller. If @a can_return is false the
619 * method should never return. (The caller will call abort())
624 svn_error__malfunction(svn_boolean_t can_return,
629 /** A type of function that handles an assertion failure or other internal
630 * malfunction detected within the Subversion libraries.
632 * The error occurred in the source file @a file at line @a line, and was an
633 * assertion failure of the expression @a expr, or, if @a expr is null, an
634 * unconditional error.
636 * If @a can_return is false a function of this type must never return.
638 * If @a can_return is true a function of this type must do one of:
639 * - Return an error object describing the error, using an error code in
640 * the category SVN_ERR_MALFUNC_CATEGORY_START.
643 * The function may alter its behaviour according to compile-time
644 * and run-time and even interactive conditions.
646 * @see SVN_ERROR_IN_CATEGORY()
650 typedef svn_error_t *(*svn_error_malfunction_handler_t)
651 (svn_boolean_t can_return, const char *file, int line, const char *expr);
653 /** Cause subsequent malfunctions to be handled by @a func.
654 * Return the handler that was previously in effect.
656 * @a func may not be null.
658 * @note The default handler is svn_error_abort_on_malfunction().
660 * @note This function must be called in a single-threaded context.
664 svn_error_malfunction_handler_t
665 svn_error_set_malfunction_handler(svn_error_malfunction_handler_t func);
667 /** Return the malfunction handler that is currently in effect.
668 * @since New in 1.9. */
669 svn_error_malfunction_handler_t
670 svn_error_get_malfunction_handler(void);
672 /** Handle a malfunction by returning an error object that describes it.
674 * When @a can_return is false, abort()
676 * This function implements @c svn_error_malfunction_handler_t.
681 svn_error_raise_on_malfunction(svn_boolean_t can_return,
686 /** Handle a malfunction by printing a message to stderr and aborting.
688 * This function implements @c svn_error_malfunction_handler_t.
693 svn_error_abort_on_malfunction(svn_boolean_t can_return,
703 #endif /* __cplusplus */
705 #endif /* SVN_ERROR_H */