paystack/libraries/Paystack_error_handler.php
2025-01-19 12:18:55 +00:00

112 lines
2.6 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Paystack_error_handler
{
protected $CI;
protected $errors = [];
public function __construct()
{
$this->CI = &get_instance();
}
/**
* Handle API errors
*/
public function handle_api_error($response, $context = '')
{
$error_data = [
'type' => 'api_error',
'context' => $context,
'message' => isset($response->message) ? $response->message : 'Unknown API error',
'code' => isset($response->code) ? $response->code : null,
'timestamp' => date('Y-m-d H:i:s')
];
$this->log_error($error_data);
return $error_data;
}
/**
* Handle validation errors
*/
public function handle_validation_error($errors, $context = '')
{
$error_data = [
'type' => 'validation_error',
'context' => $context,
'message' => is_array($errors) ? implode(', ', $errors) : $errors,
'timestamp' => date('Y-m-d H:i:s')
];
$this->log_error($error_data);
return $error_data;
}
/**
* Handle system errors
*/
public function handle_system_error($exception, $context = '')
{
$error_data = [
'type' => 'system_error',
'context' => $context,
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString(),
'timestamp' => date('Y-m-d H:i:s')
];
$this->log_error($error_data);
return $error_data;
}
/**
* Log error
*/
protected function log_error($error_data)
{
// Add to errors array
$this->errors[] = $error_data;
// Log to database
$this->CI->load->model('paystack_model');
$log_data = [
'message' => json_encode($error_data),
'log_type' => 'error'
];
$this->CI->paystack_model->add_payment_log($log_data);
// Log to system log if serious error
if ($error_data['type'] === 'system_error') {
log_message('error', 'Paystack Error: ' . $error_data['message']);
}
}
/**
* Get last error
*/
public function get_last_error()
{
return end($this->errors);
}
/**
* Get all errors
*/
public function get_all_errors()
{
return $this->errors;
}
/**
* Clear errors
*/
public function clear_errors()
{
$this->errors = [];
}
}