Cakephp: How to handle exception if action is missing - php

I am developing an Api in Cakephp. I need to handle the exception if android app calls wrong function or wrong controller. I need to know how can I can handle that and return the response in json to my android app. I mean I know I can write something in my beforefilter function because this function will execute first But I don't know how I can catch first the exception or how can I detect the event. By Googling I come to some solution but still it is not working. Here is my code below which I have tried.
App/Lib/AppErrorHandler.php
class AppErrorHandler extends ExceptionRenderer{
public static function handleException($error) {
if ($error instanceof MissingActionException) {
echo "incorrect controller action name";
exit;
}
}
}
?>
in bootstrap.php
App::uses('AppErrorHandler', 'Lib');
I didn't do anything in my Api regarding exception. Please let me know if I have to write some code in Api class too

You are extending ExceptionRenderer for your custom ExceptionHandler, which is the right way. Now you are trying to override method handleException which basically is not present in the baseClass. So Its wrong to do that..
Now for your solution:
You need to override function render() from ExceptionRenderer class. The way you do it is below:
class AppErrorHandler extends ExceptionRenderer{
public function render() {
if ($this->method) {
if($this->error instanceof MissingActionException) {
// echo here whatever you want..
}
call_user_func_array(array($this, $this->method), array($this->error));
// this line is required to render the normal page as per the error code.. 400 or 500 or similar..
}
}
}
P.S: in lib/Cake/Error you will find the files which handle the default exceptions. Here in exceptions.php you will find the different errors which are thrown or can be thrown.

Related

Laravel Controller -> API class

I'm struggling with how to do this correctly following best practices. It might be difficult to explain but i'll try my best here.
I have an external API I need to make very many different calls to. So what I did was creating a class in the App folder called Api.php for now. It's using Guzzle for API calls.
In the Controller for the view I create the Api object in the needed functions and call the corresponding function in the API class.
Controller
public function uploadDevice(Request $request)
{
## Validation etc is performed
// Calling the API
$api = new Api();
$api->uploadDevice();
}
Api.php
class Api
{
private $token;
public function __construct(){}
public function checkIfHasToken(){}
public function getTokenFromSession(){}
public function getFreshToken(){}
public function uploadDevice(){}
}
Some questions
The checkIfHasToken() needs to be called before every request. Should it be done in the constructor, first in each function doing API calls or directly from the Controller?
Exceptions : Where should I do the Try/catch etc ? Should it be done in the Api class where it's needed or in the Controller by calling each and every function from the API class and wrapping it in try/catch?
Redirects : I want to redirect back to the Route the request came from with every possible errors or success message included. So if I have a try/catch I want to redirect with the result of the catch included. Where to put this logic? Redirecting from the nested function does not seem to work. So then I'm back to calling each and every function in the Api class from the Controller one by one and handle the exceptions/errors/validations separately in the Controller?
Maybe I'm thinking too much about this or making it more complicated than it needs to be. Not sure anymore.
// Controller
public function __construct(ApiService $apiService)
{
$this->api = $apiService;
}
public function uploadDevice(Request $request)
{
// Ensure that the user has a token in a custom HTTP request or in a middleware somewhere
try {
$this->api->uploadDevice();
}
catch (Exception $exception){
return redirect()->back();
//You can include errors from $exception here.
}
}
// Service
class ApiService
{
public function uploadDevice()
{
return 'I did a thing';
}
}
Explaination
Laravel has many ways to do the same thing, it is all about what you need and how you want your application to scale.
Checking if a token is present or valid should be done in a middleware.
A try catch can be anywhere depending on how much you need to see in the exception, normally just in a controller is ok, but you can
do this in many ways. I personally like to make an event listener
for any http error.
Return redirect back from the controller will be fine to always redirect to the place that invoked the controller
The checkIfHasToken() needs to be called before every request. Should
it be done in the constructor, first in each function doing API calls
or directly from the Controller?
If it needs to be called for every request, I suggest making it middleware as it's made for this purpose.
Exceptions : Where should I do the Try/catch etc ? Should it be done
in the Api class where it's needed or in the Controller by calling
each and every function from the API class and wrapping it in
try/catch?
This depends, if you want to be able to control the output when an exception occurs then you probably want it in your controller. If you can program something to do when the exception occurs (return unsuccessful for instance), do it in a lower level (api).
Redirects : I want to redirect back to the Route the request came from
with every possible errors or success message included. So if I have a
try/catch I want to redirect with the result of the catch included.
Where to put this logic? Redirecting from the nested function does not
seem to work. So then I'm back to calling each and every function in
the Api class from the Controller one by one and handle the
exceptions/errors/validations separately in the Controller?
You can go back by returning redirect()->back() as the response, the best way to show errors would to include them somewhere. I suggest using session()->flash() for this. These calls can be made from the try/catch.

