开发者

Symfony error handling for both website and REST API of website

开发者 https://www.devze.com 2023-01-13 08:12 出处:网络
I have a symfony project. There is a website and a rest开发者_开发知识库 API that projects all the actions that can be done on the website.

I have a symfony project.

There is a website and a rest开发者_开发知识库 API that projects all the actions that can be done on the website.

I would like to manage the data handling (get, update, delete) for both website and API on the same code.

I realized that I need to put all the code in the MODEL so that it will be accessible via the API module and the website modules.

How do I handle errors in an Elegant way for both componenets?


I would either:

  • Throw exceptions in the model methods. The exceptions can contain the message of what went wrong and your API actions / standard module actions can handle the exceptions as necessary.
  • Return success/failure codes. Have your delete/update/get methods return various codes indicating what could have possibly gone wrong. Alternatively, you could return true/false and have some sort of getLastError method.
0

精彩评论

暂无评论...
验证码 换一张
取 消