exception.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBHDFS_EXCEPTION_H
  19. #define LIBHDFS_EXCEPTION_H
  20. /**
  21. * Exception handling routines for libhdfs.
  22. *
  23. * The convention we follow here is to clear pending exceptions as soon as they
  24. * are raised. Never assume that the caller of your function will clean up
  25. * after you-- do it yourself. Unhandled exceptions can lead to memory leaks
  26. * and other undefined behavior.
  27. *
  28. * If you encounter an exception, return a local reference to it. The caller is
  29. * responsible for freeing the local reference, by calling a function like
  30. * PrintExceptionAndFree. (You can also free exceptions directly by calling
  31. * DeleteLocalRef. However, that would not produce an error message, so it's
  32. * usually not what you want.)
  33. */
  34. #include "common/platform.h"
  35. #include <jni.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <search.h>
  40. #include <errno.h>
  41. /**
  42. * Exception noprint flags
  43. *
  44. * Theses flags determine which exceptions should NOT be printed to stderr by
  45. * the exception printing routines. For example, if you expect to see
  46. * FileNotFound, you might use NOPRINT_EXC_FILE_NOT_FOUND, to avoid filling the
  47. * logs with messages about routine events.
  48. *
  49. * On the other hand, if you don't expect any failures, you might pass
  50. * PRINT_EXC_ALL.
  51. *
  52. * You can OR these flags together to avoid printing multiple classes of
  53. * exceptions.
  54. */
  55. #define PRINT_EXC_ALL 0x00
  56. #define NOPRINT_EXC_FILE_NOT_FOUND 0x01
  57. #define NOPRINT_EXC_ACCESS_CONTROL 0x02
  58. #define NOPRINT_EXC_UNRESOLVED_LINK 0x04
  59. #define NOPRINT_EXC_PARENT_NOT_DIRECTORY 0x08
  60. #define NOPRINT_EXC_ILLEGAL_ARGUMENT 0x10
  61. /**
  62. * Get information about an exception.
  63. *
  64. * @param excName The Exception name.
  65. * This is a Java class name in JNI format.
  66. * @param noPrintFlags Flags which determine which exceptions we should NOT
  67. * print.
  68. * @param excErrno (out param) The POSIX error number associated with the
  69. * exception.
  70. * @param shouldPrint (out param) Nonzero if we should print this exception,
  71. * based on the noPrintFlags and its name.
  72. */
  73. void getExceptionInfo(const char *excName, int noPrintFlags,
  74. int *excErrno, int *shouldPrint);
  75. /**
  76. * Print out information about an exception and free it.
  77. *
  78. * @param env The JNI environment
  79. * @param exc The exception to print and free
  80. * @param noPrintFlags Flags which determine which exceptions we should NOT
  81. * print.
  82. * @param fmt Printf-style format list
  83. * @param ap Printf-style varargs
  84. *
  85. * @return The POSIX error number associated with the exception
  86. * object.
  87. */
  88. int printExceptionAndFreeV(JNIEnv *env, jthrowable exc, int noPrintFlags,
  89. const char *fmt, va_list ap);
  90. /**
  91. * Print out information about an exception and free it.
  92. *
  93. * @param env The JNI environment
  94. * @param exc The exception to print and free
  95. * @param noPrintFlags Flags which determine which exceptions we should NOT
  96. * print.
  97. * @param fmt Printf-style format list
  98. * @param ... Printf-style varargs
  99. *
  100. * @return The POSIX error number associated with the exception
  101. * object.
  102. */
  103. int printExceptionAndFree(JNIEnv *env, jthrowable exc, int noPrintFlags,
  104. const char *fmt, ...) TYPE_CHECKED_PRINTF_FORMAT(4, 5);
  105. /**
  106. * Print out information about the pending exception and free it.
  107. *
  108. * @param env The JNI environment
  109. * @param noPrintFlags Flags which determine which exceptions we should NOT
  110. * print.
  111. * @param fmt Printf-style format list
  112. * @param ... Printf-style varargs
  113. *
  114. * @return The POSIX error number associated with the exception
  115. * object.
  116. */
  117. int printPendingExceptionAndFree(JNIEnv *env, int noPrintFlags,
  118. const char *fmt, ...) TYPE_CHECKED_PRINTF_FORMAT(3, 4);
  119. /**
  120. * Get a local reference to the pending exception and clear it.
  121. *
  122. * Once it is cleared, the exception will no longer be pending. The caller will
  123. * have to decide what to do with the exception object.
  124. *
  125. * @param env The JNI environment
  126. *
  127. * @return The exception, or NULL if there was no exception
  128. */
  129. jthrowable getPendingExceptionAndClear(JNIEnv *env);
  130. /**
  131. * Create a new runtime error.
  132. *
  133. * This creates (but does not throw) a new RuntimeError.
  134. *
  135. * @param env The JNI environment
  136. * @param fmt Printf-style format list
  137. * @param ... Printf-style varargs
  138. *
  139. * @return A local reference to a RuntimeError
  140. */
  141. jthrowable newRuntimeError(JNIEnv *env, const char *fmt, ...)
  142. TYPE_CHECKED_PRINTF_FORMAT(2, 3);
  143. #undef TYPE_CHECKED_PRINTF_FORMAT
  144. #endif