管理框架错误 [英] Manage the errors of a framework

查看:130
本文介绍了管理框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发个人MVC框架以供学习。但是每次我陷入这个问题:错误。

I'm trying to develop a personal MVC framework for learning purposes. But every time I'm stuck in this problem: errors.

我觉得我对它们的处理非常糟糕。目前,我有一个异常系统(一切都转换为异常,甚至PHP触发了错误),该异常系统被捕获在try {}块中,该块包含框架和用户应用程序的每一行代码。

I feel like I'm handling them very bad. Currently I have an exception system (everything is converted to exception, even PHP triggered errors) that is catch in a try{} block that contains every line of code of the framework and the user application.

我正在像对待其他错误一样处理找不到控制器或找不到操作之类的错误,例如无法连接到数据库。但是我觉得后者在某种程度上更是一种例外,而不是一种非常普通的找不到控制器(404)。

I'm treating errors such as "controller not found" or "action not found" like any other, for example "unable to connect to the database". But I feel like the latter is somehow more an "exception" rather than a pretty common "controller not found (404)".

目前我正在使用一个错误从某种意义上说,当发生错误时,我会加载特定的操作并为每种类型的错误加载特定的视图文件,这就是在MVC在我的框架中处理该副本的方式。我没有使用我的框架的MVC(按MVC,我的意思是加载控制器,运行操作,加载模型和用户应用程序视图的所有机制),因为MVC中的错误可能会导致触发错误,它将尝试使用MVC对其进行管理,这将再次触发相同的错误,然后再次无限循环加载MVC,依此类推。

Also currently I'm using an error handling that pretty much copy the way MVC works in my framework, in the sense that when an error occurs I load a specific action and load a specific view file for each type of error. I'm not using the MVC (by MVC I mean all the mechanism that load a controller, run an action, load a model and views for the user application) of my framework because an error in the MVC could cause an error to be triggered, which would try to manage it with the MVC which would trigger the same error again and then the MVC to be loaded again and so on in an infinite loop.

应该如何我处理我的框架的每个错误?现在的最佳做法是什么?

How should I handle every error of my framework? What are the best practice right now?

推荐答案

执行控制器恕我直言会产生两个异常:

The execution of controller IMHO can generate two exceptions:


  • 未找到:缺少控制器或方法时

  • 权限被拒绝:当 ACL 阻止访问

  • not found: when controller or method is missing
  • permission denied: when ACL blocked access

要处理此问题,我会选择类似以下代码。您可以使用多个catch块。

To handle this, i would just go with something like following code. And you can use multiple catch block.

try
{
    $controller->$command($request, $response);
}
catch(AccessDeniedException $e)
{
    $controller = new ErrorController;
    $controller->accessDenied($request, $response);
}
catch(NotFoundException $e)
{
    $controller = new ErrorController;
    $controller->notFound($request, $response);
}

您可以让 AccessDeniedException 也可以从模型层中冒出来,但是通常这是一个不好的做法。异常应在抛出该异常的同一抽象级别内进行,或者在发生关键异常(对象本身无法处理时)的情况下,这些异常可能会穿透一个抽象边界。并且异常不应离开 模型层 ,而应在层中创建错误状态,并在您当前的 View 实例中进行处理。

You can let AccessDeniedException to bubble up from Model Layer too, but usually it is a bad practice. Exception should be handles within same level of abstraction, where it was thrown, or, in case of critical exceptions (when object itself is unable to deal with it), the exceptions might penetrate ONE abstraction boundary. And exceptions should NOT leave the Model Layer, instead they should create error state in the layer, and be processed in your current View instance.

重点是:对于所有错误,请使用 magical handler 代替,您应该在错误产生的地方附近处理错误。

The point is this: instead of magical handler for all errors, you should handle errors close to the place where it originated.

这篇关于管理框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