1

I am new in zendframework. I am using apigility for rest services and ApiProblemListner to return the response if any error occures.

I have one function in model and this function just through an exception using php exception to use in catch block

I am using model function as the utility function in controller to catch those Exception. While catching exception I am using as

try{
   imageUploade(); // this function in model and throwing exception if any error
}catch(\Exception $e){
   return new ApiProblemResponse(new ApiProblem(500 , $e->getMessage()));
}

if imageUploade() throw an exception if the image size is more then I am able to catch the exception in catch block. I tried echo $e->getMessage(); and its printing the exception bt if I use new ApiProblem(500 , $e->getMessage()) it is not retuning the json error response with the 500 message. It is returning nothing. even it is not showing any error.

Seems like it is unable to render the error with this class. I am not sure if any event needs to add.

I have tried to search for documents but unable to find it.

Thanks in advance.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PhpDev.Raj
  • 113
  • 5
  • Where do you return this `ApiProblemResponse`? Can you add some context to your question. – Wilt Aug 31 '15 at 12:11
  • I am returning it in controller itself. ImageUpload() function is in model class and using this function in controller. whtever error happens it will be handled in controller itself. That message I am using in apiProblemResponse. I am also including the classes as well – PhpDev.Raj Aug 31 '15 at 12:19

1 Answers1

1

Normally it should work if you return an ApiProblemResponse straight from a controller action.
Are you sure your Api-Problem module is active?

Try once like this:

<?php

Application\Namespace;

use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;

class IndexController
{
    /**
     * @return ApiProblemResponse
     */
    public function indexAction()
    {
        return new ApiProblemResponse(new ApiProblem(500, 'test'));
    }
}

If that doesn't work then I think your Api-Problem module is not running or the exception is never caught.

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Thanks.. Actually normally Api-Problem is working fine. But when in controller I am catching the Exception which is from other model function which is I am unable to use with Api-Problem module. Suppose, ImageUpload is a function throwing any error because image type is not correct, then I am not able to get the error using $e->getMessage(); but if I use in new ApiProblem(500, $e->getMessage()); then it is giving me blank response which should be jason from api-problem module – PhpDev.Raj Sep 01 '15 at 08:12
  • hey I have updated the question. Please check the question once again and thanks once again @Wilt – PhpDev.Raj Sep 01 '15 at 08:40
  • @PhpDev.Raj Did you try to do what I wrote in my answer? Does it work if you do this? Please give some feedback on this... – Wilt Sep 02 '15 at 07:37
  • @PhpDev.Raj then my guess would be that there is something wrong in the `imageUpload` method where you throw the exception. Can you show the code in this method? Can you test if you ever end up un the `catch` part of the code (by using debug, or add a `die('test')` in the catch expression. – Wilt Sep 02 '15 at 08:27