35 lines
1.0 KiB
PHP
35 lines
1.0 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
function json_response($success, $message, array $data = array(), $httpStatus = 200)
|
|
{
|
|
$CI =& get_instance();
|
|
$payload = array_merge(array(
|
|
'status' => (bool) $success,
|
|
'message' => (string) $message
|
|
), $data);
|
|
|
|
return $CI->output
|
|
->set_status_header((int) $httpStatus)
|
|
->set_content_type('application/json', 'utf-8')
|
|
->set_output(json_encode($payload));
|
|
}
|
|
|
|
function business_exception_response(Throwable $exception)
|
|
{
|
|
$status = $exception instanceof BusinessException ? $exception->getCode() : 500;
|
|
if ($status < 400 || $status > 599) {
|
|
$status = 500;
|
|
}
|
|
|
|
if (!($exception instanceof BusinessException)) {
|
|
log_message('error', get_class($exception) . ': ' . $exception->getMessage());
|
|
}
|
|
|
|
$message = $exception instanceof BusinessException
|
|
? $exception->getMessage()
|
|
: 'Terjadi kesalahan saat memproses transaksi.';
|
|
|
|
return json_response(false, $message, array(), $status);
|
|
}
|