Skip to content

Commit

Permalink
Merge pull request #31 from nguyenanhung/v3.2.0-develop
Browse files Browse the repository at this point in the history
V3.2.0 develop
  • Loading branch information
nguyenanhung committed Mar 29, 2023
2 parents 9b56b8f + 5305b27 commit 80cba82
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 36 deletions.
32 changes: 32 additions & 0 deletions helpers/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ function bear_str_length($str)
return strlen($str);
}
}
if (!function_exists('__get_error_message__')) {
/**
* Function __get_error_message__
*
* @param \Exception|\Throwable $e
*
* @return string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 29/03/2023 53:17
*/
function __get_error_message__($e)
{
return "Error Code: " . $e->getCode() . " - File: " . $e->getFile() . " - Line: " . $e->getLine() . " - Message: " . $e->getMessage();
}
}
if (!function_exists('__get_error_trace__')) {
/**
* Function __get_error_trace__
*
* @param \Exception|\Throwable $e
*
* @return string
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 29/03/2023 53:48
*/
function __get_error_trace__($e)
{
return "Error Trace: " . $e->getTraceAsString();
}
}
15 changes: 7 additions & 8 deletions hungng/HungNG_CI_Base_Controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ protected function jsonResponsePretty($response = array(), $status = 200)
*/
protected function errorExceptionResponse($exception)
{
$errorMessage = "Error Code: " . $exception->getCode() . " - Error File: " . $exception->getFile() . " - Error Line: " . $exception->getLine() . " - Error Message: " . $exception->getMessage();
log_message('error', $errorMessage);
log_message('error', $exception->getTraceAsString());
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));

$response = array();
$response['code'] = StatusCodes::HTTP_BAD_REQUEST;
Expand Down Expand Up @@ -366,11 +365,11 @@ protected function log($name = '', $message = '', $context = array(), $inputLeve
log_message($inputLevel, $name . " | Error Message: " . $message . ' | Context: ' . json_encode($context));
}
} catch (InvalidArgumentException $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));
} catch (Exception $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));
}
}

Expand Down Expand Up @@ -422,7 +421,7 @@ protected function default_base_flush_logs()
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output($output . PHP_EOL)->_display();
exit;
} catch (Exception $e) {
$message = 'Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage();
$message = __get_error_message__($e);
log_message('error', $message);
echo $message . PHP_EOL;
exit;
Expand Down
15 changes: 7 additions & 8 deletions hungng/HungNG_CI_Base_Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ protected function jsonResponsePretty($response = array(), $status = 200)
*/
protected function errorExceptionResponse($exception)
{
$errorMessage = "Error Code: " . $exception->getCode() . " - Error File: " . $exception->getFile() . " - Error Line: " . $exception->getLine() . " - Error Message: " . $exception->getMessage();
log_message('error', $errorMessage);
log_message('error', $exception->getTraceAsString());
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));

$response = array();
$response['code'] = StatusCodes::HTTP_BAD_REQUEST;
Expand Down Expand Up @@ -366,11 +365,11 @@ protected function log($name = '', $message = '', $context = array(), $inputLeve
log_message($inputLevel, $name . " | Error Message: " . $message . ' | Context: ' . json_encode($context));
}
} catch (InvalidArgumentException $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));
} catch (Exception $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
log_message('error', __get_error_message__($exception));
log_message('error', __get_error_trace__($exception));
}
}

Expand Down Expand Up @@ -422,7 +421,7 @@ protected function default_base_flush_logs()
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output($output . PHP_EOL)->_display();
exit;
} catch (Exception $e) {
$message = 'Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage();
$message = __get_error_message__($e);
log_message('error', $message);
echo $message . PHP_EOL;
exit;
Expand Down
45 changes: 25 additions & 20 deletions system/core/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
/**
* Determines if the current version of PHP is equal to or greater than the supplied value
*
* @param string
* @param string $version
* @return bool TRUE if the current version is $version or higher
*/
function is_php($version)
Expand Down Expand Up @@ -112,7 +112,7 @@ function is_linux()
* on Unix servers if safe_mode is on.
*
* @link https://bugs.php.net/bug.php?id=54709
* @param string
* @param string $file
* @return bool
*/
function is_really_writable($file)
Expand Down Expand Up @@ -160,9 +160,9 @@ function is_really_writable($file)
* exist it is instantiated and set to a static variable. If it has
* previously been instantiated the variable is returned.
*
* @param string the class name being requested
* @param string the directory where the class should be found
* @param mixed an optional argument to pass to the class constructor
* @param string $class the class name being requested
* @param string $directory the directory where the class should be found
* @param mixed $param an optional argument to pass to the class constructor
* @return object
*/
function &load_class($class, $directory = 'libraries', $param = NULL)
Expand Down Expand Up @@ -233,7 +233,7 @@ function &load_class($class, $directory = 'libraries', $param = NULL)
* Keeps track of which libraries have been loaded. This function is
* called by the load_class() function above
*
* @param string
* @param string $class
* @return array
*/
function &is_loaded($class = '')
Expand All @@ -259,10 +259,10 @@ function &is_loaded($class = '')
* This function lets us grab the config file even if the Config class
* hasn't been instantiated yet
*
* @param array
* @param array $replace
* @return array
*/
function &get_config(Array $replace = array())
function &get_config(array $replace = array())
{
static $config;

Expand Down Expand Up @@ -314,7 +314,7 @@ function &get_config(Array $replace = array())
/**
* Returns the specified config item
*
* @param string
* @param string $item
* @return mixed
*/
function config_item($item)
Expand Down Expand Up @@ -427,9 +427,9 @@ function is_cli()
* This function will send the error page directly to the
* browser and exit.
*
* @param string
* @param int
* @param string
* @param string $message
* @param int|float $status_code
* @param string $heading
* @return void
*/
function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
Expand Down Expand Up @@ -462,8 +462,8 @@ function show_error($message, $status_code = 500, $heading = 'An Error Was Encou
* However, instead of the standard error template it displays
* 404 errors.
*
* @param string
* @param bool
* @param string $page
* @param bool $log_error
* @return void
*/
function show_404($page = '', $log_error = TRUE)
Expand All @@ -484,8 +484,8 @@ function show_404($page = '', $log_error = TRUE)
* We use this as a simple mechanism to access the logging
* class and send messages to be logged.
*
* @param string the error level: 'error', 'debug' or 'info'
* @param string the error message
* @param string $level the error level: 'error', 'debug' or 'info'
* @param string $message the error message
* @return void
*/
function log_message($level, $message)
Expand All @@ -498,6 +498,11 @@ function log_message($level, $message)
$_log[0] =& load_class('Log', 'core');
}

// 2023-03-29: In log_message - If message not string, force string with json_encode
if (!is_string($message)) {
$message = json_encode($message);
}

$_log[0]->write_log($level, $message);
}
}
Expand All @@ -509,8 +514,8 @@ function log_message($level, $message)
/**
* Set HTTP Status Header
*
* @param int the status code
* @param string
* @param int $code the status code
* @param string $text
* @return void
*/
function set_status_header($code = 200, $text = '')
Expand Down Expand Up @@ -804,8 +809,8 @@ function html_escape($var, $double_encode = TRUE)
* Helper function used to convert a string, array, or object
* of attributes to a string.
*
* @param mixed string, array, object
* @param bool
* @param mixed $attributes string, array, object
* @param bool $js
* @return string
*/
function _stringify_attributes($attributes, $js = FALSE)
Expand Down

0 comments on commit 80cba82

Please sign in to comment.