Settings: Saving proxy fails silently
[TortoiseGit.git] / src / TortoiseMerge / svninclude / svn_error.h
blob4e8524b4d8acc21f9dd078ab01b5dc2ac0b80e9b
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
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
19 * under the License.
20 * ====================================================================
21 * @endcopyright
23 * @file svn_error.h
24 * @brief Common exception handling for Subversion.
27 #ifndef SVN_ERROR_H
28 #define SVN_ERROR_H
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
36 #endif
37 #include <apr_want.h> /* for FILE* */
39 #include "svn_types.h"
41 #ifdef __cplusplus
42 extern "C" {
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. */
48 #ifdef SVN_DEBUG
49 #define SVN_ERR__TRACING
50 #endif
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.
63 char *
64 svn_strerror(apr_status_t statcode,
65 char *buf,
66 apr_size_t bufsize);
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.
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
87 * in the future.
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.
93 * @since New in 1.8.
95 const char *
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.
103 * @since New in 1.4.
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(svn_error_t *err,
109 char *buf,
110 apr_size_t bufsize);
114 /** SVN error creation and destruction.
116 * @defgroup svn_error_error_creation_destroy Error creation and destruction
117 * @{
120 /** Create a nested exception structure.
122 * Input: an APR or SVN custom error code,
123 * a "child" error to wrap,
124 * a specific message
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.
138 svn_error_t *
139 svn_error_create(apr_status_t apr_err,
140 svn_error_t *child,
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
145 * apr_psprintf().
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)));
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.)
162 svn_error_t *
163 svn_error_wrap_apr(apr_status_t status,
164 const char *fmt,
165 ...)
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.)
172 svn_error_t *
173 svn_error_quick_wrap(svn_error_t *child,
174 const char *new_msg);
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.
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.
185 * @since New in 1.6.
187 svn_error_t *
188 svn_error_compose_create(svn_error_t *err1,
189 svn_error_t *err2);
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.
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.
199 void
200 svn_error_compose(svn_error_t *chain,
201 svn_error_t *new_err);
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.
207 * @since New in 1.5.
209 svn_error_t *
210 svn_error_root_cause(svn_error_t *err);
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.
216 * If @a err is #SVN_NO_ERROR, return #SVN_NO_ERROR.
218 * @since New in 1.7.
220 svn_error_t *
221 svn_error_find_cause(svn_error_t *err, apr_status_t apr_err);
223 /** Create a new error that is a deep copy of @a err and return it.
225 * @since New in 1.2.
227 svn_error_t *
228 svn_error_dup(svn_error_t *err);
230 /** Free the memory used by @a error, as well as all ancestors and
231 * descendants of @a error.
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.
239 void
240 svn_error_clear(svn_error_t *error);
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);
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
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.
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.
271 * @since New in 1.2.
273 void
274 svn_handle_error2(svn_error_t *error,
275 FILE *stream,
276 svn_boolean_t fatal,
277 const char *prefix);
279 /** Like svn_handle_error2() but with @c prefix set to "svn: "
281 * @deprecated Provided for backward compatibility with the 1.1 API.
283 SVN_DEPRECATED
284 void
285 svn_handle_error(svn_error_t *error,
286 FILE *stream,
287 svn_boolean_t fatal);
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.
294 * @a error may not be @c NULL.
296 * @since New in 1.2.
298 void
299 svn_handle_warning2(FILE *stream,
300 svn_error_t *error,
301 const char *prefix);
303 /** Like svn_handle_warning2() but with @c prefix set to "svn: "
305 * @deprecated Provided for backward compatibility with the 1.1 API.
307 SVN_DEPRECATED
308 void
309 svn_handle_warning(FILE *stream,
310 svn_error_t *error);
313 /** A statement macro for checking error values.
315 * Evaluate @a expr. If it yields an error, return that error from the
316 * current function. Otherwise, continue.
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
322 * @code
323 * if (a)
324 * SVN_ERR(some operation);
325 * else
326 * foo;
327 * @endcode
329 * would not mean what they appear to.
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)
339 * A macro for wrapping an error in a source-location trace message.
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.
345 * @since New in 1.7.
347 #ifdef SVN_ERR__TRACING
348 svn_error_t *
349 svn_error__trace(const char *file, long line, svn_error_t *err);
351 #define svn_error_trace(expr) svn_error__trace(__FILE__, __LINE__, (expr))
352 #else
353 #define svn_error_trace(expr) (expr)
354 #endif
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.
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.
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.
372 * @since New in 1.7.
374 svn_error_t *svn_error_purge_tracing(svn_error_t *err);
377 /** A statement macro, very similar to @c SVN_ERR.
379 * This macro will wrap the error with the specified text before
380 * returning the error.
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)
390 /** A statement macro, similar to @c SVN_ERR, but returns an integer.
392 * Evaluate @a expr. If it yields an error, handle that error and
393 * return @c EXIT_FAILURE.
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)
404 /** @} */
407 /** Error groups
409 * @defgroup svn_error_error_groups Error groups
410 * @{
414 * Return TRUE if @a err is an error specifically related to locking a
415 * path in the repository, FALSE otherwise.
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.
420 * @since New in 1.2.
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)
429 * Return TRUE if @a err is an error specifically related to unlocking
430 * a path in the repository, FALSE otherwise.
432 * @since New in 1.2.
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)
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.
446 * @since New in 1.7.
448 #define SVN_ERROR_IN_CATEGORY(apr_err, category) \
449 ((category) == ((apr_err) / SVN_ERR_CATEGORY_SIZE) * SVN_ERR_CATEGORY_SIZE)
452 /** @} */
455 /** Internal malfunctions and assertions
457 * @defgroup svn_error_malfunction_assertion Malfunctions and assertions
458 * @{
461 /** Report that an internal malfunction has occurred, and possibly terminate
462 * the program.
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.
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.
473 * @since New in 1.6.
475 #define SVN_ERR_MALFUNCTION() \
476 do { \
477 return svn_error_trace(svn_error__malfunction( \
478 TRUE, __FILE__, __LINE__, NULL)); \
479 } while (0)
481 /** Similar to SVN_ERR_MALFUNCTION(), but without the option of returning
482 * an error to the calling function.
484 * If possible you should use SVN_ERR_MALFUNCTION() instead.
486 * @since New in 1.6.
488 #define SVN_ERR_MALFUNCTION_NO_RETURN() \
489 do { \
490 svn_error__malfunction(FALSE, __FILE__, __LINE__, NULL); \
491 abort(); \
492 } while (1)
494 /** Check that a condition is true: if not, report an error (appending
495 * ERR if non-NULL) and possibly terminate the program.
497 * If the boolean expression @a expr is true, do nothing. Otherwise,
498 * act as determined by the current "malfunction handler" which may have
499 * been specified by a call to svn_error_set_malfunction_handler() or else
500 * is the default handler as specified in that function's documentation. If
501 * the malfunction handler returns, then cause the function using this macro
502 * to return the error object that it generated.
504 * @note The intended use of this macro is to check a condition that cannot
505 * possibly be false unless there is a bug in the program.
507 * @note The condition to be checked should not be computationally expensive
508 * if it is reached often, as, unlike traditional "assert" statements, the
509 * evaluation of this expression is not compiled out in release-mode builds.
511 * Types: (svn_boolean_t expr, svn_error_t *err)
513 * @since New in 1.8.
515 #ifdef __clang_analyzer__
516 #include <assert.h>
517 /* Just ignore ERR. If the assert triggers, it'll be our least concern. */
518 #define SVN_ERR_ASSERT_E(expr, err) assert((expr))
519 #else
520 #define SVN_ERR_ASSERT_E(expr, err) \
521 do { \
522 if (!(expr)) { \
523 return svn_error_compose_create( \
524 svn_error__malfunction(TRUE, __FILE__, __LINE__, #expr), \
525 (err)); \
527 } while (0)
528 #endif
530 /** Like SVN_ERR_ASSERT_E(), but with @a err always NULL.
532 * @see SVN_ERR_ASSERT_E()
534 * @since New in 1.6.
536 #define SVN_ERR_ASSERT(expr) SVN_ERR_ASSERT_E(expr, NULL)
538 /** Similar to SVN_ERR_ASSERT(), but without the option of returning
539 * an error to the calling function.
541 * If possible you should use SVN_ERR_ASSERT() instead.
543 * @since New in 1.6.
545 #define SVN_ERR_ASSERT_NO_RETURN(expr) \
546 do { \
547 if (!(expr)) { \
548 svn_error__malfunction(FALSE, __FILE__, __LINE__, #expr); \
549 abort(); \
551 } while (0)
553 /** Report a "Not implemented" malfunction. Internal use only. */
554 #define SVN__NOT_IMPLEMENTED() \
555 return svn_error__malfunction(TRUE, __FILE__, __LINE__, "Not implemented.")
557 /** A helper function for the macros that report malfunctions. Handle a
558 * malfunction by calling the current "malfunction handler" which may have
559 * been specified by a call to svn_error_set_malfunction_handler() or else
560 * is the default handler as specified in that function's documentation.
562 * Pass all of the parameters to the handler. The error occurred in the
563 * source file @a file at line @a line, and was an assertion failure of the
564 * expression @a expr, or, if @a expr is null, an unconditional error.
566 * If @a can_return is true, the handler can return an error object
567 * that is returned by the caller. If @a can_return is false the
568 * method should never return. (The caller will call abort())
570 * @since New in 1.6.
572 svn_error_t *
573 svn_error__malfunction(svn_boolean_t can_return,
574 const char *file,
575 int line,
576 const char *expr);
578 /** A type of function that handles an assertion failure or other internal
579 * malfunction detected within the Subversion libraries.
581 * The error occurred in the source file @a file at line @a line, and was an
582 * assertion failure of the expression @a expr, or, if @a expr is null, an
583 * unconditional error.
585 * If @a can_return is false a function of this type must never return.
587 * If @a can_return is true a function of this type must do one of:
588 * - Return an error object describing the error, using an error code in
589 * the category SVN_ERR_MALFUNC_CATEGORY_START.
590 * - Never return.
592 * The function may alter its behaviour according to compile-time
593 * and run-time and even interactive conditions.
595 * @see SVN_ERROR_IN_CATEGORY()
597 * @since New in 1.6.
599 typedef svn_error_t *(*svn_error_malfunction_handler_t)
600 (svn_boolean_t can_return, const char *file, int line, const char *expr);
602 /** Cause subsequent malfunctions to be handled by @a func.
603 * Return the handler that was previously in effect.
605 * @a func may not be null.
607 * @note The default handler is svn_error_abort_on_malfunction().
609 * @note This function must be called in a single-threaded context.
611 * @since New in 1.6.
613 svn_error_malfunction_handler_t
614 svn_error_set_malfunction_handler(svn_error_malfunction_handler_t func);
616 /** Handle a malfunction by returning an error object that describes it.
618 * When @a can_return is false, abort()
620 * This function implements @c svn_error_malfunction_handler_t.
622 * @since New in 1.6.
624 svn_error_t *
625 svn_error_raise_on_malfunction(svn_boolean_t can_return,
626 const char *file,
627 int line,
628 const char *expr);
630 /** Handle a malfunction by printing a message to stderr and aborting.
632 * This function implements @c svn_error_malfunction_handler_t.
634 * @since New in 1.6.
636 svn_error_t *
637 svn_error_abort_on_malfunction(svn_boolean_t can_return,
638 const char *file,
639 int line,
640 const char *expr);
642 /** @} */
645 #ifdef __cplusplus
647 #endif /* __cplusplus */
649 #endif /* SVN_ERROR_H */