In PHP is there a way I can avoid repeating the same try / catch code in multiple methods of a class?

I've been searching for an existing question that already asks this, but I wasn't able to find any questions that quite ask what I'm trying to figure out. The most similar question I could find was this: php 5.3 avoid try/catch duplication nested within foreach loop (code sandwich)
Okay so the place I work at has a web application with a PHP back end. We use an MVC type structure. I'm writing a controller that has multiple methods and in each of my methods I'm wrapping my code with identical try / catch code. In the catch, I pass the exception, a reference to the class, and a reference to the function to a method that builds an error message so that the error messages are formatted the same across the application. It looks something this:
class MyController {
public function methodA() {
try {
// code for methodA
} catch(Exception $e) {
$errorMessage = Tasks::buildErrorMessage($e, __CLASS__, __FUNCTION__);
throw new Exception($errorMessage);
}
}
public function methodB() {
try {
// code for methodB
} catch(Exception $e) {
$errorMessage = Tasks::buildErrorMessage($e, __CLASS__, __FUNCTION__);
throw new Exception($errorMessage);
}
}
public function methodC() {
try {
// code for methodC
} catch(Exception $e) {
$errorMessage = Tasks::buildErrorMessage($e, __CLASS__, __FUNCTION__);
throw new Exception($errorMessage);
}
}
}
So the buildErrorMessage function prevents each method from repeating the code that formats the error message, but there is something that really bothers me about have the same code spread through out every method in the class. I know that PHP doesn't support python-like decorator syntax, but just to demonstrate what I'm envisioning conceptually; I want the code to behave something more like this:
class MyController {
#DefaultErrorHandling()
public function methodA() {
// code for methodB
}
#DefaultErrorHandling()
public function methodB() {
// code for methodB
}
#DefaultErrorHandling()
public function methodC() {
// code for methodC
}
}
Where the #DefaultErrorHandling decorator would wrap each method in that standard try / catch. Is there a way I could achieve this behavior so I don't have to have all of these methods that have repeated code? Or am I thinking about error handling incorrectly?
Thanks to anyone who takes the time to answer this.
Have you looked at a writing a custom exception handler and using set_exception_handler?
What you are doing seems a bit like reinventing the wheel. Does the Exception not already have the info you are collecting in the trace? See: Exception::getTrace
Maybe buildErrorMessage does more? Anyway, I assume a custom exception handler is what you are after.
Not sure if there is a better way to solve this or not, but I created a logging class that formatted the log for me. Then just called this in my catch block.
To log the correct Class and Method, I the debug_backtrace() function. See this answer for more information.
Entry point that calls controller methods can wrap those calls with try / catch. That being said, if you are planning to use different type of error handlers on those methods then you can implement something in your base controller (or use trait) that keeps track of which handler should be invoked on each particular method. Something like
<?php
class MyController extends Controller
{
function __construct()
{
$this->setActionErrorHandler('function_name', 'handler');
}
}
Or just call it at the beginning of action method body. Keeping this type of configuration within class itself will help with readability. Not as neat as python example but better than somewhere in configuration files.
More generic error handlers can be implemented in php by using set_exception_handler mentioned by others.
I'm not really getting why there is such a requirement.

Joomla: Error 500 View not found when requesting subcontroller response

I'm trying to return the result of a task from a subcontroller with the following url:
index.php?option=com_example&task=subctrl.test&format=json
but I keep getting the 500 View not found Error...
class ExampleControllersSubctrl extends JControllerForm
{
public function test()
{
$result= array("val1","val2");
echo json_encode($result);
}
}
I've tried naming the subcontroller file both Subctrl.php & Subctrl.json.php but neither worked. I believe I shouldn't need a view to render the result based on other SO posts I've read but maybe that is incorrect.
This setup will eventually be used to return an Ajax call when I get it working.
What am I doing wrong here?
Add an exit statement after the echo statement or Joomla will continue processing the component and will try to call a view. Since no view value was set, no view will be found and the system will redirect to an error page. Full code below:
class ExampleControllerSubctrl extends JControllerForm
{
public function test()
{
$result= array("val1","val2");
echo json_encode($result);
exit();
}
}
Joomla also some other methods that you can use such as call jexit() or JFactory::getApplication()->close(). The general idea is to get the application to stop here. Continuing is a waste.
Also, had to make sure the class name is set right. Middle work should be Controller not Controllers.
The problem is that you're extending JControllerForm which will try and guess the view for your form if one isn't provided.
On Joomla 2.5 you can change JControllerForm to JController and that will resolve the problem.
As you have a JSON controller that Joomla is routing you to via format=json you don't need an exit on your test() method either.

