context = $context; } /** * Get error code * * @since 1.0.0 * @return string Error code. */ public function get_error_code(): string { return $this->error_code; } /** * Get error context * * @since 1.0.0 * @return array Error context. */ public function get_context(): array { return $this->context; } /** * Set error context * * @since 1.0.0 * @param array $context Error context. * @return void. */ public function set_context( array $context ): void { $this->context = $context; } /** * Add context data * * @since 1.0.0 * @param string $key Context key. * @param mixed $value Context value. * @return void. */ public function add_context( string $key, $value ): void { $this->context[ $key ] = $value; } /** * Get formatted error message * * @since 1.0.0 * @return string Formatted error message. */ public function get_formatted_message(): string { $message = sprintf( '[%s] %s', $this->get_error_code(), $this->getMessage() ); if ( ! empty( $this->context ) ) { $message .= ' Context: ' . wp_json_encode( $this->context ); } return $message; } /** * Log the exception * * @since 1.0.0 * @return void. */ public function log(): void { if ( function_exists( 'accessibility_audio_tts_error_log' ) ) { accessibility_audio_tts_error_log( $this->get_formatted_message() ); } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // Only log in debug mode if accessibility_audio_tts_error_log is not available. error_log( $this->get_formatted_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } } }