ERROR.CONVENTIONS 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. The intl extension has particular conventions regarding error reporting.
  2. These conventions are enumerated in this document.
  3. :: The last error is always stored globally.
  4. The global error code can be obtained in userland with intl_get_error_code().
  5. This is a U_* error code defined by ICU, but it does not have necessarily to be
  6. returned obtained after a call to an ICU function. That is to say, the internal
  7. PHP wrapper functions can set these error codes when appropriate. For instance,
  8. in response to bad arguments (e.g. zend_parse_parameters() failure), the PHP
  9. wrapper function should set the global error code to U_ILLEGAL_ARGUMENT_ERROR).
  10. The error code (an integer) can be converter to the corresponding enum name
  11. string in userland with intl_error_name().
  12. The associated message can be obtained with intl_get_error_message(). This is a
  13. message set by the PHP wrapping code, not by ICU. The message should include the
  14. name of the function that failed in order to make debugging easier (though if
  15. you activate warnings with intl.error_level or exceptions with
  16. intl.use_exceptions you get more fine-grained information about where the
  17. error occurred).
  18. The internal PHP code can set the global last error with:
  19. void intl_error_set_code(intl_error* err, UErrorCode err_code TSRMLS_DC);
  20. void intl_error_set_custom_msg(intl_error* err, char* msg, int copyMsg TSRMLS_DC);
  21. void intl_error_set(intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC);
  22. and by passing NULL as the first parameter. The last function is a combination
  23. of the first two. If the message is not a static buffer, copyMsg should be 1.
  24. This makes the message string be copied and freed when no longer needed. There's
  25. no way to pass ownership of the string without it being copied.
  26. :: The last is ALSO stored in the object whose method call triggered the error,
  27. unless the error is due to bad arguments, in which case only the global error
  28. should be set
  29. Objects store an intl_error structed in their private data. For instance:
  30. typedef struct {
  31. zend_object zo;
  32. intl_error err;
  33. Calendar* ucal;
  34. } Calendar_object;
  35. The global error and the object error can be SIMULTANEOUSLY set with these
  36. functions:
  37. void intl_errors_set_custom_msg(intl_error* err, char* msg, int copyMsg TSRMLS_DC);
  38. void intl_errors_set_code(intl_error* err, UErrorCode err_code TSRMLS_DC);
  39. void intl_errors_set(intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC);
  40. by passing a pointer to the object's intl_error structed as the first parameter.
  41. Node the extra 's' in the functions' names ('errors', not 'error').
  42. Static methods should only set the global error.
  43. :: Intl classes that can be instantiated should provide ::getErrorCode() and
  44. getErrorMessage() methods
  45. These methods are used to retrieve the error codes stored in the object's
  46. private intl_error structured and mirror the global intl_get_error_code() and
  47. intl_get_error_message().
  48. :: Intl methods and functions should return FALSE on error (even argument
  49. parsing errors), not NULL. Constructors and factory methods are the
  50. exception; these should return NULL, not FALSE.
  51. Not that constructors in Intl generally (always?) don't throws exceptions.
  52. They instead destroy the object to that the result of new IntlClass() can
  53. be NULL. This may be surprising.
  54. :: Intl functions and methods should reset the global error before doing
  55. anything else (even parse the arguments); instance methods should also reset
  56. the object's private error
  57. Errors should be lost after a function call. This is different from the way
  58. ICU operates, where functions return immediately if an error is set.
  59. Error resetting can be done with:
  60. void intl_error_reset(NULL TSRMLS_DC); /* reset global error */
  61. void intl_errors_reset(intl_error* err TSRMLS_DC ); /* reset global and object error */
  62. In practice, intl_errors_reset() is not used because most classes have also
  63. plain functions mapped to the same internal functions as their instance methods.
  64. Fetching of the object is done with zend_parse_method_parameters() instead of
  65. directly using getThis(). Therefore, no reference to object is obtained until
  66. the arguments are fully parsed. Without a reference to the object, there's no
  67. way to reset the object's internal error code. Instead, resetting of the
  68. object's internal error code is done upon fetching the object from its zval.
  69. Example:
  70. U_CFUNC PHP_FUNCTION(breakiter_set_text)
  71. {
  72. /* ... variable declations ... */
  73. BREAKITER_METHOD_INIT_VARS; /* macro also resets global error */
  74. object = getThis();
  75. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  76. &text, &text_len) == FAILURE) {
  77. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  78. "breakiter_set_text: bad arguments", 0 TSRMLS_CC);
  79. RETURN_FALSE;
  80. }
  81. /* ... */
  82. BREAKITER_METHOD_FETCH_OBJECT; /* macro also resets object's error */
  83. /* ... */
  84. }
  85. Implementations of ::getErrorCode() and ::getErrorMessage() should not reset the
  86. object's error code.