Skip to content

Commit

Permalink
misc for debuggers
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Nov 6, 2023
1 parent 051954f commit 71b046d
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 158 deletions.
2 changes: 1 addition & 1 deletion assets/core/dist/debugger/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

17 changes: 2 additions & 15 deletions assets/core/src/debugger/layouts/DefaultLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,12 @@
</div>
</template>

<script>
<script setup>
import { useRoute } from 'vue-router';
import { goToLast } from '../services/nav';
import { currentData, currentId } from '../services/store';
export default {
name: 'DefaultLayout',
setup() {
const route = useRoute();
return {
currentId,
currentData,
route,
goToLast
};
}
};
const route = useRoute();
</script>

<style scoped>
Expand Down
16 changes: 16 additions & 0 deletions assets/core/src/debugger/services/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ export function stateColor(value, avg) {
return 'info';
}
}

export function httpStatusColor(status) {
if (status >= 300 && status < 400) {
return 'info';
}

if (status >= 400 && status < 500) {
return 'warning';
}

if (status >= 200 && status < 300) {
return 'success';
}

return 'danger';
}
18 changes: 13 additions & 5 deletions assets/core/src/debugger/views/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@
{{ item.ip }}
</td>
<td>
{{ item.method }}
<div>
{{ item.method }}
</div>
<div>
<span v-if="item.ajax" class="badge bg-danger">
AJAX | API
</span>
</div>
</td>
<td>
<td style="word-break: break-all">
<a :href="item.url"
target="_blank"
class="link-secondary">
Expand All @@ -59,7 +66,9 @@
{{ dateFormat(item.time) }}
</td>
<td>
{{ item.response?.status }}
<span class="badge" :class="`bg-${httpStatusColor(item.response?.status || 0)}`">
{{ item.response?.status }}
</span>
</td>
</tr>
</tbody>
Expand All @@ -73,6 +82,7 @@ import { onMounted, reactive, ref, toRefs } from 'vue';
import router from '../routes.js';
import $http from '../services/http.js';
import { currentId } from '../services/store.js';
import { httpStatusColor } from '../services/utilities.js';
const state = reactive({
items: [],
Expand All @@ -87,8 +97,6 @@ onMounted(async () => {
});
function selectId(id) {
currentId.value = id;

router.push('/system/' + id);
}
Expand Down
1 change: 0 additions & 1 deletion assets/core/src/debugger/views/Database.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@

<script setup>
import { computed, ref } from 'vue';
import { onBeforeRouteUpdate } from 'vue-router';
import BsModal from '../components/BsModal.vue';
import QueryInfo from '../components/db/QueryInfo.vue';
import DefaultLayout from '../layouts/DefaultLayout.vue';
Expand Down
80 changes: 41 additions & 39 deletions src/Core/Application/WebApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Log\LogLevel;
use ReflectionException;
use Stringable;
use Throwable;
use Windwalker\Core\CliServer\CliServerClient;
Expand All @@ -32,6 +33,7 @@
use Windwalker\Core\Router\Navigator;
use Windwalker\Core\Security\Exception\InvalidTokenException;
use Windwalker\Core\Service\ErrorService;
use Windwalker\Core\Service\LoggerService;
use Windwalker\DI\Container;
use Windwalker\DI\Exception\DefinitionException;
use Windwalker\DI\Exception\DefinitionResolveException;
Expand Down Expand Up @@ -235,8 +237,8 @@ public function runContext(AppContext $appContext, ?callable $handler = null): R

$middlewares = MiddlewareRunner::chainMiddlewares(
$this->middlewares,
fn(ServerRequestInterface $request) => static::anyToResponse(
$this->dispatchController($container)
static fn(ServerRequestInterface $request) => static::anyToResponse(
$container->get(AppContext::class)->dispatchController()
)
);

Expand Down Expand Up @@ -360,47 +362,17 @@ public function stopContext(AppContext $appContext): void
$this->releaseProviders($container);
}

protected function dispatchController(Container $container)
{
$appContext = $container->get(AppContext::class);

try {
return $appContext->dispatchController();
} catch (ValidateFailException $e) {
if ($this->isDebug() || $appContext->isAjax()) {
throw $e;
}

$appContext->addMessage($e->getMessage(), 'warning');
$nav = $appContext->service(Navigator::class);

return $nav->back();
} catch (Throwable $e) {
if ($appContext->isCliRuntime()) {
$appContext->logError($e);
}

if (
$appContext->isDebug()
|| strtoupper($appContext->getRequestMethod()) === 'GET'
|| $appContext->getAppRequest()->isAcceptJson()
|| $appContext->isAjax()
) {
throw $e;
}

$appContext->addMessage($e->getMessage(), 'warning');
$nav = $appContext->service(Navigator::class);

return $nav->back();
}
}

/**
* @param AppContext $app
* @param ServerRequestInterface $request
* @param iterable $middlewares
*
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws DefinitionException
* @throws DefinitionResolveException
* @throws ReflectionException
* @throws Throwable
*/
protected function dispatch(
AppContext $app,
Expand All @@ -413,7 +385,7 @@ protected function dispatch(
return $runner->createRequestHandler($middlewares)
->handle($request);
} catch (ValidateFailException | InvalidTokenException $e) {
if ($app->isDebug()) {
if ($app->isDebug() || $app->isAjax()) {
throw $e;
}

Expand All @@ -422,10 +394,15 @@ protected function dispatch(

return $this->redirect($nav->back());
} catch (Throwable $e) {
if ($app->isCliRuntime()) {
$this->logError($e);
}

if (
$app->isDebug()
|| strtoupper($app->getRequestMethod()) === 'GET'
|| $app->getAppRequest()->isAcceptJson()
|| $app->isAjax()
) {
throw $e;
}
Expand All @@ -437,6 +414,31 @@ protected function dispatch(
}
}

/**
* @param Throwable $e
*
* @return void
*
* @throws ReflectionException
* @throws \Windwalker\DI\Exception\DefinitionException
*/
protected function logError(Throwable $e): void
{
// Do not log 40x errors.
if ($e->getCode() >= 400 && $e->getCode() < 500) {
return;
}

// $message = ErrorLogHandler::handleExceptionLogText($e, $this->app->path('@root'));

$this->retrieve(LoggerService::class)
->error(
$this->app->config('error.log_channel') ?? 'error',
$e->getMessage(),
['exception' => $e]
);
}

/**
* Redirect to another URL.
*
Expand Down
25 changes: 0 additions & 25 deletions src/Core/Controller/DelegatingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,4 @@ public function setViewMap(array $viewMap): static

return $this;
}

/**
* @param Throwable $e
*
* @return void
*
* @throws ReflectionException
* @throws \Windwalker\DI\Exception\DefinitionException
*/
protected function logError(Throwable $e): void
{
// Do not log 40x errors.
if ($e->getCode() >= 400 && $e->getCode() < 500) {
return;
}

// $message = ErrorLogHandler::handleExceptionLogText($e, $this->app->path('@root'));

$this->app->service(LoggerService::class)
->error(
$this->app->config('error.log_channel') ?? 'error',
$e->getMessage(),
['exception' => $e]
);
}
}
2 changes: 1 addition & 1 deletion src/Debugger/Module/Ajax/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AjaxController
#[JsonApi]
public function history(#[Autowire] DashboardRepository $repository): array
{
return $repository->getItems(100, false);
return array_values($repository->getItems(100, false));
}

#[JsonApi]
Expand Down
1 change: 1 addition & 0 deletions src/Debugger/Module/Dashboard/DashboardRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function writeFile(string $id, Collection $collector): FileObject
'response' => $collector->getDeep('http.response'),
'time' => microtime(true),
'ip' => $collector->getDeep('http.remoteIP'),
'ajax' => $collector->getDeep('http.request.ajax'),
];

$folder = $this->getCacheFolder()->appendPath('/' . $id);
Expand Down
1 change: 1 addition & 0 deletions src/Debugger/Subscriber/DebuggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ function () use ($app, $event, $appReq) {
'version' => $req->getProtocolVersion(),
'target' => $req->getRequestTarget(),
'env' => $_ENV ?? [],
'ajax' => $app->isAjax() || $appReq->isAcceptJson()
];
$http['systemUri'] = $appReq->getSystemUri();
$http['overrideMethod'] = $appReq->getOverrideMethod();
Expand Down

0 comments on commit 71b046d

Please sign in to comment.