Skip to content

Commit

Permalink
Merge pull request #206
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
leonardosahon authored Nov 24, 2024
2 parents 11ed9f4 + 5c09a8f commit 2c52af4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Core/Api/ApiEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ public static function end(bool $print_existing_result = true) : ?string {
"active_prefix" => self::$prefix ?? "",
"similar_routes" => $json_error
];

self::exception(
"NoRequestExecuted",
"No valid handler for route [<span style='color: #F3F9FA'>$uri</span>]<br><br>
Expand Down
53 changes: 33 additions & 20 deletions src/Core/CoreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ private function container(?string $title, ?string $body, array $other = []): ar
$echo_error = $other['echo_error'] ?? true;
$display_error = $env == "DEVELOPMENT" || $other['core'] == "view";
$cli_mode = LayConfig::get_mode() === LayMode::CLI;
$use_json = $cli_mode ? false : ($this->throw_as_json && !isset(LayConfig::user_agent()['browser']));
$use_json = $cli_mode ? false : $this->throw_as_json;
$use_json = $use_json ?: !isset(LayConfig::user_agent()['browser']);
$show_internal_trace = $other['show_internal_trace'] ?? self::$show_internal_trace;

if (!empty(@$other['raw'])) {
Expand Down Expand Up @@ -343,27 +344,34 @@ private function container(?string $title, ?string $body, array $other = []): ar
$error_json = [
"code" => $other['json_packet']['code'] ?? 500,
"message" => $other['json_packet']['message'] ?? "Internal Server Error",
"packet" => $other['json_packet']['others'] ?? [],
];

$error_json['packet']['more_info'] = str_replace(["\n"], " ", ($body ? strip_tags($body) : ""));

if(self::$show_x_info) {
$error_json["x_info"] = [
"env" => $env,
"host" => $origin,
"referer" => $referer,
"cors" => $cors_active,
"ip" => $ip,
"os" => $os,
"trace" => [
"app" => $stack_json['app'] ?? null,
"internal" => $stack_json['internal'] ?? null,
],
"headers" => $headers_json,
];
if($env == "DEVELOPMENT") {
$error_json['packet']['more_info'] = str_replace(["\n"], " ", ($body ? strip_tags($body) : ""));

if (self::$show_x_info) {
$error_json["x_info"] = [
"env" => $env,
"host" => $origin,
"referer" => $referer,
"cors" => $cors_active,
"ip" => $ip,
"os" => $os,
"trace" => [
"app" => $stack_json['app'] ?? null,
"internal" => $stack_json['internal'] ?? null,
],
"headers" => $headers_json,
];
}
}

$error_json["status"] = in_array($error_json['code'], [
ApiStatus::INTERNAL_SERVER_ERROR->value,
ApiStatus::NOT_FOUND->value,
ApiStatus::TOO_MANY_REQUESTS->value,
]) ? 'error' : 'success';

$code = ApiStatus::tryFrom($error_json['code']);
$code = $code->value ?? 500;

Expand All @@ -374,8 +382,14 @@ private function container(?string $title, ?string $body, array $other = []): ar
return [
"act" => $other['act'] ?? "allow",
"error" => json_encode($error_json),
"as_string" => $return_as_string
"as_string" => $return_as_string,
"display_error" => $display_error,
"echo_error" => $echo_error,
];

$error = json_encode($error_json);
$display_error = true;
$echo_error = true;
}

if (!$use_json && !$cli_mode && $display_error) {
Expand Down Expand Up @@ -416,7 +430,6 @@ private function container(?string $title, ?string $body, array $other = []): ar
"display_error" => $display_error,
"echo_error" => $echo_error,
];

if(!$this->always_log)
return $rtn;

Expand Down
7 changes: 0 additions & 7 deletions src/Orm/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
**/
class SQL
{
private static bool $IS_FIRST_QUERY = true;

use IsSingleton;
use Config;
use SelectorOOP;
Expand Down Expand Up @@ -86,11 +84,6 @@ final public function query(
"No connection detected: <h5>Connection might be closed!</h5>",
);

if(self::$IS_FIRST_QUERY)
$this->__rollback_on_error();

self::$IS_FIRST_QUERY = false;

$option = LayArray::flatten($option);
$debug = $option['debug'] ?? false;
$catch_error = $option['catch'] ?? false;
Expand Down
23 changes: 6 additions & 17 deletions src/Orm/Traits/TransactionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,23 @@ trait TransactionHandler
private static bool $DB_IN_TRANSACTION = false;
private static int $BEGIN_TRANSACTION_COUNTER = 0;

private static function cache_counter() : void
{
self::save_to_session("BEGIN_TRANSACTION_COUNTER", self::$BEGIN_TRANSACTION_COUNTER);
}

private static function decrease_counter() : void
{
self::$BEGIN_TRANSACTION_COUNTER--;

if(self::$BEGIN_TRANSACTION_COUNTER < 0)
self::$BEGIN_TRANSACTION_COUNTER = 0;

self::cache_counter();
}

final public function in_transaction() : bool
final public function __rollback_on_error() : void
{
return self::$DB_IN_TRANSACTION;
if((self::$DB_IN_TRANSACTION || self::$BEGIN_TRANSACTION_COUNTER == 0) && isset(self::$link))
$this->rollback();
}

final public function __rollback_on_error() : void
final public function in_transaction() : bool
{
$trx_exists = (bool) self::get_from_session("BEGIN_TRANSACTION_COUNTER");

if($trx_exists && isset(self::$link))
$this->rollback();
return self::$DB_IN_TRANSACTION;
}

/**
Expand All @@ -55,14 +46,12 @@ final public function begin_transaction(#[ExpectedValues([
MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT,
])] int $flags = 0, ?string $name = null) : bool
{
self::$DB_IN_TRANSACTION = true;
self::$BEGIN_TRANSACTION_COUNTER++;

self::cache_counter();

if(self::$DB_IN_TRANSACTION && !$name)
return true;

self::$DB_IN_TRANSACTION = true;
return self::new()->get_link()->begin_transaction($flags, $name);
}

Expand Down

0 comments on commit 2c52af4

Please sign in to comment.