执行异常处理的正确方法 [英] The proper way to do exception handling

查看:144
本文介绍了执行异常处理的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,在编写代码时,通常会做的是这样的

Now what I generally do when writing code is something like this

function changeBookAuthor(int $id, string $newName){
  if(!$newName){
   throw new MyAppException('No author name was provided');
  }

  $book = Books::find($id);

  if(!$book){
   throw new MyAppException('The provided book id could not be found');
  }
}

在laravel doc中我们看到:

in the laravel doc we see:

https://laravel.com/docs/5.4/errors

https://laravel.com/docs/5.4/errors

public function report(Exception $exception)
{
    if ($exception instanceof CustomException) {
        //
    }

    return parent::report($exception);
}

现在我如何正确处理异常?他们都是一样的例外,他们也没有任何代码。我应该提供错误代码吗?

Now how to I properly handle the exception? they are all the same exception and they have no code neither. Should I provide an error code?

php异常的问题是它们使用整数。是非常烦人的imho。更好的将是'changeauthor_bookid_notfound'作为代码而不是随机数。我应该为每个异常创建一个异常类吗?例如不重用 MyAppException 这似乎有点乏味。我会有一个万亿类。

the problem with php exception is that they use integers. Is quite annoying imho. Better would be 'changeauthor_bookid_notfound' as code instead of a random number. Should I create an exception class for each single exception? e.g. not reuse MyAppException that seems a bit tedious. I would have a trillion classes.

现在如果一个特殊的异常我想要特殊的处理,与我的代码,我不能轻易地做到这一点。我没有代码检查(例如 $ exception-> code == 3331然后执行特殊的),我没有自定义异常类既不是

Now if for a special exception I want special handling, with my code, I cannot easily do it. I have no code to check for (e.g. $exception->code == 3331 then do special) and I don't have custom exception classes neither

这是一个很好的固定方法来处理这种情况?
代码,每个错误的新类,其他的一切都在一起?

what is a proven good solid way to handle this case? code, new class on each error, something else all together?

如果提供一个代码,什么是一个很好的方法?

and if provide a code, what is a nice way to do it?

推荐答案

正确的方法是为每个异常定义一个自定义的Exception类,或者根据抛出的错误类型,但是意识到Laravel已经有许多内置的异常和机制来处理您概述的用例。

The "proper" way to do it would be to define either a custom Exception class for each exception, or to define custom exceptions based on the type of error being thrown, however realize that Laravel already has many built in exceptions and mechanics for handling the use cases you outlined.

例如,在Book Not Found异常,而不是自己手动触发异常,您可以使用 Books :: findOrFail($ id); ,它会抛出一个 ModelNotFoundException

For instance, in the case of the "Book Not Found" exception, rather than manually triggering an exception yourself, you could use Books::findOrFail($id); which throws an instance of ModelNotFoundException when appropriate.

此外,在PHP中,没有必要处理未提供参数的异常。除非明确表示为可选,否则所有方法参数都是必需的,如果缺少参数,Laravel将抛出一个PHP异常。

Also, in PHP there is no need to handle exceptions for unprovided arguments. Unless expressly denoted as optional, all method arguments are required, and Laravel will throw a PHP exception if an argument is missing.

另外,Laravel还提供了 abort() magic方法会引发HTTP错误以及自定义错误消息,可以这样使用:

Additionally, Laravel provides the abort() magic method which throws a HTTP error along with a custom error message and can be used like so:

abort(418, "I'm a teapot...")

所以如果您必须重新发明轮子,正确的方法是定义自定义异常类并定义这些类的自定义处理程序,但是意识到Laravel已经有许多内置的工具来管理异常,而无需这样做。

So, if you must reinvent the wheel, the proper way is to define custom exception classes and define the custom handlers for those classes, but realize that Laravel already has many built in tools for managing exceptions without needing to do so.

这篇关于执行异常处理的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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