php OOP - call static method from another class within class

I have the following class to another class in my main class.
class Products
{
public function __get( $key ){
return trim(functions::mssql_escape_string_rev($this->fields[ $key ]));
}
}
This beings back error: Call to undefined method functions::mssql_escape_string_rev()
Is there something wrong with my syntax or can this not be done?
Below is code used to autoload classes, this works for everything else so I know there is nothign wrong with the code. It just doesnt seem to initiate within the class.
// autoloader function called when we try to instantiate a class but haven't included the file
function __autoload($resource_name){
$resource_name = trim($resource_name);
try {
$filepath = CLASS_PATH."/class.".$resource_name.".inc.php";
if(#!include($filepath)){
throw new Exception('');
}
} catch(Exception $e) {
exit("Could not find the required file: ".$resource_name);
}
}
*******EDIT*****
Please ignore this, I made a stupid mistake and included the functions::mssql_escape_string_rev twice. Sorry for timewasting..
As the error says the problem is that functions::mssql_escape_string_rev() is not defined.
Since we can't see what you think is the definition we can not really help you.
For me it looks like the call should be Functions::mysql_escape_string_rev() with capital F and mysql.
Update
Calling static functions from another class works normally: http://codepad.org/wrfm5X7j
Maybe you are calling mysql_escape_string_rev before you included the functions class.

php symfony exception handling/error handling

Working on a symfony application that uses nusoap (is this the best method for integrating soap work with php/symfony?) for taking credit card payments.
I've simplified an example of my code below.
What I'm struggling with is the best way to handle exceptions. The example below only has 1 custom exception (where should my custom exceptions reside within the directory structure of symfony? (lib/exception?)) But what happens when there are several different types of exceptions that handle a specific error? It's not very elegant to have a try/catch block with 20 odd exceptions.
I'm also not sure of where I should be throwing and catching. I need to set some user flashes to alert the user of any problems, so I figure the catching should be done in the actions controller rather than within the class that handles the soap call.
Could anyone please advise where I might be going wrong?
I hate messy code/solutions and want to stick to the DRY principle as much as possible. I think I might also be missing some built in symfony functionality that might help with this but whenever I search I usually find examples that are for symfony 1.2, I'm using 1.4.
Some examples would be great, thanks.
lib/soap_payment.class.php
class SoapPayment
{
public function charge()
{
/*assume options are setup correctly for sake of example*/
try
{
$this->call();
}
catch (SoapPaymentClientFaultException $e)
{
/* should this be caught here? */
}
}
private function call()
{
$this->client->call($this->options);
if ($this->client->hasFault())
{
throw new SoapPaymentClientFaultException();
}
}
}
apps/frontend/payment/actions/actions.class.php
class paymentActions extends sfActions
{
public function executeCreate(sfWebRequest $request)
{
/* check form is valid etc */
$soap_payment = new SoapPayment();
try
{
$soap_payment->charge();
}
catch (SoapPaymentClientFaultException $e)
{
/* or throw/catch here? */
$this->getUser()->setFlash('error', ...);
$this->getLogger()->err(...);
}
/* save form regardless, will set a flag to check if successful or not in try/catch block */
}
}
One not very well known feature of Symfony is that exceptions can manage the content sent in a response. So you could do something like this:
class SoapException extends sfException
{
public function printStackTrace() //called by sfFrontWebController when an sfException is thrown
{
$response = sfContext::getInstance()->getResponse();
if (null === $response)
{
$response = new sfWebResponse(sfContext::getInstance()->getEventDispatcher());
sfContext::getInstance()->setResponse($response);
}
$response->setStatusCode(5xx);
$response->setContent('oh noes'); //probably you want a whole template here that prints the message that was a part of the SoapException
}
}
If you need a cleaner handling of SOAP exceptions, like setting flashes, etc. you'll probably have to catch each exception. One idea here might be to create a generic SoapException class that is extended by more specific SoapExceptions so you don't have to catch a bunch of different types. The above code may be a useful fallback mechanism as well.
Finally, yes, you should place custom exceptions in lib/exception.

Categories