pods_error

Error Handling which throws / displays errors.

Description

pods_error( (string|array) $error, (null) $obj = null ); 

Parameters (2)

0. $error (string|array)
The error message(s) to be thrown / displayed.
1. $obj — Optional. (null) => null
If $obj->display_errors is set and is set to true it will display errors, if boolean and is set to true it will display errors.

Usage

  1. if ( !function_exists( 'pods_error' ) ) { 
  2. require_once '/includes/general.php'; 
  3.  
  4. // The error message(s) to be thrown / displayed. 
  5. $error = null; 
  6.  
  7. // If $obj->display_errors is set and is set to true it will display errors, if boolean and is set to true it will display errors. 
  8. $obj = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pods_error($error, $obj); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/general.php  
  1. function pods_error( $error, $obj = null ) { 
  2.  
  3. global $pods_errors; 
  4.  
  5. $error_mode = 'exception'; 
  6.  
  7. if ( is_object( $obj ) && isset( $obj->display_errors ) ) { 
  8. if ( true === $obj->display_errors ) { 
  9. $error_mode = 'exit'; 
  10. } elseif ( false === $obj->display_errors ) { 
  11. $error_mode = 'exception'; 
  12. } else { 
  13. $error_mode = $obj->display_errors; 
  14. } elseif ( true === $obj ) { 
  15. $error_mode = 'exit'; 
  16. } elseif ( false === $obj ) { 
  17. $error_mode = 'exception'; 
  18. } elseif ( is_string( $obj ) ) { 
  19. $error_mode = $obj; 
  20.  
  21. if ( is_object( $error ) && 'Exception' === get_class( $error ) ) { 
  22. $error = $error->getMessage(); 
  23.  
  24. $error_mode = 'exception'; 
  25.  
  26. /** 
  27. * @var string $error_mode Throw an exception, exit with the message, return false, or return WP_Error 
  28. */ 
  29. if ( ! in_array( $error_mode, array( 'exception', 'exit', 'false', 'wp_error' ), true ) ) { 
  30. $error_mode = 'exception'; 
  31.  
  32. /** 
  33. * Filter the error mode used by pods_error. 
  34. * @param string $error_mode Error mode 
  35. * @param string|array $error Error message(s) 
  36. * @param object|boolean|string|null $obj 
  37. */ 
  38. $error_mode = apply_filters( 'pods_error_mode', $error_mode, $error, $obj ); 
  39.  
  40. if ( is_array( $error ) ) { 
  41. $error = array_map( 'wp_kses_post', $error ); 
  42.  
  43. if ( 1 === count( $error ) ) { 
  44. $error = current( $error ); 
  45.  
  46. // Create WP_Error for use later. 
  47. $wp_error = new WP_Error( 'pods-error-' . md5( $error ), $error ); 
  48. } else { 
  49. // Create WP_Error for use later. 
  50. $wp_error = new WP_Error(); 
  51.  
  52. foreach ( $error as $error_message ) { 
  53. $wp_error->add( 'pods-error-' . md5( $error_message ), $error_message ); 
  54.  
  55. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  56. $error = __( 'The following issue occurred:', 'pods' ) 
  57. . "\n\n- " . implode( "\n- ", $error ); 
  58. } else { 
  59. $error = __( 'The following issues occurred:', 'pods' ) 
  60. . "\n<ul><li>" . implode( "</li>\n<li>", $error ) . "</li></ul>"; 
  61. } else { 
  62. if ( is_object( $error ) ) { 
  63. $error = __( 'An unknown error has occurred', 'pods' ); 
  64.  
  65. $error = wp_kses_post( $error ); 
  66.  
  67. // Create WP_Error for use later. 
  68. $wp_error = new WP_Error( 'pods-error-' . md5( $error ), $error ); 
  69.  
  70. $last_error = $pods_errors; 
  71.  
  72. $pods_errors = array(); 
  73.  
  74. if ( $last_error === $error && 'exception' === $error_mode ) { 
  75. $error_mode = 'exit'; 
  76.  
  77. if ( ! empty( $error ) ) { 
  78. if ( 'exception' === $error_mode ) { 
  79. $exception_bypass = apply_filters( 'pods_error_exception', null, $error ); 
  80.  
  81. if ( null !== $exception_bypass ) { 
  82. return $exception_bypass; 
  83.  
  84. $pods_errors = $error; 
  85.  
  86. set_exception_handler( 'pods_error' ); 
  87.  
  88. throw new Exception( $error ); 
  89. } elseif ( 'exit' === $error_mode ) { 
  90. $die_bypass = apply_filters( 'pods_error_die', null, $error ); 
  91.  
  92. if ( null !== $die_bypass ) { 
  93. return $die_bypass; 
  94.  
  95. // die with error 
  96. if ( ! defined( 'DOING_AJAX' ) && ! headers_sent() && ( is_admin() || false !== strpos( $_SERVER['REQUEST_URI'], 'wp-comments-post.php' ) ) ) { 
  97. wp_die( $error ); 
  98. } else { 
  99. die( sprintf( '<e>%s</e>', $error ) ); 
  100. } elseif ( 'wp_error' === $error_mode ) { 
  101. return $wp_error; 
  102.  
  103. return false; 
  104.